Refactor DrupalKernel to extend HttpKernel.

8.0.x
Larry Garfield 2012-03-23 12:49:37 -05:00
parent d64072f2b2
commit 0876971e2c
2 changed files with 53 additions and 36 deletions

View File

@ -5,15 +5,14 @@ namespace Drupal\Core;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\EventListener\RouterListener;
use Symfony\Component\HttpKernel\EventListener\ExceptionListener;
use Drupal\Core\EventSubscriber\HtmlSubscriber;
use Drupal\Core\EventSubscriber\JsonSubscriber;
use Drupal\Core\EventSubscriber\AccessSubscriber;
@ -31,7 +30,45 @@ use Exception;
/**
* The DrupalApp class is the core of Drupal itself.
*/
class DrupalKernel implements HttpKernelInterface {
class DrupalKernel extends HttpKernel {
protected $dispatcher;
protected $resolver;
/**
* Constructor
*
* @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
* @param ControllerResolverInterface $resolver A ControllerResolverInterface instance
*
* @api
*/
public function __construct(EventDispatcherInterface $dispatcher, ControllerResolverInterface $resolver) {
parent::__construct($dispatcher, $resolver);
$this->dispatcher = $dispatcher;
$this->resolver = $resolver;
// @todo Make this extensible rather than just hard coding some.
// @todo Add a subscriber to handle other things, too, like our Ajax
// replacement system.
$this->dispatcher->addSubscriber(new HtmlSubscriber());
$this->dispatcher->addSubscriber(new JsonSubscriber());
$this->dispatcher->addSubscriber(new AccessSubscriber());
$this->dispatcher->addSubscriber(new PathSubscriber());
$this->dispatcher->addSubscriber(new LegacyControllerSubscriber());
// 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
// combination that is not accounted for, or some other type of error.
// Either way, treat it as a server-level error and return an HTTP 500.
// By default, this will be an HTML-type response because that's a decent
// best guess if we don't know otherwise.
$this->dispatcher->addSubscriber(new ExceptionListener(function(Exception $e) {
return new Response('A fatal error occurred: ' . $e->getMessage(), 500);
}));
}
/**
*
@ -41,32 +78,11 @@ class DrupalKernel implements HttpKernelInterface {
* The response object to return to the requesting user agent.
*/
function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true) {
try {
$dispatcher = $this->getDispatcher();
if ($type == self::MASTER_REQUEST) {
$matcher = $this->getMatcher($request);
$dispatcher->addSubscriber(new RouterListener($matcher));
$dispatcher->addSubscriber(new AccessSubscriber());
$dispatcher->addSubscriber(new PathSubscriber());
$dispatcher->addSubscriber(new LegacyControllerSubscriber());
$resolver = new ControllerResolver();
$kernel = new HttpKernel($dispatcher, $resolver);
$response = $kernel->handle($request);
$this->dispatcher->addSubscriber(new RouterListener($matcher));
}
catch (Exception $e) {
// 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
// combination that is not accounted for, or some other type of error.
// Either way, treat it as a server-level error and return an HTTP 500.
// By default, this will be an HTML-type response because that's a decent
// best guess if we don't know otherwise.
$response = new Response('A fatal error occurred: ' . $e->getMessage(), 500);
}
return $response;
return parent::handle($request, $type, $catch);
}
/**
@ -80,13 +96,7 @@ class DrupalKernel implements HttpKernelInterface {
* @return EventDispatcher
*/
protected function getDispatcher() {
$dispatcher = new EventDispatcher();
// @todo Make this extensible rather than just hard coding some.
// @todo Add a subscriber to handle other things, too, like our Ajax
// replacement system.
$dispatcher->addSubscriber(new HtmlSubscriber());
$dispatcher->addSubscriber(new JsonSubscriber());
return $dispatcher;
}

View File

@ -2,6 +2,9 @@
use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
/**
* @file
@ -26,5 +29,9 @@ drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
// A request object from the HTTPFoundation to tell us about the request.
$request = Request::createFromGlobals();
$kernel = new DrupalKernel();
$dispatcher = new EventDispatcher();
$resolver = new ControllerResolver();
$kernel = new DrupalKernel($dispatcher, $resolver);
$kernel->handle($request)->send();