class AddHTTPHeaders in HTTP Response Headers 8.2
Same name and namespace in other branches
- 8 src/EventSubscriber/AddHTTPHeaders.php \Drupal\http_response_headers\EventSubscriber\AddHTTPHeaders
- 2.0.x 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'
1 service uses AddHTTPHeaders
File
- src/
EventSubscriber/ AddHTTPHeaders.php, line 18 - Contains \Drupal\http_response_headers\EventSubscriber\AddHTTPHeaders.
Namespace
Drupal\http_response_headers\EventSubscriberView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AddHTTPHeaders:: |
protected | property | The entity storage manager for response_header entities. | |
AddHTTPHeaders:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
AddHTTPHeaders:: |
public | function | Sets extra HTTP headers. | |
AddHTTPHeaders:: |
public | function | Constructs a new Google Tag response subscriber. |