You are here

AcsfMaintenanceModeSubscriber.php in Acquia Cloud Site Factory Connector 8

Same filename and directory in other branches
  1. 8.2 src/EventSubscriber/AcsfMaintenanceModeSubscriber.php

File

src/EventSubscriber/AcsfMaintenanceModeSubscriber.php
View source
<?php

namespace Drupal\acsf\EventSubscriber;

use Drupal\Core\State\StateInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
 * Response subscriber to add SF maintenance mode header.
 */
class AcsfMaintenanceModeSubscriber implements EventSubscriberInterface {

  /**
   * Name of ACSF maintenance mode's response header.
   */
  const HEADER = 'X-SF-Maintenance';

  /**
   * The state.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * Constructs a new AcsfMaintenanceModeSubscriber.
   *
   * @param \Drupal\Core\State\StateInterface $state
   *   The state.
   */
  public function __construct(StateInterface $state) {
    $this->state = $state;
  }

  /**
   * Sets SF maintenance mode header (on successful responses).
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   The event to process.
   */
  public function onRespond(FilterResponseEvent $event) {
    if (!$event
      ->isMasterRequest()) {
      return;
    }
    if ($this->state
      ->get('system.maintenance_mode')) {
      $response = $event
        ->getResponse();
      $response->headers
        ->set(self::HEADER, 'enabled');
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::RESPONSE][] = [
      'onRespond',
    ];
    return $events;
  }

}

Classes

Namesort descending Description
AcsfMaintenanceModeSubscriber Response subscriber to add SF maintenance mode header.