class MaintenanceModeSubscriber

Same name in this branch

Maintenance mode subscriber for controller requests.

Hierarchy

Expanded class hierarchy of MaintenanceModeSubscriber

1 string reference to 'MaintenanceModeSubscriber'
core.services.yml in drupal/core/core.services.yml
drupal/core/core.services.yml

File

drupal/core/lib/Drupal/Core/EventSubscriber/MaintenanceModeSubscriber.php, line 19
Contains \Drupal\Core\EventSubscriber\MaintenanceModeSubscriber.

Namespace

Drupal\Core\EventSubscriber
View source
class MaintenanceModeSubscriber implements EventSubscriberInterface {

  /**
   * Determine whether the page is configured to be offline.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   The Event to process.
   */
  public function onKernelRequestDetermineSiteStatus(GetResponseEvent $event) {

    // Check if the site is offline.
    $request = $event
      ->getRequest();
    $is_offline = _menu_site_is_offline() ? MENU_SITE_OFFLINE : MENU_SITE_ONLINE;
    $request->attributes
      ->set('_maintenance', $is_offline);
  }

  /**
   * Returns the site maintenance page if the site is offline.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   The event to process.
   */
  public function onKernelRequestMaintenance(GetResponseEvent $event) {
    $request = $event
      ->getRequest();
    $response = $event
      ->getResponse();

    // Continue if the site is online and the response is not a redirection.
    if ($request->attributes
      ->get('_maintenance') != MENU_SITE_ONLINE && !$response instanceof RedirectResponse) {

      // Deliver the 503 page.
      drupal_maintenance_theme();
      drupal_set_title(t('Site under maintenance'));
      $content = theme('maintenance_page', array(
        'content' => filter_xss_admin(t(config('system.maintenance')
          ->get('message'), array(
          '@site' => config('system.site')
            ->get('name'),
        ))),
      ));
      $response = new Response('Service unavailable', 503);
      $response
        ->setContent($content);
      $event
        ->setResponse($response);
    }
  }

  /**
   * {@inheritdoc}
   */
  static function getSubscribedEvents() {

    // In order to change the maintenance status an event subscriber with a
    // priority between 30 and 40 should be added.
    $events[KernelEvents::REQUEST][] = array(
      'onKernelRequestDetermineSiteStatus',
      40,
    );
    $events[KernelEvents::REQUEST][] = array(
      'onKernelRequestMaintenance',
      30,
    );
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MaintenanceModeSubscriber::getSubscribedEvents static function Returns an array of event names this subscriber wants to listen to. Overrides EventSubscriberInterface::getSubscribedEvents
MaintenanceModeSubscriber::onKernelRequestDetermineSiteStatus public function Determine whether the page is configured to be offline.
MaintenanceModeSubscriber::onKernelRequestMaintenance public function Returns the site maintenance page if the site is offline.