Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maintenance mode #326

Merged
merged 6 commits into from
Apr 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
];
}
}