You are here

class AddHTTPHeaders in HTTP Response Headers 2.0.x

Same name and namespace in other branches
  1. 8.2 src/EventSubscriber/AddHTTPHeaders.php \Drupal\http_response_headers\EventSubscriber\AddHTTPHeaders
  2. 8 src/EventSubscriber/AddHTTPHeaders.php \Drupal\http_response_headers\EventSubscriber\AddHTTPHeaders

Provides AddHTTPHeaders.

Hierarchy

  • class \Drupal\http_response_headers\EventSubscriber\AddHTTPHeaders implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of AddHTTPHeaders

1 string reference to 'AddHTTPHeaders'
http_response_headers.services.yml in ./http_response_headers.services.yml
http_response_headers.services.yml
1 service uses AddHTTPHeaders
http_response_headers in ./http_response_headers.services.yml
\Drupal\http_response_headers\EventSubscriber\AddHTTPHeaders

File

src/EventSubscriber/AddHTTPHeaders.php, line 18
Contains \Drupal\http_response_headers\EventSubscriber\AddHTTPHeaders.

Namespace

Drupal\http_response_headers\EventSubscriber
View source
class AddHTTPHeaders implements EventSubscriberInterface {

  /**
   * The entity storage manager for response_header entities.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $entityManager;

  /**
   * Constructs a new Google Tag response subscriber.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The config factory service.
   */
  public function __construct(ConfigFactoryInterface $configFactory) {
    $this->entityManager = \Drupal::entityTypeManager()
      ->getStorage('response_header');
  }

  /**
   * Sets extra HTTP headers.
   */
  public function onRespond(FilterResponseEvent $event) {
    if (!$event
      ->isMasterRequest()) {
      return;
    }
    $response = $event
      ->getResponse();
    $headers = $this->entityManager
      ->loadMultiple();
    if (!empty($headers)) {
      foreach ($headers as $key => $header) {
        if (!empty($header
          ->get('name'))) {

          // @TODO Add context rules to header groups to allow
          // certain groups to only be applied in certain contexts.
          if (!empty($header
            ->get('value'))) {

            // Must remove the existing header if settings a new value.
            if ($response->headers
              ->has($header
              ->get('name'))) {
              $response->headers
                ->remove($header
                ->get('name'));
            }
            $response->headers
              ->set($header
              ->get('name'), $header
              ->get('value'));
          }
          else {
            $response->headers
              ->remove($header
              ->get('name'));
          }
        }
      }
    }
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
AddHTTPHeaders::$entityManager protected property The entity storage manager for response_header entities.
AddHTTPHeaders::getSubscribedEvents public static function
AddHTTPHeaders::onRespond public function Sets extra HTTP headers.
AddHTTPHeaders::__construct public function Constructs a new Google Tag response subscriber.