You are here

class AddHttpHeaders in Access unpublished 8

Add new HTTP headers as added in settings form.

Hierarchy

  • class \Drupal\access_unpublished\EventSubscriber\AddHttpHeaders implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of AddHttpHeaders

1 string reference to 'AddHttpHeaders'
access_unpublished.services.yml in ./access_unpublished.services.yml
access_unpublished.services.yml
1 service uses AddHttpHeaders
access_unpublished.add_http_header in ./access_unpublished.services.yml
Drupal\access_unpublished\EventSubscriber\AddHttpHeaders

File

src/EventSubscriber/AddHttpHeaders.php, line 14

Namespace

Drupal\access_unpublished\EventSubscriber
View source
class AddHttpHeaders implements EventSubscriberInterface {

  /**
   * Access Unpublished custom configurations.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $config;

  /**
   * The token getter service.
   *
   * @var \Drupal\access_unpublished\TokenGetter
   */
  protected $tokenGetter;

  /**
   * Constructs a new response subscriber.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The config factory service.
   * @param \Drupal\access_unpublished\TokenGetter $tokenGetter
   *   The token getter service.
   */
  public function __construct(ConfigFactoryInterface $configFactory, TokenGetter $tokenGetter) {
    $this->config = $configFactory
      ->get('access_unpublished.settings');
    $this->tokenGetter = $tokenGetter;
  }

  /**
   * Set HTTP headers configured on admin/config/content/access_unpublished.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   Event.
   */
  public function onResponse(FilterResponseEvent $event) {
    if (!$event
      ->isMasterRequest()) {
      return;
    }
    if ($this->tokenGetter
      ->getToken()) {
      foreach ($this->config
        ->get('modify_http_headers') as $key => $header) {

        // Must remove the existing header if settings a new value.
        if ($event
          ->getResponse()->headers
          ->has($key)) {
          $event
            ->getResponse()->headers
            ->remove($key);
        }
        $event
          ->getResponse()->headers
          ->set($key, $header);
      }
    }
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
AddHttpHeaders::$config protected property Access Unpublished custom configurations.
AddHttpHeaders::$tokenGetter protected property The token getter service.
AddHttpHeaders::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
AddHttpHeaders::onResponse public function Set HTTP headers configured on admin/config/content/access_unpublished.
AddHttpHeaders::__construct public function Constructs a new response subscriber.