You are here

ReadonlymodeCommands.php in Read only mode 2.0.x

File

src/Commands/ReadonlymodeCommands.php
View source
<?php

namespace Drupal\readonlymode\Commands;

use Consolidation\AnnotatedCommand\CommandData;
use Drupal\readonlymode\ReadonlymodeManager;
use Drush\Commands\DrushCommands;

/**
 * The drush commands.
 */
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');
      }
    }
  }

}

Classes

Namesort descending Description
ReadonlymodeCommands The drush commands.