ba82988a14d17b8d0b2fdcdf752c27d3aa4fe009
[yaffs-website] / web / core / lib / Drupal / Core / EventSubscriber / MaintenanceModeSubscriber.php
1 <?php
2
3 namespace Drupal\Core\EventSubscriber;
4
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;
18
19 /**
20  * Maintenance mode subscriber for controller requests.
21  */
22 class MaintenanceModeSubscriber implements EventSubscriberInterface {
23
24   use StringTranslationTrait;
25
26   /**
27    * The maintenance mode.
28    *
29    * @var \Drupal\Core\Site\MaintenanceModeInterface
30    */
31   protected $maintenanceMode;
32
33   /**
34    * The current account.
35    *
36    * @var \Drupal\Core\Session\AccountInterface
37    */
38   protected $account;
39
40   /**
41    * The config factory.
42    *
43    * @var \Drupal\Core\Config\ConfigFactoryInterface
44    */
45   protected $config;
46
47   /**
48    * The url generator.
49    *
50    * @var \Drupal\Core\Routing\UrlGeneratorInterface
51    */
52   protected $urlGenerator;
53
54   /**
55    * The bare HTML page renderer.
56    *
57    * @var \Drupal\Core\Render\BareHtmlPageRendererInterface
58    */
59   protected $bareHtmlPageRenderer;
60
61   /**
62    * Constructs a new MaintenanceModeSubscriber.
63    *
64    * @param \Drupal\Core\Site\MaintenanceModeInterface $maintenance_mode
65    *   The maintenance mode.
66    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
67    *   The config factory.
68    * @param \Drupal\Core\StringTranslation\TranslationInterface $translation
69    *   The string translation.
70    * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
71    *   The url generator.
72    * @param \Drupal\Core\Session\AccountInterface $account
73    *   The current user.
74    * @param \Drupal\Core\Render\BareHtmlPageRendererInterface $bare_html_page_renderer
75    *   The bare HTML page renderer.
76    */
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;
84   }
85
86   /**
87    * Returns the site maintenance page if the site is offline.
88    *
89    * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
90    *   The event to process.
91    */
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();
98
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.
102
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);
108           return;
109         }
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);
114       }
115       else {
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
118         // settings page.
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);
122           }
123           else {
124             $this->drupalSetMessage($this->t('Operating in maintenance mode.'), 'status', FALSE);
125           }
126         }
127       }
128     }
129   }
130
131   /**
132    * Gets the site maintenance message.
133    *
134    * @return \Drupal\Component\Render\MarkupInterface
135    *   The formatted site maintenance message.
136    */
137   protected function getSiteMaintenanceMessage() {
138     return SafeMarkup::format($this->config->get('system.maintenance')->get('message'), [
139       '@site' => $this->config->get('system.site')->get('name'),
140     ]);
141   }
142
143   /**
144    * Wraps the drupal_set_message function.
145    */
146   protected function drupalSetMessage($message = NULL, $type = 'status', $repeat = FALSE) {
147     return drupal_set_message($message, $type, $repeat);
148   }
149
150   /**
151    * {@inheritdoc}
152    */
153   public static function getSubscribedEvents() {
154     $events[KernelEvents::REQUEST][] = ['onKernelRequestMaintenance', 30];
155     $events[KernelEvents::EXCEPTION][] = ['onKernelRequestMaintenance'];
156     return $events;
157   }
158
159 }