diff --git a/core/core.services.yml b/core/core.services.yml index af7193c..1f3c2a1 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -308,7 +308,7 @@ services: arguments: ['@router.dumper', '@lock', '@event_dispatcher', '@module_handler', '@controller_resolver', '@state'] router.rebuild_subscriber: class: Drupal\Core\EventSubscriber\RouterRebuildSubscriber - arguments: ['@router.builder', '@cache.cache'] + arguments: ['@router.builder'] tags: - { name: event_subscriber } path.alias_manager.cached: diff --git a/core/lib/Drupal/Core/EventSubscriber/RouterRebuildSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/RouterRebuildSubscriber.php index 28addb8..d432656 100644 --- a/core/lib/Drupal/Core/EventSubscriber/RouterRebuildSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/RouterRebuildSubscriber.php @@ -7,11 +7,12 @@ namespace Drupal\Core\EventSubscriber; -use Drupal\Core\Cache\CacheBackendInterface; +use Drupal\Core\Cache\Cache; 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; /** @@ -25,33 +26,34 @@ class RouterRebuildSubscriber implements EventSubscriberInterface { protected $routeBuilder; /** - * @var \Drupal\Core\Cache\CacheBackendInterface - */ - protected $cacheBackend; - - /** + * Constructs the RouterRebuildSubscriber object. + * * @param \Drupal\Core\Routing\RouteBuilderInterface $route_builder * The route builder. - * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend */ - public function __construct(RouteBuilderInterface $route_builder, CacheBackendInterface $cache_backend) { + public function __construct(RouteBuilderInterface $route_builder) { $this->routeBuilder = $route_builder; - $this->cacheBackend = $cache_backend; } /** * Rebuilds routers if necessary. * - * @param \Symfony\Component\EventDispatcher\Event $event + * @param \Symfony\Component\HttpKernel\Event\PostResponseEvent $event * The event object. */ - public function onKernelTerminate(Event $event) { + public function onKernelTerminate(PostResponseEvent $event) { $this->routeBuilder->rebuildIfNeeded(); } + /** + * Rebuilds the menu_router and deletes the local_task cache tag. + * + * @param \Symfony\Component\EventDispatcher\Event $event + * The event object. + */ public function onRouterRebuild(Event $event) { menu_router_rebuild(); - $this->cacheBackend->deleteTags(array('local_task' => 1)); + Cache::deleteTags(array('local_task' => 1)); } /** diff --git a/core/lib/Drupal/Core/Routing/RouteBuilderInterface.php b/core/lib/Drupal/Core/Routing/RouteBuilderInterface.php index 116ea27..2c9ef24 100644 --- a/core/lib/Drupal/Core/Routing/RouteBuilderInterface.php +++ b/core/lib/Drupal/Core/Routing/RouteBuilderInterface.php @@ -28,7 +28,7 @@ public function rebuild(); public function rebuildIfNeeded(); /** - * Set the router to be rebuilt next next it is needed. + * Sets the router to be rebuilt next time rebuildIfNeeded() is called. */ public function setRebuildNeeded(); diff --git a/core/lib/Drupal/Core/Routing/RouteBuilderStatic.php b/core/lib/Drupal/Core/Routing/RouteBuilderStatic.php index 9c81ffa..327621e 100644 --- a/core/lib/Drupal/Core/Routing/RouteBuilderStatic.php +++ b/core/lib/Drupal/Core/Routing/RouteBuilderStatic.php @@ -13,6 +13,13 @@ class RouteBuilderStatic implements RouteBuilderInterface { /** + * Marks a rebuild as being necessary. + * + * @var bool + */ + protected $rebuildNeeded = FALSE; + + /** * @inheritdoc */ public function rebuild() { @@ -24,17 +31,18 @@ public function rebuild() { * @inheritdoc */ public function rebuildIfNeeded(){ - // @todo Add the route for the batch pages when that conversion happens, - // http://drupal.org/node/1987816. + if ($this->rebuildNeeded && $this->rebuild()) { + $this->rebuildNeeded = FALSE; + return TRUE; + } + return FALSE; } /** * @inheritdoc */ public function setRebuildNeeded() { - // @todo Add the route for the batch pages when that conversion happens, - // http://drupal.org/node/1987816. - + $this->rebuildNeeded = TRUE; } } diff --git a/core/lib/Drupal/Core/Routing/RouteProvider.php b/core/lib/Drupal/Core/Routing/RouteProvider.php index 43aa271..20b0ec1 100644 --- a/core/lib/Drupal/Core/Routing/RouteProvider.php +++ b/core/lib/Drupal/Core/Routing/RouteProvider.php @@ -288,7 +288,7 @@ public function getAllRoutes() { /** * {@inheritdoc} */ - public function resetStaticCache() { + public function reset() { $this->routes = array(); } @@ -296,7 +296,7 @@ public function resetStaticCache() { * {@inheritdoc} */ static function getSubscribedEvents() { - $events[RoutingEvents::FINISHED][] = array('resetStaticCache'); + $events[RoutingEvents::FINISHED][] = array('reset'); return $events; } diff --git a/core/lib/Drupal/Core/Routing/RouteProviderInterface.php b/core/lib/Drupal/Core/Routing/RouteProviderInterface.php index 2a02277..8c8e356 100644 --- a/core/lib/Drupal/Core/Routing/RouteProviderInterface.php +++ b/core/lib/Drupal/Core/Routing/RouteProviderInterface.php @@ -41,8 +41,8 @@ public function getRoutesByPattern($pattern); public function getAllRoutes(); /** - * Resets the static route cache. + * Resets the route provider object. */ - public function resetStaticCache(); + public function reset(); } diff --git a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php index 9744276..560afb1 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php @@ -790,11 +790,6 @@ public function run(array $methods = array()) { ); $completion_check_id = TestBase::insertAssert($this->testId, $class, FALSE, t('The test did not complete due to a fatal error.'), 'Completion check', $caller); $this->setUp(); - // If set up is broken we can get here without a container. - // @see \Drupal\simpletest\Tests\BrokenSetUpTest - if ($this->container instanceOf ContainerBuilder && $this->container->has('router.builder')) { - $this->container->get('router.builder')->rebuildIfNeeded(); - } if ($this->setup) { try { $this->$method(); diff --git a/core/modules/system/lib/Drupal/system/Tests/Menu/RebuildTest.php b/core/modules/system/lib/Drupal/system/Tests/Menu/RebuildTest.php index bf1464e..e02c9c4 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Menu/RebuildTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Menu/RebuildTest.php @@ -11,7 +11,7 @@ use Drupal\simpletest\WebTestBase; /** - * Tests rebuilding the menu by setting 'menu_rebuild_needed.' + * Tests rebuilding the router. */ class RebuildTest extends WebTestBase { public static function getInfo() { @@ -23,9 +23,9 @@ public static function getInfo() { } /** - * Test if the 'menu_rebuild_needed' variable triggers a menu_rebuild() call. + * Tests that set a router rebuild needed works. */ - function testMenuRebuildByVariable() { + function testMenuRebuild() { // Check if 'admin' path exists. $admin_exists = db_query('SELECT path from {menu_router} WHERE path = :path', array(':path' => 'admin'))->fetchField(); $this->assertEqual($admin_exists, 'admin', "The path 'admin/' exists prior to deleting."); @@ -37,9 +37,8 @@ function testMenuRebuildByVariable() { $admin_exists = db_query('SELECT path from {menu_router} WHERE path = :path', array(':path' => 'admin'))->fetchField(); $this->assertFalse($admin_exists, "The path 'admin/' has been deleted and doesn't exist in the database."); - // Now we set the rebuild state flag and send a request to rebuild the menu - // item. Now 'admin' should exist. - \Drupal::state()->set(RouteBuilderInterface::REBUILD_NEEDED, TRUE); + // Now we set the router to be rebuilt. After the rebuild 'admin' should exist. + \Drupal::service('router.builder')->setRebuildNeeded(); // The request should trigger the rebuild. $this->drupalGet(''); diff --git a/core/modules/system/lib/Drupal/system/Tests/Routing/MockRouteProvider.php b/core/modules/system/lib/Drupal/system/Tests/Routing/MockRouteProvider.php index 20b9bef..4a6ece5 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Routing/MockRouteProvider.php +++ b/core/modules/system/lib/Drupal/system/Tests/Routing/MockRouteProvider.php @@ -85,7 +85,7 @@ public function getAllRoutes() { /** * @inheritdoc} */ - public function resetStaticCache() { + public function reset() { $this->routes = array(); } diff --git a/core/tests/Drupal/Tests/Core/Routing/RouteBuilderTest.php b/core/tests/Drupal/Tests/Core/Routing/RouteBuilderTest.php index dd15f61..9e05a5c 100644 --- a/core/tests/Drupal/Tests/Core/Routing/RouteBuilderTest.php +++ b/core/tests/Drupal/Tests/Core/Routing/RouteBuilderTest.php @@ -267,7 +267,7 @@ public function testRebuildWithProviderBasedRoutes() { } /** - * Tests that the route rebuilding if necessary works. + * Tests \Drupal\Core\Routing\RouteBuilder::rebuildIfNeeded() method. */ public function testRebuildIfNecessary() { $this->lock->expects($this->once())