diff options
author | Larry Garfield <larry@garfieldtech.com> | 2012-03-23 12:49:37 -0500 |
---|---|---|
committer | Larry Garfield <larry@garfieldtech.com> | 2012-03-23 12:49:37 -0500 |
commit | 0876971e2cda739d0066dd22efbe43bcdd525f4e (patch) | |
tree | 4946a80f3c70a4214698dfb24df6404ed522a502 | |
parent | d64072f2b2e6be3a9d10cb0bd77cda8313ad91ef (diff) | |
download | drupal-0876971e2cda739d0066dd22efbe43bcdd525f4e.tar.gz drupal-0876971e2cda739d0066dd22efbe43bcdd525f4e.zip |
Refactor DrupalKernel to extend HttpKernel.
-rw-r--r-- | core/lib/Drupal/Core/DrupalKernel.php | 80 | ||||
-rw-r--r-- | index.php | 9 |
2 files changed, 53 insertions, 36 deletions
diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php index 32ba880ca14..bb0737a853c 100644 --- a/core/lib/Drupal/Core/DrupalKernel.php +++ b/core/lib/Drupal/Core/DrupalKernel.php @@ -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; } diff --git a/index.php b/index.php index 7609b3ea34c..62d162e4684 100644 --- a/index.php +++ b/index.php @@ -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(); |