Skip to content
Permalink
Browse files

Maintenance mode (#326)

* 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 8ee1af0f338c9dea501773e8321e41a67ddd70a8
Showing with 157 additions and 0 deletions.
  1. +8 −0 src/Application.php
  2. +33 −0 src/Command/Command.php
  3. +116 −0 src/EventSubscriber/MaintenanceModeListener.php
@@ -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;
@@ -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;
}
@@ -33,6 +33,11 @@ abstract class Command extends BaseCommand
*/
private $io;
/**
* @var bool
*/
private $maintenance = false;
/**
* {@inheritdoc}
*/
@@ -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);
@@ -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.
You can’t perform that action at this time.