Issue #1595146 by Niklas Fiekas, cosmicdreams, Rob Loach, effulgentsia: Added Load the HttpKernel from the DI Container.

8.0.x
webchick 2012-07-05 09:08:42 -07:00
parent 4c81313cad
commit c1817f9691
5 changed files with 75 additions and 93 deletions

View File

@ -5104,9 +5104,9 @@ function _drupal_bootstrap_code() {
}
/**
* Temporary BC function for scripts not using DrupalKernel.
* Temporary BC function for scripts not using HttpKernel.
*
* DrupalKernel skips this and replicates it via event listeners.
* HttpKernel skips this and replicates it via event listeners.
*
* @see Drupal\Core\EventSubscriber\PathSubscriber;
* @see Drupal\Core\EventSubscriber\LegacyRequestSubscriber;

View File

@ -7,9 +7,22 @@
namespace Drupal\Core\DependencyInjection;
use Drupal\Core\ContentNegotiation;
use Drupal\Core\EventSubscriber\AccessSubscriber;
use Drupal\Core\EventSubscriber\FinishResponseSubscriber;
use Drupal\Core\EventSubscriber\LegacyControllerSubscriber;
use Drupal\Core\EventSubscriber\LegacyRequestSubscriber;
use Drupal\Core\EventSubscriber\MaintenanceModeSubscriber;
use Drupal\Core\EventSubscriber\PathSubscriber;
use Drupal\Core\EventSubscriber\RequestCloseSubscriber;
use Drupal\Core\EventSubscriber\RouterListener;
use Drupal\Core\EventSubscriber\ViewSubscriber;
use Drupal\Core\ExceptionController;
use Drupal\Core\LegacyUrlMatcher;
use Symfony\Component\DependencyInjection\ContainerBuilder as BaseContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpKernel\EventListener\ExceptionListener;
/**
* Drupal's dependency injection container.
@ -50,5 +63,57 @@ class ContainerBuilder extends BaseContainerBuilder {
// Register configuration object factory.
$this->register('config.factory', 'Drupal\Core\Config\ConfigFactory')
->addArgument(new Reference('config.storage.dispatcher'));
// Register the HTTP kernel services.
$this->register('dispatcher', 'Symfony\Component\EventDispatcher\EventDispatcher')
->addArgument(new Reference('service_container'))
->setFactoryClass('Drupal\Core\DependencyInjection\ContainerBuilder')
->setFactoryMethod('getKernelEventDispatcher');
$this->register('resolver', 'Symfony\Component\HttpKernel\Controller\ControllerResolver');
$this->register('httpkernel', 'Symfony\Component\HttpKernel\HttpKernel')
->addArgument(new Reference('dispatcher'))
->addArgument(new Reference('resolver'));
}
/**
* Creates an EventDispatcher for the HttpKernel. Factory method.
*
* @param Drupal\Core\DependencyInjection\ContainerBuilder $container
* The dependency injection container that contains the HTTP kernel.
*
* @return Symfony\Component\EventDispatcher\EventDispatcher
* An EventDispatcher with the default listeners attached to it.
*/
public static function getKernelEventDispatcher($container) {
$dispatcher = new EventDispatcher();
$matcher = new LegacyUrlMatcher();
$dispatcher->addSubscriber(new RouterListener($matcher));
$negotiation = new ContentNegotiation();
// @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 ViewSubscriber($negotiation));
$dispatcher->addSubscriber(new AccessSubscriber());
$dispatcher->addSubscriber(new MaintenanceModeSubscriber());
$dispatcher->addSubscriber(new PathSubscriber());
$dispatcher->addSubscriber(new LegacyRequestSubscriber());
$dispatcher->addSubscriber(new LegacyControllerSubscriber());
$dispatcher->addSubscriber(new FinishResponseSubscriber());
$dispatcher->addSubscriber(new RequestCloseSubscriber());
// 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.
$exceptionController = new ExceptionController($negotiation);
$exceptionController->setContainer($container);
$dispatcher->addSubscriber(new ExceptionListener(array($exceptionController, 'execute')));
return $dispatcher;
}
}

View File

