Skip to content

Commit 8ee1af0

Browse files
LOBsTerrjmolivas
authored andcommitted
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
1 parent e93efb2 commit 8ee1af0

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

src/Application.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Drupal\Console\Core\EventSubscriber\ShowGeneratedFilesListener;
2121
use Drupal\Console\Core\EventSubscriber\ShowGenerateInlineListener;
2222
use Drupal\Console\Core\EventSubscriber\CallCommandListener;
23+
use Drupal\Console\Core\EventSubscriber\MaintenanceModeListener;
2324
use Drupal\Console\Core\Utils\ConfigurationManager;
2425
use Drupal\Console\Core\Style\DrupalStyle;
2526
use Drupal\Console\Core\Utils\ChainDiscovery;
@@ -310,6 +311,13 @@ private function registerEvents()
310311
)
311312
);
312313

314+
$dispatcher->addSubscriber(
315+
new MaintenanceModeListener(
316+
$this->container->get('console.translator_manager'),
317+
$this->container->get('state')
318+
)
319+
);
320+
313321
$this->setDispatcher($dispatcher);
314322
$this->eventRegistered = true;
315323
}

src/Command/Command.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ abstract class Command extends BaseCommand
3333
*/
3434
private $io;
3535

36+
/**
37+
* @var bool
38+
*/
39+
private $maintenance = false;
40+
3641
/**
3742
* {@inheritdoc}
3843
*/
@@ -49,6 +54,34 @@ public function getIo()
4954
return $this->io;
5055
}
5156

57+
/**
58+
* Check maintenance mode.
59+
*
60+
* @return bool
61+
*/
62+
public function isMaintenance()
63+
{
64+
return $this->maintenance;
65+
}
66+
67+
/**
68+
* Enable maintenance mode.
69+
*
70+
* @return $this
71+
* Command.
72+
*/
73+
public function enableMaintenance()
74+
{
75+
$this->maintenance = true;
76+
return $this;
77+
}
78+
79+
/**
80+
* Create Exception
81+
*
82+
* @return void
83+
*
84+
*/
5285
public function createException($message) {
5386
$this->getIo()->error($message);
5487
exit(1);
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \Drupal\Console\Core\EventSubscriber\MaintenanceModeListener.
6+
*/
7+
8+
namespace Drupal\Console\Core\EventSubscriber;
9+
10+
use Drupal\Core\State\StateInterface;
11+
use Drupal\Console\Core\Style\DrupalStyle;
12+
use Drupal\Console\Core\Utils\TranslatorManagerInterface;
13+
use Symfony\Component\Console\ConsoleEvents;
14+
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Event\ConsoleEvent;
16+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17+
18+
/**
19+
* Class MaintenanceModeListener
20+
*
21+
* @package Drupal\Console\Core\EventSubscriber
22+
*/
23+
class MaintenanceModeListener implements EventSubscriberInterface
24+
{
25+
/**
26+
* @var TranslatorManagerInterface
27+
*/
28+
protected $translator;
29+
30+
/**
31+
* @var StateInterface
32+
*/
33+
protected $state;
34+
35+
/**
36+
* MaintenanceModeListener constructor.
37+
*
38+
* @param TranslatorManagerInterface $translator
39+
* @param StateInterface $state
40+
*/
41+
public function __construct(
42+
TranslatorManagerInterface $translator,
43+
StateInterface $state
44+
) {
45+
$this->translator = $translator;
46+
$this->state = $state;
47+
}
48+
49+
/**
50+
* Enable maintenance mode.
51+
*
52+
* @param ConsoleEvent $event
53+
*/
54+
public function enableMaintenanceMode(ConsoleEvent $event)
55+
{
56+
$this->switchMaintenanceMode($event, 'on');
57+
}
58+
59+
/**
60+
* Disable maintenance mode.
61+
*
62+
* @param ConsoleEvent $event
63+
*/
64+
public function disableMaintenanceMode(ConsoleEvent $event)
65+
{
66+
$this->switchMaintenanceMode($event, 'off');
67+
}
68+
69+
/**
70+
* Switch maintenance mode.
71+
*
72+
* @param ConsoleEvent $event
73+
* @param string $mode
74+
*/
75+
public function switchMaintenanceMode(ConsoleEvent $event, $mode)
76+
{
77+
/* @var Command $command */
78+
$command = $event->getCommand();
79+
80+
if ($command->isMaintenance()) {
81+
82+
/* @var DrupalStyle $io */
83+
$io = new DrupalStyle($event->getInput(), $event->getOutput());
84+
$stateName = 'system.maintenance_mode';
85+
$modeMessage = null;
86+
87+
if ($mode == 'on') {
88+
$this->state->set($stateName, true);
89+
$modeMessage = $this->translator->trans('commands.site.maintenance.messages.maintenance-on');
90+
}
91+
92+
if ($mode == 'off') {
93+
$this->state->set($stateName, false);
94+
$modeMessage = $this->translator->trans('commands.site.maintenance.messages.maintenance-off');
95+
}
96+
97+
if ($modeMessage) {
98+
$io->newLine();
99+
$io->info($modeMessage, true);
100+
$io->newLine();
101+
}
102+
}
103+
}
104+
105+
106+
/**
107+
* @{@inheritdoc}
108+
*/
109+
public static function getSubscribedEvents()
110+
{
111+
return [
112+
ConsoleEvents::COMMAND => 'enableMaintenanceMode',
113+
ConsoleEvents::TERMINATE => 'disableMaintenanceMode',
114+
];
115+
}
116+
}

0 commit comments

Comments
 (0)