Skip to content

Commit

Permalink
Maintenance mode (#326)
Browse files Browse the repository at this point in the history
* Replace direct access to sites with getSites

* Add a new maintenance listener, which can enable/disable maintenance mode for specific commands

* Rename the getMaintenance to isMaintenance
  • Loading branch information
LOBsTerr authored and jmolivas committed Apr 22, 2018
1 parent e93efb2 commit 8ee1af0
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Drupal\Console\Core\EventSubscriber\ShowGeneratedFilesListener;
use Drupal\Console\Core\EventSubscriber\ShowGenerateInlineListener;
use Drupal\Console\Core\EventSubscriber\CallCommandListener;
use Drupal\Console\Core\EventSubscriber\MaintenanceModeListener;
use Drupal\Console\Core\Utils\ConfigurationManager;
use Drupal\Console\Core\Style\DrupalStyle;
use Drupal\Console\Core\Utils\ChainDiscovery;
Expand Down Expand Up @@ -310,6 +311,13 @@ private function registerEvents()
)
);

$dispatcher->addSubscriber(
new MaintenanceModeListener(
$this->container->get('console.translator_manager'),
$this->container->get('state')
)
);

$this->setDispatcher($dispatcher);
$this->eventRegistered = true;
}
Expand Down
33 changes: 33 additions & 0 deletions src/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ abstract class Command extends BaseCommand
*/
private $io;

/**
* @var bool
*/
private $maintenance = false;

/**
* {@inheritdoc}
*/
Expand All @@ -49,6 +54,34 @@ public function getIo()
return $this->io;
}

/**
* Check maintenance mode.
*
* @return bool
*/
public function isMaintenance()
{
return $this->maintenance;
}

/**
* Enable maintenance mode.
*
* @return $this
* Command.
*/
public function enableMaintenance()
{
$this->maintenance = true;
return $this;
}

/**
* Create Exception
*
* @return void
*
*/
public function createException($message) {
$this->getIo()->error($message);
exit(1);
Expand Down
116 changes: 116 additions & 0 deletions src/EventSubscriber/MaintenanceModeListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

/**
* @file
* Contains \Drupal\Console\Core\EventSubscriber\MaintenanceModeListener.
*/

namespace Drupal\Console\Core\EventSubscriber;

use Drupal\Core\State\StateInterface;
use Drupal\Console\Core\Style\DrupalStyle;
use Drupal\Console\Core\Utils\TranslatorManagerInterface;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Event\ConsoleEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Class MaintenanceModeListener
*
* @package Drupal\Console\Core\EventSubscriber
*/
class MaintenanceModeListener implements EventSubscriberInterface
{
/**
* @var TranslatorManagerInterface
*/
protected $translator;

/**
* @var StateInterface
*/
protected $state;

/**
* MaintenanceModeListener constructor.
*
* @param TranslatorManagerInterface $translator
* @param StateInterface $state
*/
public function __construct(
TranslatorManagerInterface $translator,
StateInterface $state
) {
$this->translator = $translator;
$this->state = $state;
}

/**
* Enable maintenance mode.
*
* @param ConsoleEvent $event
*/
public function enableMaintenanceMode(ConsoleEvent $event)
{
$this->switchMaintenanceMode($event, 'on');
}

/**
* Disable maintenance mode.
*
* @param ConsoleEvent $event
*/
public function disableMaintenanceMode(ConsoleEvent $event)
{
$this->switchMaintenanceMode($event, 'off');
}

/**
* Switch maintenance mode.
*
* @param ConsoleEvent $event
* @param string $mode
*/
public function switchMaintenanceMode(ConsoleEvent $event, $mode)
{
/* @var Command $command */
$command = $event->getCommand();

if ($command->isMaintenance()) {

/* @var DrupalStyle $io */
$io = new DrupalStyle($event->getInput(), $event->getOutput());
$stateName = 'system.maintenance_mode';
$modeMessage = null;

if ($mode == 'on') {
$this->state->set($stateName, true);
$modeMessage = $this->translator->trans('commands.site.maintenance.messages.maintenance-on');
}

if ($mode == 'off') {
$this->state->set($stateName, false);
$modeMessage = $this->translator->trans('commands.site.maintenance.messages.maintenance-off');
}

if ($modeMessage) {
$io->newLine();
$io->info($modeMessage, true);
$io->newLine();
}
}
}


/**
* @{@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
ConsoleEvents::COMMAND => 'enableMaintenanceMode',
ConsoleEvents::TERMINATE => 'disableMaintenanceMode',
];
}
}

0 comments on commit 8ee1af0

Please sign in to comment.