MaintenanceModeSubscriber.php in Zircon Profile 8.0
File
core/lib/Drupal/Core/EventSubscriber/MaintenanceModeSubscriber.php
View source
<?php
namespace Drupal\Core\EventSubscriber;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Render\BareHtmlPageRendererInterface;
use Drupal\Core\Routing\RouteMatch;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Site\MaintenanceModeInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class MaintenanceModeSubscriber implements EventSubscriberInterface {
use StringTranslationTrait;
protected $maintenanceMode;
protected $account;
protected $config;
protected $urlGenerator;
protected $bareHtmlPageRenderer;
public function __construct(MaintenanceModeInterface $maintenance_mode, ConfigFactoryInterface $config_factory, TranslationInterface $translation, UrlGeneratorInterface $url_generator, AccountInterface $account, BareHtmlPageRendererInterface $bare_html_page_renderer) {
$this->maintenanceMode = $maintenance_mode;
$this->config = $config_factory;
$this->stringTranslation = $translation;
$this->urlGenerator = $url_generator;
$this->account = $account;
$this->bareHtmlPageRenderer = $bare_html_page_renderer;
}
public function onKernelRequestMaintenance(GetResponseEvent $event) {
$route_match = RouteMatch::createFromRequest($event
->getRequest());
if ($this->maintenanceMode
->applies($route_match)) {
\Drupal::service('page_cache_kill_switch')
->trigger();
if (!$this->maintenanceMode
->exempt($this->account)) {
drupal_maintenance_theme();
$content = Xss::filterAdmin(SafeMarkup::format($this->config
->get('system.maintenance')
->get('message'), array(
'@site' => $this->config
->get('system.site')
->get('name'),
)));
$response = $this->bareHtmlPageRenderer
->renderBarePage([
'#markup' => $content,
], $this
->t('Site under maintenance'), 'maintenance_page');
$response
->setStatusCode(503);
$event
->setResponse($response);
}
else {
if ($route_match
->getRouteName() != 'system.site_maintenance_mode') {
if ($this->account
->hasPermission('administer site configuration')) {
$this
->drupalSetMessage($this
->t('Operating in maintenance mode. <a href=":url">Go online.</a>', array(
':url' => $this->urlGenerator
->generate('system.site_maintenance_mode'),
)), 'status', FALSE);
}
else {
$this
->drupalSetMessage($this
->t('Operating in maintenance mode.'), 'status', FALSE);
}
}
}
}
}
protected function drupalSetMessage($message = NULL, $type = 'status', $repeat = FALSE) {
return drupal_set_message($message, $type, $repeat);
}
public static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = array(
'onKernelRequestMaintenance',
30,
);
$events[KernelEvents::EXCEPTION][] = array(
'onKernelRequestMaintenance',
);
return $events;
}
}