class BoostResponseSubscriber in Boost 8
Class BoostResponseSubscriber.
Hierarchy
- class \Drupal\boost\EventSubscriber\BoostResponseSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of BoostResponseSubscriber
1 string reference to 'BoostResponseSubscriber'
1 service uses BoostResponseSubscriber
File
- src/
EventSubscriber/ BoostResponseSubscriber.php, line 23 - Contains Drupal\boost\EventSubscriber\BoostResponseSubscriber
Namespace
Drupal\boost\EventSubscriberView source
class BoostResponseSubscriber implements EventSubscriberInterface {
/**
* The HTML response attachments processor service.
*
* @var \Drupal\Core\Render\AttachmentsResponseProcessorInterface
*/
protected $htmlResponseAttachmentsProcessor;
/**
* The current account.
*
* @var \Drupal\Core\Session\AccountProxy
*/
protected $account;
/**
* Constructs a HtmlResponseSubscriber object.
*
* @param \Drupal\Core\Render\AttachmentsResponseProcessorInterface $html_response_attachments_processor
* The HTML response attachments processor service.
* @param \Drupal\Core\Session\AccountProxy $account
*/
public function __construct(AttachmentsResponseProcessorInterface $html_response_attachments_processor, AccountProxy $account) {
$this->boostCache = new BoostCache();
$this->htmlResponseAttachmentsProcessor = $html_response_attachments_processor;
$this->account = $account;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return array(
KernelEvents::REQUEST => array(
array(
'onRequest',
100,
),
),
KernelEvents::RESPONSE => array(
array(
'onRespond',
-100,
),
),
);
}
/**
* Process a GetResponseEvent instance.
*/
public function onRequest(GetResponseEvent $event) {
if ($this->account
->isAuthenticated()) {
return;
}
// Invalidate on POST to rebuild page response.
$method = $event
->getRequest()
->getMethod();
if ($method == 'POST') {
$this->boostCache
->delete();
}
// Only cache GET requests.
if ($method != 'GET') {
return;
}
// Check for existing cached response.
if ($content = $this->boostCache
->retrieve()) {
$response = new Response();
$response
->setContent($content);
$response
->setStatusCode(Response::HTTP_OK);
$response->headers
->set('X-Boost-Cache', 'partial');
$event
->setResponse($response);
}
}
/**
* Processes HtmlResponse event.
*
* @todo, split things off into the class.
*
* @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
* The event to process.
*/
public function onRespond(FilterResponseEvent $event) {
if ($this->account
->isAuthenticated()) {
return;
}
$response = $event
->getResponse();
if (!$response instanceof HtmlResponse) {
return;
}
if ($response
->isRedirect() || $response
->isForbidden() || $response
->isNotFound()) {
return;
}
// Process response attachments.
$this->htmlResponseAttachmentsProcessor
->processAttachments($response);
$content = $response
->getContent();
// Create a cached response on the local file system.
$this->boostCache
->index($content);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BoostResponseSubscriber:: |
protected | property | The current account. | |
BoostResponseSubscriber:: |
protected | property | The HTML response attachments processor service. | |
BoostResponseSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
BoostResponseSubscriber:: |
public | function | Process a GetResponseEvent instance. | |
BoostResponseSubscriber:: |
public | function | Processes HtmlResponse event. | |
BoostResponseSubscriber:: |
public | function | Constructs a HtmlResponseSubscriber object. |