ApeSubscriber.php in Advanced Page Expiration 8
File
src/EventSubscriber/ApeSubscriber.php
View source
<?php
namespace Drupal\ape\EventSubscriber;
use Drupal\Component\Plugin\Factory\FactoryInterface;
use Drupal\Core\Cache\CacheableResponseInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\PageCache\RequestPolicyInterface;
use Drupal\Core\PageCache\ResponsePolicyInterface;
class ApeSubscriber implements EventSubscriberInterface {
protected $configApe;
protected $configSystem;
protected $requestPolicy;
protected $responsePolicy;
protected $conditionManager;
public function __construct(ConfigFactoryInterface $config_factory, RequestPolicyInterface $request_policy, ResponsePolicyInterface $response_policy, FactoryInterface $plugin_factory) {
$this->configApe = $config_factory
->get('ape.settings');
$this->configSystem = $config_factory
->get('system.performance');
$this->requestPolicy = $request_policy;
$this->responsePolicy = $response_policy;
$this->conditionManager = $plugin_factory;
}
public function onRespond(FilterResponseEvent $event) {
if (!$event
->isMasterRequest()) {
return;
}
$response = $event
->getResponse();
if (!$response instanceof CacheableResponseInterface) {
return;
}
$maxAge = ape_cache_set();
if (is_null($maxAge)) {
$condition = $this->conditionManager
->createInstance('request_path');
$condition
->setConfig('pages', $this->configApe
->get('alternatives'));
if (!empty($this->configApe
->get('alternatives')) && $condition
->evaluate()) {
$maxAge = $this->configApe
->get('lifetime.alternatives');
}
else {
$maxAge = $this->configSystem
->get('cache.page.max_age');
}
}
switch ($response
->getStatusCode()) {
case 301:
$maxAge = $this->configApe
->get('lifetime.301');
break;
case 302:
$maxAge = $this->configApe
->get('lifetime.302');
break;
case 403:
$maxAge = 0;
break;
case 404:
$maxAge = $this->configApe
->get('lifetime.404');
break;
}
$originalMaxAge = $maxAge;
\Drupal::moduleHandler()
->alter('ape_cache', $maxAge, $originalMaxAge);
$this
->setCacheHeader($event, $maxAge);
}
private function checkCacheable(FilterResponseEvent $event, $maxAge) {
$request = $event
->getRequest();
$response = $event
->getResponse();
$isCacheable = $this->requestPolicy
->check($request) === RequestPolicyInterface::ALLOW && $this->responsePolicy
->check($response, $request) !== ResponsePolicyInterface::DENY;
return $isCacheable && $maxAge > 0 ? TRUE : FALSE;
}
private function setCacheHeader(FilterResponseEvent $event, $maxAge) {
$response = $event
->getResponse();
$value = 'no-cache, must-revalidate';
if ($this
->checkCacheable($event, $maxAge)) {
$value = 'public, max-age=' . $maxAge;
}
$response->headers
->set('Cache-Control', $value);
}
public static function getSubscribedEvents() {
$events[KernelEvents::RESPONSE][] = array(
'onRespond',
-1024,
);
return $events;
}
}
Classes
Name |
Description |
ApeSubscriber |
Alter Cache-control header based on configuration of ape. |