3 namespace Drupal\Core\EventSubscriber;
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\Render\BareHtmlPageRendererInterface;
8 use Drupal\Core\Routing\RouteMatch;
9 use Drupal\Core\Routing\UrlGeneratorInterface;
10 use Drupal\Core\Session\AccountInterface;
11 use Drupal\Core\Site\MaintenanceModeInterface;
12 use Drupal\Core\StringTranslation\StringTranslationTrait;
13 use Drupal\Core\StringTranslation\TranslationInterface;
14 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15 use Symfony\Component\HttpFoundation\Response;
16 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
17 use Symfony\Component\HttpKernel\KernelEvents;
20 * Maintenance mode subscriber for controller requests.
22 class MaintenanceModeSubscriber implements EventSubscriberInterface {
24 use StringTranslationTrait;
27 * The maintenance mode.
29 * @var \Drupal\Core\Site\MaintenanceModeInterface
31 protected $maintenanceMode;
34 * The current account.
36 * @var \Drupal\Core\Session\AccountInterface
43 * @var \Drupal\Core\Config\ConfigFactoryInterface
50 * @var \Drupal\Core\Routing\UrlGeneratorInterface
52 protected $urlGenerator;
55 * The bare HTML page renderer.
57 * @var \Drupal\Core\Render\BareHtmlPageRendererInterface
59 protected $bareHtmlPageRenderer;
62 * Constructs a new MaintenanceModeSubscriber.
64 * @param \Drupal\Core\Site\MaintenanceModeInterface $maintenance_mode
65 * The maintenance mode.
66 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
68 * @param \Drupal\Core\StringTranslation\TranslationInterface $translation
69 * The string translation.
70 * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
72 * @param \Drupal\Core\Session\AccountInterface $account
74 * @param \Drupal\Core\Render\BareHtmlPageRendererInterface $bare_html_page_renderer
75 * The bare HTML page renderer.
77 public function __construct(MaintenanceModeInterface $maintenance_mode, ConfigFactoryInterface $config_factory, TranslationInterface $translation, UrlGeneratorInterface $url_generator, AccountInterface $account, BareHtmlPageRendererInterface $bare_html_page_renderer) {
78 $this->maintenanceMode = $maintenance_mode;
79 $this->config = $config_factory;
80 $this->stringTranslation = $translation;
81 $this->urlGenerator = $url_generator;
82 $this->account = $account;
83 $this->bareHtmlPageRenderer = $bare_html_page_renderer;
87 * Returns the site maintenance page if the site is offline.
89 * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
90 * The event to process.
92 public function onKernelRequestMaintenance(GetResponseEvent $event) {
93 $request = $event->getRequest();
94 $route_match = RouteMatch::createFromRequest($request);
95 if ($this->maintenanceMode->applies($route_match)) {
96 // Don't cache maintenance mode pages.
97 \Drupal::service('page_cache_kill_switch')->trigger();
99 if (!$this->maintenanceMode->exempt($this->account)) {
100 // Deliver the 503 page if the site is in maintenance mode and the
101 // logged in user is not allowed to bypass it.
103 // If the request format is not 'html' then show default maintenance
104 // mode page else show a text/plain page with maintenance message.
105 if ($request->getRequestFormat() !== 'html') {
106 $response = new Response($this->getSiteMaintenanceMessage(), 503, ['Content-Type' => 'text/plain']);
107 $event->setResponse($response);
110 drupal_maintenance_theme();
111 $response = $this->bareHtmlPageRenderer->renderBarePage(['#markup' => $this->getSiteMaintenanceMessage()], $this->t('Site under maintenance'), 'maintenance_page');
112 $response->setStatusCode(503);
113 $event->setResponse($response);
116 // Display a message if the logged in user has access to the site in
117 // maintenance mode. However, suppress it on the maintenance mode
119 if ($route_match->getRouteName() != 'system.site_maintenance_mode') {
120 if ($this->account->hasPermission('administer site configuration')) {
121 $this->drupalSetMessage($this->t('Operating in maintenance mode. <a href=":url">Go online.</a>', [':url' => $this->urlGenerator->generate('system.site_maintenance_mode')]), 'status', FALSE);
124 $this->drupalSetMessage($this->t('Operating in maintenance mode.'), 'status', FALSE);
132 * Gets the site maintenance message.
134 * @return \Drupal\Component\Render\MarkupInterface
135 * The formatted site maintenance message.
137 protected function getSiteMaintenanceMessage() {
138 return SafeMarkup::format($this->config->get('system.maintenance')->get('message'), [
139 '@site' => $this->config->get('system.site')->get('name'),
144 * Wraps the drupal_set_message function.
146 protected function drupalSetMessage($message = NULL, $type = 'status', $repeat = FALSE) {
147 return drupal_set_message($message, $type, $repeat);
153 public static function getSubscribedEvents() {
154 $events[KernelEvents::REQUEST][] = ['onKernelRequestMaintenance', 30];
155 $events[KernelEvents::EXCEPTION][] = ['onKernelRequestMaintenance'];