diff --git a/core/core.services.yml b/core/core.services.yml index 5419d85..8e6f77e 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -634,7 +634,7 @@ services: - { name: backend_overridable } router.builder: class: Drupal\Core\Routing\RouteBuilder - arguments: ['@router.dumper', '@lock', '@event_dispatcher', '@module_handler', '@controller_resolver', '@access_manager.check_provider', '@cache.default'] + arguments: ['@router.dumper', '@lock', '@event_dispatcher', '@module_handler', '@controller_resolver', '@access_manager.check_provider'] tags: - { name: needs_destruction } menu.rebuild_subscriber: diff --git a/core/lib/Drupal/Core/Routing/RouteBuilder.php b/core/lib/Drupal/Core/Routing/RouteBuilder.php index 23f93c7..6112e0c 100644 --- a/core/lib/Drupal/Core/Routing/RouteBuilder.php +++ b/core/lib/Drupal/Core/Routing/RouteBuilder.php @@ -8,7 +8,6 @@ namespace Drupal\Core\Routing; use Drupal\Component\Discovery\YamlDiscovery; -use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Access\CheckProviderInterface; use Drupal\Core\Controller\ControllerResolverInterface; use Drupal\Core\Extension\ModuleHandlerInterface; @@ -109,17 +108,14 @@ class RouteBuilder implements RouteBuilderInterface, DestructableInterface { * The controller resolver. * @param \Drupal\Core\Access\CheckProviderInterface $check_provider * The check provider. - * @param \Drupal\Core\Cache\CacheBackendInterface $cache - * The cache object. */ - public function __construct(MatcherDumperInterface $dumper, LockBackendInterface $lock, EventDispatcherInterface $dispatcher, ModuleHandlerInterface $module_handler, ControllerResolverInterface $controller_resolver, CheckProviderInterface $check_provider, CacheBackendInterface $cache) { + public function __construct(MatcherDumperInterface $dumper, LockBackendInterface $lock, EventDispatcherInterface $dispatcher, ModuleHandlerInterface $module_handler, ControllerResolverInterface $controller_resolver, CheckProviderInterface $check_provider) { $this->dumper = $dumper; $this->lock = $lock; $this->dispatcher = $dispatcher; $this->moduleHandler = $module_handler; $this->controllerResolver = $controller_resolver; $this->checkProvider = $check_provider; - $this->cache = $cache; } /** @@ -148,10 +144,7 @@ public function rebuild() { $this->building = TRUE; $collection = new RouteCollection(); - - $module_dirs = $this->moduleHandler->getModuleDirectories(); - $this->yamlDiscovery = new YamlDiscovery('routing', $module_dirs); - foreach ($this->yamlDiscovery->findAll() as $routes) { + foreach ($this->getRouteDefinitions() as $routes) { // The top-level 'routes_callback' is a list of methods in controller // syntax, see \Drupal\Core\Controller\ControllerResolver. These methods // should return a set of \Symfony\Component\Routing\Route objects, either @@ -207,7 +200,6 @@ public function rebuild() { $this->dispatcher->dispatch(RoutingEvents::FINISHED, new Event()); $this->building = FALSE; - $this->routeCollection = NULL; $this->rebuildNeeded = FALSE; return TRUE; @@ -227,9 +219,20 @@ public function rebuildIfNeeded() { * {@inheritdoc} */ public function destruct() { - if ($this->rebuildNeeded) { - $this->rebuild(); + $this->rebuildIfNeeded(); + } + + /** + * Retrieves all defined routes from .routing.yml files. + * + * @return array + * The defined routes, keyed by provider. + */ + protected function getRouteDefinitions() { + if (!isset($this->yamlDiscovery)) { + $this->yamlDiscovery = new YamlDiscovery('routing', $this->moduleHandler->getModuleDirectories()); } + return $this->yamlDiscovery->findAll(); } } diff --git a/core/lib/Drupal/Core/Routing/RouteProvider.php b/core/lib/Drupal/Core/Routing/RouteProvider.php index e16dcb7..86ecbd1 100644 --- a/core/lib/Drupal/Core/Routing/RouteProvider.php +++ b/core/lib/Drupal/Core/Routing/RouteProvider.php @@ -117,7 +117,7 @@ public function __construct(Connection $connection, RouteBuilderInterface $route public function getRouteCollectionForRequest(Request $request) { $path = $this->currentPath->getPath($request); - return $this->getRoutesByPath($path); + return $this->getRoutesByPath(rtrim($path, '/')); } /** diff --git a/core/modules/field/src/Tests/FieldImportDeleteUninstallTest.php b/core/modules/field/src/Tests/FieldImportDeleteUninstallTest.php index 08dda90..d71be2e 100644 --- a/core/modules/field/src/Tests/FieldImportDeleteUninstallTest.php +++ b/core/modules/field/src/Tests/FieldImportDeleteUninstallTest.php @@ -27,7 +27,6 @@ class FieldImportDeleteUninstallTest extends FieldUnitTestBase { protected function setUp() { parent::setUp(); // Module uninstall requires the router and users_data tables. - // FieldUnitTestBase already install the router table. // @see drupal_flush_all_caches() // @see user_modules_uninstalled() $this->installSchema('user', array('users_data')); diff --git a/core/modules/field_ui/field_ui.module b/core/modules/field_ui/field_ui.module index 24fbf6e..5c2e22f 100644 --- a/core/modules/field_ui/field_ui.module +++ b/core/modules/field_ui/field_ui.module @@ -169,14 +169,14 @@ function field_ui_form_node_type_form_submit($form, FormStateInterface $form_sta * Implements hook_entity_view_mode_presave(). */ function field_ui_entity_view_mode_presave(EntityViewModeInterface $view_mode) { - \Drupal::service('router.builder')->rebuild(); + \Drupal::service('router.builder')->setRebuildNeeded(); } /** * Implements hook_entity_form_mode_presave(). */ function field_ui_entity_form_mode_presave(EntityFormModeInterface $form_mode) { - \Drupal::service('router.builder')->rebuild(); + \Drupal::service('router.builder')->setRebuildNeeded(); } /** diff --git a/core/modules/shortcut/src/Tests/ShortcutLinksTest.php b/core/modules/shortcut/src/Tests/ShortcutLinksTest.php index 5cb82f4..a59e5d5 100644 --- a/core/modules/shortcut/src/Tests/ShortcutLinksTest.php +++ b/core/modules/shortcut/src/Tests/ShortcutLinksTest.php @@ -183,7 +183,7 @@ public function testShortcutLinkChangeRoute() { entity_load('view', 'content')->disable()->save(); /** @var \Drupal\Core\Routing\RouteBuilderInterface $router_builder */ $router_builder = \Drupal::service('router.builder'); - $router_builder->rebuild(); + $router_builder->rebuildIfNeeded(); $this->drupalGet('admin/content'); $this->assertResponse(200); } diff --git a/core/modules/views/src/Tests/Plugin/RelationshipJoinTestBase.php b/core/modules/views/src/Tests/Plugin/RelationshipJoinTestBase.php index 868c4db..5620c0a 100644 --- a/core/modules/views/src/Tests/Plugin/RelationshipJoinTestBase.php +++ b/core/modules/views/src/Tests/Plugin/RelationshipJoinTestBase.php @@ -27,7 +27,7 @@ /** * @var \Drupal\user\Entity\User */ - protected $root_user; + protected $rootUser; /** * Overrides \Drupal\views\Tests\ViewUnitTestBase::setUpFixtures(). @@ -38,9 +38,8 @@ protected function setUpFixtures() { parent::setUpFixtures(); // Create a record for uid 1. - $this->installSchema('system', 'sequences'); - $this->root_user = entity_create('user', array('name' => $this->randomMachineName())); - $this->root_user->save(); + $this->rootUser = entity_create('user', array('name' => $this->randomMachineName())); + $this->rootUser->save(); Views::viewsData()->clear(); } diff --git a/core/tests/Drupal/Tests/Core/Routing/RouteBuilderTest.php b/core/tests/Drupal/Tests/Core/Routing/RouteBuilderTest.php index 0b00a7c..94a001a 100644 --- a/core/tests/Drupal/Tests/Core/Routing/RouteBuilderTest.php +++ b/core/tests/Drupal/Tests/Core/Routing/RouteBuilderTest.php @@ -72,13 +72,6 @@ class RouteBuilderTest extends UnitTestCase { protected $controllerResolver; /** - * The cache object for our RouteBuilder. - * - * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject - */ - protected $cache; - - /** * @var \Drupal\Core\Access\CheckProviderInterface|\PHPUnit_Framework_MockObject_MockObject */ protected $checkProvider; @@ -87,7 +80,6 @@ protected function setUp() { $this->dumper = $this->getMock('Drupal\Core\Routing\MatcherDumperInterface'); $this->lock = $this->getMock('Drupal\Core\Lock\LockBackendInterface'); $this->dispatcher = $this->getMock('\Symfony\Component\EventDispatcher\EventDispatcherInterface'); - $this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface'); $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'); $this->controllerResolver = $this->getMock('Drupal\Core\Controller\ControllerResolverInterface'); $this->yamlDiscovery = $this->getMockBuilder('\Drupal\Component\Discovery\YamlDiscovery') @@ -95,7 +87,7 @@ protected function setUp() { ->getMock(); $this->checkProvider = $this->getMock('\Drupal\Core\Access\CheckProviderInterface'); - $this->routeBuilder = new TestRouteBuilder($this->dumper, $this->lock, $this->dispatcher, $this->moduleHandler, $this->controllerResolver, $this->checkProvider, $this->cache); + $this->routeBuilder = new TestRouteBuilder($this->dumper, $this->lock, $this->dispatcher, $this->moduleHandler, $this->controllerResolver, $this->checkProvider); $this->routeBuilder->setYamlDiscovery($this->yamlDiscovery); } @@ -112,14 +104,6 @@ public function testRebuildLockingUnlocking() { ->method('release') ->with('router_rebuild'); - $this->moduleHandler->expects($this->once()) - ->method('getModuleDirectories') - ->will($this->returnValue(array())); - - $this->cache->expects($this->once()) - ->method('getMultiple') - ->will($this->returnValue(array())); - $this->yamlDiscovery->expects($this->any()) ->method('findAll') ->will($this->returnValue(array())); @@ -167,14 +151,6 @@ public function testRebuildWithStaticModuleRoutes() { ->method('findAll') ->will($this->returnValue(array('test_module' => $routes))); - $this->moduleHandler->expects($this->once()) - ->method('getModuleDirectories') - ->will($this->returnValue(array('test_module' => 'some/dir'))); - - $this->cache->expects($this->once()) - ->method('getMultiple') - ->will($this->returnValue(array())); - $route_collection = $routing_fixtures->sampleRouteCollection(); $route_build_event = new RouteBuildEvent($route_collection); @@ -248,14 +224,6 @@ public function testRebuildWithProviderBasedRoutes() { $route_build_event = new RouteBuildEvent($route_collection_filled); - $this->moduleHandler->expects($this->once()) - ->method('getModuleDirectories') - ->will($this->returnValue(array('test_module' => 'some/dir'))); - - $this->cache->expects($this->once()) - ->method('getMultiple') - ->will($this->returnValue(array())); - // Ensure that the alter routes events are fired. $this->dispatcher->expects($this->at(0)) ->method('dispatch')