You are here

class ReadonlymodeCommands in Read only mode 2.0.x

The drush commands.

Hierarchy

Expanded class hierarchy of ReadonlymodeCommands

1 string reference to 'ReadonlymodeCommands'
drush.services.yml in ./drush.services.yml
drush.services.yml
1 service uses ReadonlymodeCommands
readonlymode.commands in ./drush.services.yml
Drupal\readonlymode\Commands\ReadonlymodeCommands

File

src/Commands/ReadonlymodeCommands.php, line 12

Namespace

Drupal\readonlymode\Commands
View source
class ReadonlymodeCommands extends DrushCommands {

  /**
   * The read only mode manager.
   *
   * @var \Drupal\readonlymode\ReadonlymodeManager
   */
  protected $manager;

  /**
   * ReadonlymodeCommands constructor.
   *
   * @param \Drupal\readonlymode\ReadonlymodeManager $manager
   *   The read only mode manager.
   */
  public function __construct(ReadonlymodeManager $manager) {
    parent::__construct();
    $this->manager = $manager;
  }

  /**
   * Get the status of the read only mode.
   *
   * @command readonlymode:status
   *
   * @usage drush readonlymode:status
   *   Prints the warning of the read-only mode.
   */
  public function getStatus() {
    if ($this->manager
      ->isReadonly()) {
      $this
        ->io()
        ->warning($this->manager
        ->getWarningMessage());
      return;
    }
    $this
      ->io()
      ->success("The read-only mode is inactive.");
  }

  /**
   * Set the status of the read only mode.
   *
   * @param mixed $mode
   *   The new mode.
   *
   * @command readonlymode:set
   *
   * @usage drush readonlymode:set 1
   *   Activate the read only mode
   */
  public function setMode($mode) {

    // Cast the method argument to boolean.
    switch (strtolower($mode)) {
      case 'true':
      case 'on':
        $mode = TRUE;
        break;
      case 'false':
      case 'off':
        $mode = FALSE;
        break;
      default:
        $mode = (bool) $mode;
    }
    $this->manager
      ->setReadonly($mode);
    $message = 'Read-only mode ' . ($mode ? 'activated' : 'deactivated');
    $this
      ->io()
      ->success($message);
  }

  /**
   * After the deploy hook has run we can disable the readonly mode.
   *
   * @hook post-command deploy:hook
   */
  public function postDeploy($result, CommandData $commandData) {
    if ($result === 0) {

      // We could make this configurable.
      if ($this->manager
        ->isReadonly()) {
        $this->manager
          ->setReadonly(FALSE);
        $this
          ->io()
          ->success('Read-only mode disabled after deployment');
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ReadonlymodeCommands::$manager protected property The read only mode manager.
ReadonlymodeCommands::getStatus public function Get the status of the read only mode.
ReadonlymodeCommands::postDeploy public function After the deploy hook has run we can disable the readonly mode.
ReadonlymodeCommands::setMode public function Set the status of the read only mode.
ReadonlymodeCommands::__construct public function ReadonlymodeCommands constructor.