@ -1,65 +0,0 @@
<?php
/**
* @file
* Definition of Drupal\Core\DrupalKernel.
*/
namespace Drupal\Core;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
use Symfony\Component\HttpKernel\EventListener\ExceptionListener;
use Drupal\Core\EventSubscriber\ViewSubscriber;
use Drupal\Core\EventSubscriber\AccessSubscriber;
use Drupal\Core\EventSubscriber\FinishResponseSubscriber;
use Drupal\Core\EventSubscriber\PathSubscriber;
use Drupal\Core\EventSubscriber\LegacyRequestSubscriber;
use Drupal\Core\EventSubscriber\LegacyControllerSubscriber;
use Drupal\Core\EventSubscriber\MaintenanceModeSubscriber;
use Drupal\Core\EventSubscriber\RequestCloseSubscriber;
use Drupal\Core\EventSubscriber\RouterListener;
/**
* The DrupalKernel class is the core of Drupal itself.
*/
class DrupalKernel extends HttpKernel {
/**
* Constructor.
*
* @param Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
* An EventDispatcherInterface instance.
* @param Symfony\Component\HttpKernel\Controller\ControllerResolverInterface $resolver
* A ControllerResolverInterface instance.
*/
public function __construct(EventDispatcherInterface $dispatcher, ControllerResolverInterface $resolver) {
parent::__construct($dispatcher, $resolver);
$this->matcher = new LegacyUrlMatcher();
$this->dispatcher->addSubscriber(new RouterListener($this->matcher));
$negotiation = new ContentNegotiation();
// @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 ViewSubscriber($negotiation));
$this->dispatcher->addSubscriber(new AccessSubscriber());
$this->dispatcher->addSubscriber(new MaintenanceModeSubscriber());
$this->dispatcher->addSubscriber(new PathSubscriber());
$this->dispatcher->addSubscriber(new LegacyRequestSubscriber());
$this->dispatcher->addSubscriber(new LegacyControllerSubscriber());
$this->dispatcher->addSubscriber(new FinishResponseSubscriber());
$this->dispatcher->addSubscriber(new RequestCloseSubscriber());
// 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(array(new ExceptionController($this, $negotiation), 'execute')));
}
}

View File

@ -7,25 +7,17 @@
namespace Drupal\Core;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\Exception\FlattenException;
/**
* This controller handles HTTP errors generated by the routing system.
*/
class ExceptionController {
/**
* The kernel that spawned this controller.
*
* We will use this to fire subrequests as needed.
*
* @var Symfony\Component\HttpKernel\HttpKernelInterface
*/
protected $kernel;
class ExceptionController extends ContainerAware {
/**
* The content negotiation library.
@ -37,15 +29,11 @@ class ExceptionController {
/**
* Constructor.
*
* @param Symfony\Component\HttpKernel\HttpKernelInterface $kernel
* The kernel that spawned this controller, so that it can be reused
* for subrequests.
* @param Drupal\Core\ContentNegotiation $negotiation
* The content negotiation library to use to determine the correct response
* format.
*/
public function __construct(HttpKernelInterface $kernel, ContentNegotiation $negotiation) {
$this->kernel = $kernel;
public function __construct(ContentNegotiation $negotiation) {
$this->negotiation = $negotiation;
}
@ -119,7 +107,7 @@ class ExceptionController {
drupal_static_reset('menu_set_active_trail');
menu_reset_static_cache();
$response = $this->kernel->handle($subrequest, DrupalKernel::SUB_REQUEST);
$response = $this->container->get('httpkernel')->handle($subrequest, HttpKernel::SUB_REQUEST);
$response->setStatusCode(403, 'Access denied');
}
else {
@ -184,7 +172,7 @@ class ExceptionController {
drupal_static_reset('menu_set_active_trail');
menu_reset_static_cache();
$response = $this->kernel->handle($subrequest, HttpKernelInterface::SUB_REQUEST);
$response = $this->container->get('httpkernel')->handle($subrequest, HttpKernel::SUB_REQUEST);
$response->setStatusCode(404, 'Not Found');
}
else {

View File

@ -11,10 +11,7 @@
* See COPYRIGHT.txt and LICENSE.txt files in the "core" directory.
*/
use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
/**
* Root directory of Drupal installation.
@ -39,9 +36,6 @@ request($request);
// @see Drupal\Core\EventSubscriber\LegacyRequestSubscriber;
drupal_bootstrap(DRUPAL_BOOTSTRAP_CODE);
$dispatcher = new EventDispatcher();
$resolver = new ControllerResolver();
$kernel = new DrupalKernel($dispatcher, $resolver);
$kernel = drupal_container()->get('httpkernel');
$response = $kernel->handle($request)->prepare($request)->send();
$kernel->terminate($request, $response);