Adding a ResponseSubscriber for taking care of end of request tasks, including caching of pages and system paths

8.0.x
Katherine Bailey 2012-04-15 08:55:59 -07:00 committed by Larry Garfield
parent 71146f2397
commit b99d11fac6
3 changed files with 71 additions and 2 deletions

View File

@ -5242,7 +5242,7 @@ function _drupal_bootstrap_full() {
*
* @see drupal_page_header()
*/
function drupal_page_set_cache() {
function drupal_page_set_cache($response_body) {
global $base_root;
if (drupal_page_is_cacheable()) {
@ -5250,7 +5250,7 @@ function drupal_page_set_cache() {
'cid' => $base_root . request_uri(),
'data' => array(
'path' => $_GET['q'],
'body' => ob_get_clean(),
'body' => $response_body,
'title' => drupal_get_title(),
'headers' => array(),
),

View File

@ -24,6 +24,7 @@ use Drupal\Core\EventSubscriber\AccessSubscriber;
use Drupal\Core\EventSubscriber\PathSubscriber;
use Drupal\Core\EventSubscriber\LegacyControllerSubscriber;
use Drupal\Core\EventSubscriber\MaintenanceModeSubscriber;
use Drupal\Core\EventSubscriber\ResponseSubscriber;
use Exception;
@ -74,6 +75,7 @@ class DrupalKernel extends HttpKernel {
$this->dispatcher->addSubscriber(new MaintenanceModeSubscriber());
$this->dispatcher->addSubscriber(new PathSubscriber());
$this->dispatcher->addSubscriber(new LegacyControllerSubscriber());
$this->dispatcher->addSubscriber(new ResponseSubscriber());
// Some other form of error occured that wasn't handled by another kernel
// listener. That could mean that it's a method/mime-type/error

View File

@ -0,0 +1,67 @@
<?php
namespace Drupal\Core\EventSubscriber;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* @file
*
* Definition of Drupal\Core\EventSubscriber\ResponseSubscriber;
*/
/**
* Subscriber for all responses.
*/
class ResponseSubscriber implements EventSubscriberInterface {
/**
* Performs end of request tasks.
*
* @todo The body of this function has just been copied almost verbatim from
* drupal_page_footer(), with the exception of now passing the response
* content to drupal_page_set_cache(). There's probably a lot in here that
* needs to get removed/changed.
*
* @param FilterResponseEvent $event
* The Event to process.
*/
public function onKernelResponse(FilterResponseEvent $event) {
global $user;
module_invoke_all('exit');
// Commit the user session, if needed.
drupal_session_commit();
$response = $event->getResponse();
$config = config('system.performance');
if ($config->get('cache') && ($cache = drupal_page_set_cache($response->getContent()))) {
drupal_serve_page_from_cache($cache);
}
else {
ob_flush();
}
_registry_check_code(REGISTRY_WRITE_LOOKUP_CACHE);
drupal_cache_system_paths();
module_implements_write_cache();
system_run_automated_cron();
}
/**
* Registers the methods in this class that should be listeners.
*
* @return array
* An array of event listener definitions.
*/
static function getSubscribedEvents() {
$events[KernelEvents::RESPONSE][] = array('onKernelResponse');
return $events;
}
}