class RouteBuilder
Same name in this branch
- main core/lib/Drupal/Core/ProxyClass/Routing/RouteBuilder.php \Drupal\Core\ProxyClass\Routing\RouteBuilder
Same name and namespace in other branches
- 11.x core/lib/Drupal/Core/ProxyClass/Routing/RouteBuilder.php \Drupal\Core\ProxyClass\Routing\RouteBuilder
- 11.x core/lib/Drupal/Core/Routing/RouteBuilder.php \Drupal\Core\Routing\RouteBuilder
- 10 core/lib/Drupal/Core/ProxyClass/Routing/RouteBuilder.php \Drupal\Core\ProxyClass\Routing\RouteBuilder
- 10 core/lib/Drupal/Core/Routing/RouteBuilder.php \Drupal\Core\Routing\RouteBuilder
- 9 core/lib/Drupal/Core/ProxyClass/Routing/RouteBuilder.php \Drupal\Core\ProxyClass\Routing\RouteBuilder
- 9 core/lib/Drupal/Core/Routing/RouteBuilder.php \Drupal\Core\Routing\RouteBuilder
- 8.9.x core/lib/Drupal/Core/ProxyClass/Routing/RouteBuilder.php \Drupal\Core\ProxyClass\Routing\RouteBuilder
- 8.9.x core/lib/Drupal/Core/Routing/RouteBuilder.php \Drupal\Core\Routing\RouteBuilder
Managing class for rebuilding the router table.
Deprecated service properties:
@property \Drupal\Core\Controller\ControllerResolverInterface $controllerResolver @property \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
Hierarchy
- class \Drupal\Core\Routing\RouteBuilder implements \Drupal\Core\Routing\RouteBuilderInterface, \Drupal\Core\DestructableInterface uses \Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait
Expanded class hierarchy of RouteBuilder
2 files declare their use of RouteBuilder
- InstallerRouteBuilder.php in core/
lib/ Drupal/ Core/ Installer/ InstallerRouteBuilder.php - RouteBuilderTest.php in core/
tests/ Drupal/ Tests/ Core/ Routing/ RouteBuilderTest.php
1 string reference to 'RouteBuilder'
- core.services.yml in core/
core.services.yml - core/core.services.yml
1 service uses RouteBuilder
File
-
core/
lib/ Drupal/ Core/ Routing/ RouteBuilder.php, line 23
Namespace
Drupal\Core\RoutingView source
class RouteBuilder implements RouteBuilderInterface, DestructableInterface {
use DeprecatedServicePropertyTrait;
/**
* The dumper to which we should send collected routes.
*
* @var \Drupal\Core\Routing\MatcherDumperInterface
*/
protected $dumper;
/**
* The used lock backend instance.
*
* @var \Drupal\Core\Lock\LockBackendInterface
*/
protected $lock;
/**
* The event dispatcher to notify of routes.
*
* @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface
*/
protected $dispatcher;
/**
* The route collection during the rebuild.
*
* @var \Symfony\Component\Routing\RouteCollection
*/
protected $routeCollection;
/**
* Flag that indicates if we are currently rebuilding the routes.
*
* @var bool
*/
protected $building = FALSE;
/**
* Flag that indicates if we should rebuild at the end of the request.
*
* @var bool
*/
protected $rebuildNeeded = FALSE;
/**
* The check provider.
*
* @var \Drupal\Core\Access\CheckProviderInterface
*/
protected $checkProvider;
/**
* Deprecated service properties.
*
* @var string[]
*/
protected array $deprecatedProperties = [
'controllerResolver' => 'controller_resolver',
'moduleHandler' => 'module_handler',
];
/**
* Constructs the RouteBuilder using the passed MatcherDumperInterface.
*/
public function __construct(MatcherDumperInterface $dumper, LockBackendInterface $lock, EventDispatcherInterface $dispatcher, CheckProviderInterface|ModuleHandlerInterface $check_provider) {
$this->dumper = $dumper;
$this->lock = $lock;
$this->dispatcher = $dispatcher;
if ($check_provider instanceof ModuleHandlerInterface && count(func_get_args()) === 6) {
$check_provider = func_get_arg(5);
@trigger_error('Calling ' . __METHOD__ . '() with the module handler and controller resolver services is deprecated in drupal:11.4.0 and will be removed in drupal:12.0.0. See https://www.drupal.org/node/3324751', E_USER_DEPRECATED);
}
if (!$check_provider instanceof CheckProviderInterface) {
throw new \InvalidArgumentException();
}
$this->checkProvider = $check_provider;
}
/**
* {@inheritdoc}
*/
public function setRebuildNeeded() {
$this->rebuildNeeded = TRUE;
}
/**
* {@inheritdoc}
*/
public function rebuild() {
if ($this->building) {
throw new \RuntimeException('Recursive router rebuild detected.');
}
if (!$this->lock
->acquire('router_rebuild')) {
// Wait for another request that is already doing this work.
// We choose to block here since otherwise the routes might not be
// available, resulting in a 404.
$this->lock
->wait('router_rebuild');
return FALSE;
}
$this->building = TRUE;
$collection = new RouteCollection();
// STATIC is supposed to be used to add new routes based static information
// like routing.yml files or PHP attributes.
$this->dispatcher
->dispatch(new RouteBuildEvent($collection), RoutingEvents::STATIC);
// DYNAMIC is supposed to be used to add new routes based upon all the
// static defined ones.
$this->dispatcher
->dispatch(new RouteBuildEvent($collection), RoutingEvents::DYNAMIC);
// ALTER is the final step to alter all the existing routes. We cannot stop
// people from adding new routes here, but we define it as a separate step
// to make it clear.
$this->dispatcher
->dispatch(new RouteBuildEvent($collection), RoutingEvents::ALTER);
$this->checkProvider
->setChecks($collection);
$this->dumper
->addRoutes($collection);
$this->dumper
->dump();
$this->lock
->release('router_rebuild');
$this->dispatcher
->dispatch(new Event(), RoutingEvents::FINISHED);
$this->building = FALSE;
$this->rebuildNeeded = FALSE;
return TRUE;
}
/**
* {@inheritdoc}
*/
public function rebuildIfNeeded() {
if ($this->rebuildNeeded) {
return $this->rebuild();
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function destruct() {
// Rebuild routes only once at the end of the request lifecycle to not
// trigger multiple rebuilds and also make the page more responsive for the
// user.
$this->rebuildIfNeeded();
}
/**
* Retrieves all defined routes from .routing.yml files.
*
* @return array
* The defined routes, keyed by provider.
*
* @deprecated in drupal:11.4.0 and is removed from drupal:12.0.0. This code
* has moved to \Drupal\Core\Routing\YamlRouteDiscovery.
*
* @see https://www.drupal.org/node/3324758
*/
protected function getRouteDefinitions() {
@trigger_error(__METHOD__ . ' is deprecated in drupal:11.4.0 and is removed from drupal:12.0.0. This code has moved to \\Drupal\\Core\\Routing\\YamlRouteDiscovery. See https://www.drupal.org/node/3324758', E_USER_DEPRECATED);
// Always instantiate a new YamlDiscovery object so that we always search on
// the up-to-date list of modules.
$discovery = new YamlDiscovery('routing', $this->moduleHandler
->getModuleDirectories());
return $discovery->findAll();
}
}
Members
| Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title | Overrides |
|---|---|---|---|---|---|---|
| RouteBuilder::$building | protected | property | Flag that indicates if we are currently rebuilding the routes. | |||
| RouteBuilder::$checkProvider | protected | property | The check provider. | |||
| RouteBuilder::$dispatcher | protected | property | The event dispatcher to notify of routes. | |||
| RouteBuilder::$dumper | protected | property | The dumper to which we should send collected routes. | |||
| RouteBuilder::$lock | protected | property | The used lock backend instance. | |||
| RouteBuilder::$rebuildNeeded | protected | property | Flag that indicates if we should rebuild at the end of the request. | |||
| RouteBuilder::$routeCollection | protected | property | The route collection during the rebuild. | |||
| RouteBuilder::destruct | public | function | Overrides DestructableInterface::destruct | |||
| RouteBuilder::getRouteDefinitions | Deprecated | protected | function | Retrieves all defined routes from .routing.yml files. | 1 | |
| RouteBuilder::rebuild | public | function | Overrides RouteBuilderInterface::rebuild | |||
| RouteBuilder::rebuildIfNeeded | public | function | Overrides RouteBuilderInterface::rebuildIfNeeded | |||
| RouteBuilder::setRebuildNeeded | public | function | Overrides RouteBuilderInterface::setRebuildNeeded | |||
| RouteBuilder::__construct | public | function | Constructs the RouteBuilder using the passed MatcherDumperInterface. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.