View source
<?php
namespace Drupal\Core\EventSubscriber;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Render\BareHtmlPageRendererInterface;
use Drupal\Core\Routing\RouteMatch;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Site\MaintenanceModeEvents;
use Drupal\Core\Site\MaintenanceModeInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class MaintenanceModeSubscriber implements EventSubscriberInterface {
use StringTranslationTrait;
protected $maintenanceMode;
protected $account;
protected $config;
protected $urlGenerator;
protected $bareHtmlPageRenderer;
protected $messenger;
protected $eventDispatcher;
public function __construct(MaintenanceModeInterface $maintenance_mode, ConfigFactoryInterface $config_factory, TranslationInterface $translation, UrlGeneratorInterface $url_generator, AccountInterface $account, BareHtmlPageRendererInterface $bare_html_page_renderer, MessengerInterface $messenger, EventDispatcherInterface $event_dispatcher) {
$this->maintenanceMode = $maintenance_mode;
$this->config = $config_factory;
$this->stringTranslation = $translation;
$this->urlGenerator = $url_generator;
$this->account = $account;
$this->bareHtmlPageRenderer = $bare_html_page_renderer;
$this->messenger = $messenger;
$this->eventDispatcher = $event_dispatcher;
}
public function onKernelRequestMaintenance(RequestEvent $event) {
$request = $event
->getRequest();
$route_match = RouteMatch::createFromRequest($request);
if ($this->maintenanceMode
->applies($route_match)) {
\Drupal::service('page_cache_kill_switch')
->trigger();
if (!$this->maintenanceMode
->exempt($this->account)) {
$this->eventDispatcher
->dispatch($event, MaintenanceModeEvents::MAINTENANCE_MODE_REQUEST);
}
else {
if ($route_match
->getRouteName() != 'system.site_maintenance_mode') {
if ($this->account
->hasPermission('administer site configuration')) {
$this->messenger
->addMessage($this
->t('Operating in maintenance mode. <a href=":url">Go online.</a>', [
':url' => $this->urlGenerator
->generate('system.site_maintenance_mode'),
]), 'status', FALSE);
}
else {
$this->messenger
->addMessage($this
->t('Operating in maintenance mode.'), 'status', FALSE);
}
}
}
}
}
public function onMaintenanceModeRequest(RequestEvent $event) {
$request = $event
->getRequest();
if ($request
->getRequestFormat() !== 'html') {
$response = new Response($this->maintenanceMode
->getSiteMaintenanceMessage(), 503, [
'Content-Type' => 'text/plain',
]);
$event
->setResponse($response);
return;
}
drupal_maintenance_theme();
$response = $this->bareHtmlPageRenderer
->renderBarePage([
'#markup' => $this->maintenanceMode
->getSiteMaintenanceMessage(),
], $this
->t('Site under maintenance'), 'maintenance_page');
$response
->setStatusCode(503);
$event
->setResponse($response);
}
public static function getSubscribedEvents() : array {
$events[KernelEvents::REQUEST][] = [
'onKernelRequestMaintenance',
30,
];
$events[KernelEvents::EXCEPTION][] = [
'onKernelRequestMaintenance',
];
$events[MaintenanceModeEvents::MAINTENANCE_MODE_REQUEST][] = [
'onMaintenanceModeRequest',
-1000,
];
return $events;
}
}