Commit 784cebbe authored by catch's avatar catch

Issue #2098795 by tim.plunkett, Sean Charles, tstoeckler, dawehner, larowlan,...

Issue #2098795 by tim.plunkett, Sean Charles, tstoeckler, dawehner, larowlan, damiankloip: Create Base Class for RouteSubscriber Class.
parent 25f081af
......@@ -8,14 +8,13 @@
namespace Drupal\Core\EventSubscriber;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Routing\RouteBuildEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\Core\Routing\RoutingEvents;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
/**
* A route subscriber to remove routes that depend on modules being enabled.
*/
class ModuleRouteSubscriber implements EventSubscriberInterface {
class ModuleRouteSubscriber extends RouteSubscriberBase {
/**
* The module handler.
......@@ -37,20 +36,7 @@ public function __construct(ModuleHandlerInterface $module_handler) {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[RoutingEvents::ALTER] = 'removeRoutes';
return $events;
}
/**
* Removes any routes that have an unmet module dependency.
*
* @param \Drupal\Core\Routing\RouteBuildEvent $event
* The route building event.
*/
public function removeRoutes(RouteBuildEvent $event) {
$collection = $event->getRouteCollection();
protected function alterRoutes(RouteCollection $collection, $module) {
foreach ($collection as $name => $route) {
if ($route->hasRequirement('_module_dependencies')) {
$modules = $route->getRequirement('_module_dependencies');
......
......@@ -9,25 +9,19 @@
use Drupal\Component\Utility\String;
use Drupal\Core\Routing\RouteBuildEvent;
use Drupal\Core\Routing\RoutingEvents;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\RouteCollection;
/**
* Provides a route subscriber which checks for invalid pattern variables.
*/
class SpecialAttributesRouteSubscriber implements EventSubscriberInterface {
class SpecialAttributesRouteSubscriber extends RouteSubscriberBase {
/**
* Checks for invalid pattern variables.
*
* @param \Drupal\Core\Routing\RouteBuildEvent $event
* The event containing the build routes.
*
* @throws \InvalidArgumentException
* Thrown when a reserved variable was used as route variable.
* {@inheritdoc}
*/
public function onRouteBuilding(RouteBuildEvent $event) {
protected function alterRoutes(RouteCollection $collection, $module) {
$special_variables = array(
'_account',
'system_path',
......@@ -41,7 +35,7 @@ public function onRouteBuilding(RouteBuildEvent $event) {
'_form',
);
foreach ($event->getRouteCollection()->all() as $route) {
foreach ($collection->all() as $route) {
if ($not_allowed_variables = array_intersect($route->compile()->getVariables(), $special_variables)) {
$placeholders = array('@variables' => implode(', ', $not_allowed_variables));
drupal_set_message(String::format('The following variables are reserved names by drupal: @variables', $placeholders));
......@@ -53,11 +47,17 @@ public function onRouteBuilding(RouteBuildEvent $event) {
}
/**
* {@inheritdoc}
* Delegates the route altering to self::alterRoutes().
*
* @param \Drupal\Core\Routing\RouteBuildEvent $event
* The route build event.
*
* @return bool
* Returns TRUE if the variables were succesfully replaced, otherwise FALSE.
*/
static function getSubscribedEvents() {
$events[RoutingEvents::ALTER][] = 'onRouteBuilding';
return $events;
public function onAlterRoutes(RouteBuildEvent $event) {
$collection = $event->getRouteCollection();
return $this->alterRoutes($collection, $event->getModule());
}
}
<?php
/**
* @file
* Contains \Drupal\Core\Routing\RouteSubscriberBase
*/
namespace Drupal\Core\Routing;
use Drupal\Core\Routing\RoutingEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\RouteCollection;
/**
* Provides a base implementation for RouteSubscriber.
*/
abstract class RouteSubscriberBase implements EventSubscriberInterface {
/**
* Provides new routes by adding them to the collection.
*
* Subclasses should use this method and add \Symfony\Component\Routing\Route
* objects with $collection->add($route);.
*
* @param \Symfony\Component\Routing\RouteCollection $collection
* The route collection for adding routes.
*/
protected function routes(RouteCollection $collection) {
}
/**
* Alters existing routes for a specific collection.
*
* @param \Symfony\Component\Routing\RouteCollection $collection
* The route collection for adding routes.
* @param string $module
* The module these routes belong to. For dynamically added routes, the
* module name will be 'dynamic_routes'.
*/
protected function alterRoutes(RouteCollection $collection, $module) {
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[RoutingEvents::DYNAMIC] = 'onDynamicRoutes';
$events[RoutingEvents::ALTER] = 'onAlterRoutes';
return $events;
}
/**
* Delegates the route gathering to self::routes().
*
* @param \Drupal\Core\Routing\RouteBuildEvent $event
* The route build event.
*/
public function onDynamicRoutes(RouteBuildEvent $event) {
$collection = $event->getRouteCollection();
$this->routes($collection);
}
/**
* Delegates the route altering to self::alterRoutes().
*
* @param \Drupal\Core\Routing\RouteBuildEvent $event
* The route build event.
*/
public function onAlterRoutes(RouteBuildEvent $event) {
$collection = $event->getRouteCollection();
$this->alterRoutes($collection, $event->getModule());
}
}
......@@ -7,36 +7,19 @@
namespace Drupal\block\Routing;
use Drupal\Core\Routing\RouteBuildEvent;
use Drupal\Core\Routing\RoutingEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\Route;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\RouteCollection;
/**
* Provides dynamic routes for various block pages.
*/
class RouteSubscriber implements EventSubscriberInterface {
class RouteSubscriber extends RouteSubscriberBase {
/**
* Implements EventSubscriberInterface::getSubscribedEvents().
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[RoutingEvents::DYNAMIC] = 'routes';
return $events;
}
/**
* Generates dynamic routes for various block pages.
*
* @param \Drupal\Core\Routing\RouteBuildEvent $event
* The route building event.
*
* @return \Symfony\Component\Routing\RouteCollection
* The route collection that contains the new dynamic route.
*/
public function routes(RouteBuildEvent $event) {
$collection = $event->getRouteCollection();
protected function routes(RouteCollection $collection) {
foreach (list_themes(TRUE) as $key => $theme) {
// The block entity listing page.
$route = new Route(
......
......@@ -7,16 +7,15 @@
namespace Drupal\content_translation\Routing;
use Drupal\Core\Routing\RouteBuildEvent;
use Drupal\Core\Routing\RoutingEvents;
use Drupal\Core\Entity\EntityManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
/**
* Subscriber for entity translation routes.
*/
class ContentTranslationRouteSubscriber implements EventSubscriberInterface {
class ContentTranslationRouteSubscriber extends RouteSubscriberBase {
/**
* The entity type manager.
......@@ -38,16 +37,7 @@ public function __construct(EntityManager $entityManager) {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[RoutingEvents::DYNAMIC] = 'routes';
return $events;
}
/**
* Adds routes for entity translations.
*/
public function routes(RouteBuildEvent $event) {
$collection = $event->getRouteCollection();
protected function routes(RouteCollection $collection) {
foreach ($this->entityManager->getDefinitions() as $entity_type => $entity_info) {
if ($entity_info['translatable'] && isset($entity_info['translation'])) {
$path = '/' . str_replace($entity_info['menu_path_wildcard'], '{' . $entity_type . '}', $entity_info['menu_base_path']) . '/translations';
......
......@@ -7,16 +7,15 @@
namespace Drupal\field_ui\Routing;
use Drupal\Core\Routing\RouteBuildEvent;
use Drupal\Core\Routing\RoutingEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Route;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
/**
* Subscriber for Field UI routes.
*/
class RouteSubscriber implements EventSubscriberInterface {
class RouteSubscriber extends RouteSubscriberBase {
/**
* The entity type manager
......@@ -38,16 +37,7 @@ public function __construct(EntityManager $manager) {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[RoutingEvents::DYNAMIC] = 'routes';
return $events;
}
/**
* Adds routes for the Field UI.
*/
public function routes(RouteBuildEvent $event) {
$collection = $event->getRouteCollection();
protected function routes(RouteCollection $collection) {
foreach ($this->manager->getDefinitions() as $entity_type => $entity_info) {
$defaults = array();
if ($entity_info['fieldable'] && isset($entity_info['route_base_path'])) {
......
......@@ -7,38 +7,23 @@
namespace Drupal\image\EventSubscriber;
use Drupal\Core\Routing\RouteBuildEvent;
use Drupal\Core\Routing\RoutingEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
/**
* Defines a route subscriber to register a url for serving image styles.
*/
class RouteSubscriber implements EventSubscriberInterface {
class RouteSubscriber extends RouteSubscriberBase {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[RoutingEvents::DYNAMIC] = 'dynamicRoutes';
return $events;
}
/**
* Registers dynamic routes for image styles.
*
* Generate image derivatives of publicly available files. If clean URLs are
* disabled, image derivatives will always be served through the menu system.
* If clean URLs are enabled and the image derivative already exists, PHP will
* be bypassed.
*
* @param \Drupal\Core\Routing\RouteBuildEvent $event
* The route building event.
*/
public function dynamicRoutes(RouteBuildEvent $event) {
$collection = $event->getRouteCollection();
protected function routes(RouteCollection $collection) {
// Generate image derivatives of publicly available files. If clean URLs are
// disabled image derivatives will always be served through the menu system.
// If clean URLs are enabled and the image derivative already exists, PHP
// will be bypassed.
$directory_path = file_stream_wrapper_get_instance_by_scheme('public')->getDirectoryPath();
$route = new Route('/' . $directory_path . '/styles/{image_style}/{scheme}',
......
<?php
/**
* Definition of \Drupal\rest\EventSubscriber\RouteSubscriber.
* @file
* Contains \Drupal\rest\EventSubscriber\RouteSubscriber.
*/
namespace Drupal\rest\EventSubscriber;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Routing\RouteBuildEvent;
use Drupal\Core\Routing\RoutingEvents;
use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\rest\Plugin\Type\ResourcePluginManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\RouteCollection;
/**
* Subscriber for REST-style routes.
*/
class RouteSubscriber implements EventSubscriberInterface {
class RouteSubscriber extends RouteSubscriberBase {
/**
* The plugin manager for REST plugins.
......@@ -46,14 +45,9 @@ public function __construct(ResourcePluginManager $manager, ConfigFactory $confi
}
/**
* Adds routes to enabled REST resources.
*
* @param \Drupal\Core\Routing\RouteBuildEvent $event
* The route building event.
* {@inheritdoc}
*/
public function dynamicRoutes(RouteBuildEvent $event) {
$collection = $event->getRouteCollection();
protected function routes(RouteCollection $collection) {
$enabled_resources = $this->config->get('rest.settings')->load()->get('resources');
// Iterate over all enabled resource plugins.
......@@ -88,12 +82,4 @@ public function dynamicRoutes(RouteBuildEvent $event) {
}
}
/**
* Implements EventSubscriberInterface::getSubscribedEvents().
*/
static function getSubscribedEvents() {
$events[RoutingEvents::DYNAMIC] = 'dynamicRoutes';
return $events;
}
}
......@@ -2,21 +2,20 @@
/**
* @file
* Contains Drupal\search\Routing\SearchRouteSubscriber
* Contains \Drupal\search\Routing\SearchRouteSubscriber.
*/
namespace Drupal\search\Routing;
use Drupal\Core\Routing\RouteBuildEvent;
use Drupal\Core\Routing\RoutingEvents;
use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\search\SearchPluginManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
/**
* Provides dynamic routes for search.
*/
class SearchRouteSubscriber implements EventSubscriberInterface {
class SearchRouteSubscriber extends RouteSubscriberBase {
/**
* The search plugin manager.
......@@ -38,20 +37,7 @@ public function __construct(SearchPluginManager $search_plugin_manager) {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[RoutingEvents::DYNAMIC] = 'routes';
return $events;
}
/**
* Adds routes for search.
*
* @param \Drupal\Core\Routing\RouteBuildEvent $event
* The route building event.
*/
public function routes(RouteBuildEvent $event) {
$collection = $event->getRouteCollection();
protected function routes(RouteCollection $collection) {
foreach ($this->searchManager->getActiveDefinitions() as $plugin_id => $search_info) {
$path = 'search/' . $search_info['path'] . '/{keys}';
$defaults = array(
......
......@@ -7,32 +7,19 @@
namespace Drupal\system\Routing;
use Drupal\Core\Routing\RouteBuildEvent;
use Drupal\Core\Routing\RoutingEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
/**
* Provides dynamic routes for theme administration.
*/
class RouteSubscriber implements EventSubscriberInterface {
class RouteSubscriber extends RouteSubscriberBase {
/**
* Implements EventSubscriberInterface::getSubscribedEvents().
* {@inheritdoc}
*/
static function getSubscribedEvents() {
$events[RoutingEvents::DYNAMIC] = 'createSystemThemeRoutes';
return $events;
}
/**
* Adds dynamic system theme routes.
*
* @param \Drupal\Core\Routing\RouteBuildEvent $event
* The route building event.
*/
public function createSystemThemeRoutes(RouteBuildEvent $event) {
$collection = $event->getRouteCollection();
protected function routes(RouteCollection $collection) {
foreach (list_themes() as $theme) {
if (!empty($theme->status)) {
$route = new Route('admin/appearance/settings/' . $theme->name, array(
......
......@@ -7,29 +7,19 @@
namespace Drupal\entity_test\Routing;
use Drupal\Core\Routing\RouteBuildEvent;
use Drupal\Core\Routing\RoutingEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
/**
* Subscriber for Entity Test routes.
*/
class RouteSubscriber implements EventSubscriberInterface {
class RouteSubscriber extends RouteSubscriberBase {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[RoutingEvents::DYNAMIC] = 'routes';
return $events;
}
/**
* Adds routes for the Entity Test.
*/
public function routes(RouteBuildEvent $event) {
$collection = $event->getRouteCollection();
protected function routes(RouteCollection $collection) {
$types = entity_test_entity_types();
$types[] = 'entity_test_render';
......
......@@ -7,18 +7,18 @@
namespace Drupal\form_test\EventSubscriber;
use Drupal\Core\Routing\RouteBuildEvent;
use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\Core\Routing\RoutingEvents;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\RouteCollection;
/**
* Test event subscriber to add new attributes to the request.
*/
class FormTestEventSubscriber implements EventSubscriberInterface {
class FormTestEventSubscriber extends RouteSubscriberBase {
/**
* The module handler.
......@@ -51,18 +51,15 @@ public function onKernelRequest(GetResponseEvent $event) {
*/
public static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = array('onKernelRequest');
$events[RoutingEvents::DYNAMIC] = 'routes';
$events[RoutingEvents::DYNAMIC] = 'onDynamicRoutes';
$events[RoutingEvents::ALTER] = 'onAlterRoutes';
return $events;
}
/**
* Adds routes for the Form Tests.
* {@inheritdoc}
*/
public function routes(RouteBuildEvent $event) {
$collection = $event->getRouteCollection();
$defaults = array();
protected function routes(RouteCollection $collection) {
if ($this->moduleHandler->moduleExists('node')) {
$route = new Route(
"form-test/two-instances-of-same-form",
......
......@@ -8,15 +8,13 @@
namespace Drupal\views\EventSubscriber;
use Drupal\Component\Utility\MapArray;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\DestructableInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
use Drupal\Core\Routing\RouteBuildEvent;
use Drupal\Core\Routing\RoutingEvents;
use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\views\Plugin\views\display\DisplayRouterInterface;
use Drupal\views\ViewExecutable;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\RouteCollection;
/**
* Builds up the routes of all views.
......@@ -27,7 +25,7 @@
*
* @see \Drupal\views\Plugin\views\display\PathPluginBase
*/
class RouteSubscriber implements EventSubscriberInterface, DestructableInterface {
class RouteSubscriber extends RouteSubscriberBase implements DestructableInterface {
/**
* Stores a list of view,display IDs which haven't be used in the alter event.
......@@ -77,15 +75,6 @@ public function reset() {
$this->viewsDisplayPairs = NULL;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[RoutingEvents::DYNAMIC] = 'dynamicRoutes';
$events[RoutingEvents::ALTER] = 'alterRoutes';
return $events;
}
/**
* Gets all the views and display IDs using a route.
*/
......@@ -106,14 +95,9 @@ protected function getViewsDisplayIDsWithRoute() {
}
/**
* Adds routes defined by all views.
*
* @param \Drupal\Core\Routing\RouteBuildEvent $event
* The route building event.
* {@inheritdoc}
*/
public function dynamicRoutes(RouteBuildEvent $event) {
$collection = $event->getRouteCollection();
protected function routes(RouteCollection $collection) {
foreach ($this->getViewsDisplayIDsWithRoute() as $pair) {
list($view_id, $display_id) = explode('.', $pair);
$view = $this->viewStorageController->load($view_id);
......@@ -134,12 +118,9 @@ public function dynamicRoutes(RouteBuildEvent $event) {
}
/**
* Alters existing routes.
*
* @param \Drupal\Core\Routing\RouteBuildEvent $event
* The route building event.
* {@inheritdoc}
*/
public function alterRoutes(RouteBuildEvent $event) {
protected function alterRoutes(RouteCollection $collection, $module) {
foreach ($this->getViewsDisplayIDsWithRoute() as $pair) {
list($view_id, $display_id) = explode('.', $pair);
$view = $this->viewStorageController->load($view_id);
......@@ -149,7 +130,7 @@ public function alterRoutes(RouteBuildEvent $event) {
if ($display instanceof DisplayRouterInterface) {
// If the display returns TRUE a route item was found, so it does not
// have to be added.
$view_route_names = $display->alterRoutes($event->getRouteCollection());
$view_route_names = $display->alterRoutes($collection);
$this->viewRouteNames += $view_route_names;
foreach ($view_route_names as $id_display => $route_name) {
unset($this->viewsDisplayPairs[$id_display]);
......
......@@ -95,7 +95,7 @@ public function testPageResponses() {
public function testPageRouterItems() {
$subscriber = new RouteSubscriber($this->container->get('entity.manager'), $this->container->get('state'));
$collection = new RouteCollection();
$subscriber->dynamicRoutes(new RouteBuildEvent($collection, 'dynamic_routes'));
$subscriber->onDynamicRoutes(new RouteBuildEvent($collection, 'dynamic_routes'));
// Check the controller defaults.
foreach ($collection as $id => $route) {
......
......@@ -75,9 +75,9 @@ protected function setUp() {
}
/**
* Tests the dynamicRoutes method.
* Tests the onDynamicRoutes method.
*
* @see \Drupal\views\EventSubscriber\RouteSubscriber::dynamicRoutes()
* @see \Drupal\views\EventSubscriber\RouteSubscriber::onDynamicRoutes()
*/
public function testDynamicRoutes() {
$collection = new RouteCollection();
......@@ -92,7 +92,7 @@ public function testDynamicRoutes() {
->method('collectRoutes')
->will($this->returnValue(array('test_id.page_2' => 'views.test_id.page_2')));
$this->assertNull($this->routeSubscriber->dynamicRoutes($route_event));
$this->assertNull($this->routeSubscriber->onDynamicRoutes($route_event));
$this->state->expects($this->once())
->method('set')
......@@ -101,11 +101,11 @@ public function testDynamicRoutes() {
}
/**
* Tests the alterRoutes method.
* Tests the onAlterRoutes method.
*
* @see \Drupal\views\EventSubscriber\RouteSubscriber::alterRoutes()
* @see \Drupal\views\EventSubscriber\RouteSubscriber::onAlterRoutes()
*/
public function testAlterRoutes() {
public function testOnAlterRoutes() {
$collection = new RouteCollection();
$collection->add('test_route', new Route('test_route', array('_controller' => 'Drupal\Tests\Core\Controller\TestController')));
$route_2 = new Route('test_route/example', array('_controller' => 'Drupal\Tests\Core\Controller\TestController'));
......@@ -130,12 +130,12 @@ public function testAlterRoutes() {
->method('collectRoutes')
->will($this->returnValue(array('test_id.page_2' => 'views.test_id.page_2')));
$this->assertNull($this->routeSubscriber->alterRoutes($route_event));
$this-