AddStaleHeaders.php in Fastly 8.3
File
src/EventSubscriber/AddStaleHeaders.php
View source
<?php
namespace Drupal\fastly\EventSubscriber;
use Drupal\Core\Config\ConfigFactoryInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AddStaleHeaders implements EventSubscriberInterface {
protected $logger;
protected $config;
public function __construct(LoggerInterface $logger, ConfigFactoryInterface $config_factory) {
$this->logger = $logger;
$this->config = $config_factory;
}
public function onRespond(FilterResponseEvent $event) {
$config = $this->config
->get('fastly.settings');
if (!$event
->isMasterRequest()) {
return;
}
$response = $event
->getResponse();
$cache_control_header = $response->headers
->get('Cache-Control');
$surrogate_control_header = $cache_control_header;
if ((bool) $config
->get('stale_while_revalidate')) {
$surrogate_control_header = $surrogate_control_header . ', stale-while-revalidate=' . $config
->get('stale_while_revalidate_value');
}
if ((bool) $config
->get('stale_if_error')) {
$surrogate_control_header .= ', stale-if-error=' . $config
->get('stale_if_error_value');
}
$response->headers
->set('Surrogate-Control', $surrogate_control_header);
$response->headers
->set('Fastly-Drupal-HTML', "YES");
}
public static function getSubscribedEvents() {
$events[KernelEvents::RESPONSE][] = [
'onRespond',
];
return $events;
}
}