diff --git a/core/core.events.yml b/core/core.events.yml index bfe84d4..c4e918f 100644 --- a/core/core.events.yml +++ b/core/core.events.yml @@ -17,8 +17,12 @@ subscribers: priority: 200 - callback: Drupal\Core\EventSubscriber\LegacyRequestSubscriber::onKernelRequestLegacy priority: 90 + - callback: Drupal\Core\Ajax\AjaxSubscriber::onKernelRequest + priority: 50 - callback: Drupal\Core\EventSubscriber\MaintenanceModeSubscriber::onKernelRequestDetermineSiteStatus priority: 40 + - callback: router_listener:onKernelRequest + priority: 32 - callback: Drupal\Core\EventSubscriber\MaintenanceModeSubscriber::onKernelRequestMaintenance priority: 30 - callback: Drupal\Core\EventSubscriber\LegacyRequestSubscriber::onKernelRequestLegacyAfterRouting @@ -27,8 +31,8 @@ subscribers: priority: 30 - callback: reverse_proxy_subscriber:onKernelRequestReverseProxyCheck priority: 10 - - Drupal\Core\Ajax\AjaxSubscriber::onKernelRequest - slave_database_ignore__subscriber:checkSlaveServer + - router.route_preloader:onRequest kernel.response: - finish_response_subscriber:onRespond - redirect_response_subscriber:checkRedirectUrl @@ -36,6 +40,8 @@ subscribers: - callback: Drupal\Core\EventSubscriber\AjaxResponseSubscriber::onResponse priority: -100 kernel.exception: + - callback: exception_listener:onKernelException + priority: -128 - authentication_subscriber:onException - route_enhancer.param_conversion:onException kernel.view: @@ -44,6 +50,8 @@ subscribers: - callback: html_view_subscriber:onHtmlPage priority: 50 - view_subscriber:onView + kernel.finish_request: + - router_listener:onKernelFinishRequest kernel.terminate: - callback: path_subscriber:onKernelTerminate priority: 200 @@ -63,7 +71,14 @@ subscribers: - callback: access_route_subscriber:onRoutingRouteAlterSetAccessCheck # Setting very low priority to ensure access checks are run after alters. priority: -1000 + # Set a really low priority to catch as many as possible routes. + - callback: router.route_preloader:onAlterRoutes + priority: -1024 + - callback: router.path_roots_subscriber:onRouteAlter + priority: -1024 routing.route_finished: - router.route_provider:reset + - router.route_preloader:onFinishedRoutes + - router.path_roots_subscriber:onRouteFinished - callback: router.rebuild_subscriber:onRouterRebuild priority: 200 diff --git a/core/core.services.yml b/core/core.services.yml index c4e0310..3b816f5 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -278,8 +278,6 @@ services: router.route_preloader: class: Drupal\Core\Routing\RoutePreloader arguments: ['@router.route_provider', '@state', '@content_negotiation'] - tags: - - { name: 'event_subscriber' } router.matcher.final_matcher: class: Drupal\Core\Routing\UrlMatcher router.matcher: @@ -307,8 +305,6 @@ services: router.path_roots_subscriber: class: Drupal\Core\EventSubscriber\PathRootsSubscriber arguments: ['@state'] - tags: - - { name: event_subscriber } entity.query: class: Drupal\Core\Entity\Query\QueryFactory arguments: ['@entity.manager'] @@ -420,8 +416,6 @@ services: class: Drupal\Core\Ajax\AjaxResponseRenderer router_listener: class: Symfony\Component\HttpKernel\EventListener\RouterListener - tags: - - { name: event_subscriber } arguments: ['@router'] content_negotiation: class: Drupal\Core\ContentNegotiation @@ -511,8 +505,6 @@ services: - [setContainer, ['@service_container']] exception_listener: class: Drupal\Core\EventSubscriber\ExceptionListener - tags: - - { name: event_subscriber } arguments: [['@exception_controller', execute]] route_processor_manager: class: Drupal\Core\RouteProcessor\RouteProcessorManager diff --git a/core/lib/Drupal/Core/EventListenerPass.php b/core/lib/Drupal/Core/EventListenerPass.php index 93bacdd..a00a644 100644 --- a/core/lib/Drupal/Core/EventListenerPass.php +++ b/core/lib/Drupal/Core/EventListenerPass.php @@ -13,6 +13,31 @@ use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +/** + * Defines a listener to register listeners defined in .events.yml files. + * + * These yml files contains a list of callbacks keyed by the event name, for + * example kernel.request. + * + * Each callback can be either a class, method or service, method combination. + * Services uses a single colon, classes two. + * + * @code + * kernel.request: + * - Drupal\user\EventSubscriber\MaintenanceModeSubscriber::onKernelRequestMaintenance + * - access_subscriber:onKernelRequestAccessCheck + * @endcode + * + * Additional you can also specify a priority: + * + * @code + * kernel.request: + * - callback: Drupal\user\EventSubscriber\MaintenanceModeSubscriber::onKernelRequestMaintenance + * priority: 14 + * - callback: access_subscriber:onKernelRequestAccessCheck + * priority: 95 + * @endcode + */ class EventListenerPass implements CompilerPassInterface { /** @@ -52,6 +77,23 @@ protected function findListeners(ContainerBuilder $container, Definition $event_ } } + /** + * Creates the needed listener information to pass onto the event dispatcher. + * + * This method uses a single listener information, determines whether it is + * a service, method or just class, method. + * + * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container + * The container to add event services to. + * @param string $event_id + * The used event. + * @param array $listener + * The parsed listener array. + * + * @return array + * An array containing 'addListenerService' and the needed arguments for + * $definition->addMethodCall. + */ protected function prepareListener(ContainerBuilder $container, $event_id, $listener) { if (is_array($listener)) { $callback = $listener['callback']; @@ -80,6 +122,18 @@ protected function prepareListener(ContainerBuilder $container, $event_id, $list return array($add_method, $args); } + /** + * Registers a new service without any dependency. + * + * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container + * The container builder. + * + * @param string $class + * The class of the new service. + * + * @return string + * The name of the new service. + */ protected function defineService(ContainerBuilder $container, $class) { $definition = new Definition(); $definition->setClass($class); @@ -88,6 +142,26 @@ protected function defineService(ContainerBuilder $container, $class) { return $name; } + /** + * Parses a single callback and converts it a php array. + * + * @param string $callback + * The listener string which is part of the events.yml file. + * + * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container + * The container builder. + * + * @throws \UnexpectedValueException + * Thrown when the callback could not be parsed. + * @throws \InvalidArgumentException + * Thrown when the class/service does not exists. + * + * @return array + * An array with the following keys: + * - service: The name of the service of the listener OR + * - class: The name of the class of the listener + * - method: The method name of the listener + */ protected function parseCallback($callback, ContainerBuilder $container) { // TODO: This logic should abstracted somewhere. $optional = $callback[0] == '?'; @@ -130,7 +204,7 @@ protected function parseCallback($callback, ContainerBuilder $container) { return array('class' => $class, 'method' => $method); } else { - throw new \LogicException(sprintf('Unable to parse the callback name "%s".', $callback)); + throw new \UnexpectedValueException(sprintf('Unable to parse the callback name "%s".', $callback)); } } diff --git a/core/lib/Drupal/Core/EventSubscriber/PathRootsSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/PathRootsSubscriber.php index f2701bc..de28469 100644 --- a/core/lib/Drupal/Core/EventSubscriber/PathRootsSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/PathRootsSubscriber.php @@ -9,14 +9,12 @@ use Drupal\Core\KeyValueStore\StateInterface; use Drupal\Core\Routing\RouteBuildEvent; -use Drupal\Core\Routing\RoutingEvents; use Symfony\Component\EventDispatcher\EventSubscriberInterface; -use Symfony\Component\Routing\RouteCollection; /** * Provides all available first bits of all route paths. */ -class PathRootsSubscriber implements EventSubscriberInterface { +class PathRootsSubscriber { /** * Stores the path roots available in the router. @@ -67,15 +65,4 @@ public function onRouteFinished() { unset($this->pathRoots); } - /** - * {@inheritdoc} - */ - public static function getSubscribedEvents() { - $events = array(); - // Try to set a low priority to ensure that all routes are already added. - $events[RoutingEvents::ALTER][] = array('onRouteAlter', -1024); - $events[RoutingEvents::FINISHED][] = array('onRouteFinished'); - return $events; - } - } diff --git a/core/lib/Drupal/Core/EventSubscriber/PathSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/PathSubscriber.php index 563ec8f..53e030f 100644 --- a/core/lib/Drupal/Core/EventSubscriber/PathSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/PathSubscriber.php @@ -10,15 +10,13 @@ use Drupal\Core\Path\AliasManagerInterface; use Drupal\Core\PathProcessor\InboundPathProcessorInterface; use Symfony\Component\HttpKernel\HttpKernelInterface; -use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\Event\PostResponseEvent; -use Symfony\Component\EventDispatcher\EventSubscriberInterface; /** * Provides a path subscriber that converts path aliases. */ -class PathSubscriber extends PathListenerBase implements EventSubscriberInterface { +class PathSubscriber extends PathListenerBase { /** * The alias manager that caches alias lookups based on the request. @@ -70,14 +68,4 @@ public function onKernelTerminate(PostResponseEvent $event) { $this->aliasManager->writeCache(); } - /** - * Registers the methods in this class that should be listeners. - * - * @return array - * An array of event listener definitions. - */ - static function getSubscribedEvents() { - $events[KernelEvents::TERMINATE][] = array('onKernelTerminate', 200); - return $events; - } } diff --git a/core/lib/Drupal/Core/EventSubscriber/ReverseProxySubscriber.php b/core/lib/Drupal/Core/EventSubscriber/ReverseProxySubscriber.php index 7b490da..f963ed2 100644 --- a/core/lib/Drupal/Core/EventSubscriber/ReverseProxySubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/ReverseProxySubscriber.php @@ -7,7 +7,6 @@ namespace Drupal\Core\EventSubscriber; -use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Drupal\Component\Utility\Settings; diff --git a/core/lib/Drupal/Core/EventSubscriber/RouterRebuildSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/RouterRebuildSubscriber.php index de9f526..776f87c 100644 --- a/core/lib/Drupal/Core/EventSubscriber/RouterRebuildSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/RouterRebuildSubscriber.php @@ -10,11 +10,9 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Lock\LockBackendInterface; use Drupal\Core\Routing\RouteBuilderInterface; -use Drupal\Core\Routing\RoutingEvents; use Symfony\Component\EventDispatcher\Event; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\PostResponseEvent; -use Symfony\Component\HttpKernel\KernelEvents; /** * Rebuilds the default menu links and runs menu-specific code if necessary. diff --git a/core/lib/Drupal/Core/ParamConverter/ParamConverterManager.php b/core/lib/Drupal/Core/ParamConverter/ParamConverterManager.php index 6a0f9c3..3a9117b 100644 --- a/core/lib/Drupal/Core/ParamConverter/ParamConverterManager.php +++ b/core/lib/Drupal/Core/ParamConverter/ParamConverterManager.php @@ -146,15 +146,5 @@ public function convert(array $defaults, Request $request) { return $defaults; } - /** - * Applies parameter converters to route parameters. - * - * @param \Drupal\Core\Routing\RouteBuildEvent $event - * The event to process. - */ - public function onRoutingRouteAlterSetParameterConverters(RouteBuildEvent $event) { - $this->setRouteParameterConverters($event->getRouteCollection()); - } - } diff --git a/core/lib/Drupal/Core/Routing/Enhancer/ParamConversionEnhancer.php b/core/lib/Drupal/Core/Routing/Enhancer/ParamConversionEnhancer.php index 2798faf..7e5698e 100644 --- a/core/lib/Drupal/Core/Routing/Enhancer/ParamConversionEnhancer.php +++ b/core/lib/Drupal/Core/Routing/Enhancer/ParamConversionEnhancer.php @@ -16,7 +16,6 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; -use Symfony\Component\HttpKernel\KernelEvents; /** * Provides a route enhancer that handles parameter conversion. diff --git a/core/lib/Drupal/Core/Routing/RoutePreloader.php b/core/lib/Drupal/Core/Routing/RoutePreloader.php index daa4130..3fb2c54 100644 --- a/core/lib/Drupal/Core/Routing/RoutePreloader.php +++ b/core/lib/Drupal/Core/Routing/RoutePreloader.php @@ -10,9 +10,7 @@ use Drupal\Core\ContentNegotiation; use Drupal\Core\KeyValueStore\StateInterface; use Symfony\Component\EventDispatcher\Event; -use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\KernelEvent; -use Symfony\Component\HttpKernel\KernelEvents; /** * Defines a class which preloads non-admin routes. @@ -21,7 +19,7 @@ * list of all routes which most likely appear on the actual site, which are all * HTML routes not starting with "/admin". */ -class RoutePreloader implements EventSubscriberInterface { +class RoutePreloader { /** * The route provider. @@ -116,17 +114,4 @@ public function onFinishedRoutes(Event $event) { $this->nonAdminRoutesOnRebuild = array(); } - /** - * {@inheritdoc} - */ - public static function getSubscribedEvents() { - // Set a really low priority to catch as many as possible routes. - $events[RoutingEvents::ALTER] = array('onAlterRoutes', -1024); - $events[RoutingEvents::FINISHED] = array('onFinishedRoutes'); - // Load the routes before the controller is executed (which happens after - // the kernel request event). - $events[KernelEvents::REQUEST][] = array('onRequest'); - return $events; - } - } diff --git a/core/lib/Drupal/Core/Routing/RouteProvider.php b/core/lib/Drupal/Core/Routing/RouteProvider.php index c62e5b1..3d3b016 100644 --- a/core/lib/Drupal/Core/Routing/RouteProvider.php +++ b/core/lib/Drupal/Core/Routing/RouteProvider.php @@ -8,7 +8,6 @@ namespace Drupal\Core\Routing; use Drupal\Component\Utility\String; -use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Exception\RouteNotFoundException; use Symfony\Component\Routing\RouteCollection; diff --git a/core/modules/system/tests/modules/session_test/session_test.events.yml b/core/modules/system/tests/modules/session_test/session_test.events.yml index 94b99cd..27f061b 100644 --- a/core/modules/system/tests/modules/session_test/session_test.events.yml +++ b/core/modules/system/tests/modules/session_test/session_test.events.yml @@ -1,7 +1,7 @@ subscribers: kernel.request: - - callback: Drupal\session_test\EventSubscriber\SessionTestSubscriber::onKernelResponseSessionTest + - callback: Drupal\session_test\EventSubscriber\SessionTestSubscriber::onKernelRequestSessionTest priority: 300 kernel.response: - - callback: Drupal\session_test\EventSubscriber\SessionTestSubscriber::onKernelRequestSessionTest + - callback: Drupal\session_test\EventSubscriber\SessionTestSubscriber::onKernelResponseSessionTest priority: 100 diff --git a/core/tests/Drupal/Tests/Core/EventListenerPassTest.php b/core/tests/Drupal/Tests/Core/EventListenerPassTest.php index f365ad3..39a7375 100644 --- a/core/tests/Drupal/Tests/Core/EventListenerPassTest.php +++ b/core/tests/Drupal/Tests/Core/EventListenerPassTest.php @@ -340,7 +340,7 @@ public function testMultipleEvents() { /** * Tests the event listener pass with an invalid yaml file. * - * @expectedException \LogicException + * @expectedException \UnexpectedValueException * @expectedExceptionMessage Unable to parse the callback name "test_service-test" */ public function testInvalidYaml() {