class UrlMatcher
Same name in this branch
- 11.1.x core/lib/Drupal/Core/Routing/UrlMatcher.php \Drupal\Core\Routing\UrlMatcher
UrlMatcher matches URL based on a set of routes.
@author Fabien Potencier <fabien@symfony.com>
Hierarchy
- class \Symfony\Component\Routing\Matcher\UrlMatcher implements \Symfony\Component\Routing\Matcher\UrlMatcherInterface, \Symfony\Component\Routing\Matcher\RequestMatcherInterface
Expanded class hierarchy of UrlMatcher
1 file declares its use of UrlMatcher
- UrlMatcher.php in core/
lib/ Drupal/ Core/ Routing/ UrlMatcher.php
File
-
vendor/
symfony/ routing/ Matcher/ UrlMatcher.php, line 29
Namespace
Symfony\Component\Routing\MatcherView source
class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface {
public const REQUIREMENT_MATCH = 0;
public const REQUIREMENT_MISMATCH = 1;
public const ROUTE_MATCH = 2;
/**
* Collects HTTP methods that would be allowed for the request.
*/
protected array $allow = [];
/**
* Collects URI schemes that would be allowed for the request.
*
* @internal
*/
protected array $allowSchemes = [];
protected ?Request $request = null;
protected ExpressionLanguage $expressionLanguage;
/**
* @var ExpressionFunctionProviderInterface[]
*/
protected array $expressionLanguageProviders = [];
public function __construct(RouteCollection $routes, RequestContext $context) {
}
public function setContext(RequestContext $context) : void {
$this->context = $context;
}
public function getContext() : RequestContext {
return $this->context;
}
public function match(string $pathinfo) : array {
$this->allow = $this->allowSchemes = [];
if ($ret = $this->matchCollection(rawurldecode($pathinfo) ?: '/', $this->routes)) {
return $ret;
}
if ('/' === $pathinfo && !$this->allow && !$this->allowSchemes) {
throw new NoConfigurationException();
}
throw 0 < \count($this->allow) ? new MethodNotAllowedException(array_unique($this->allow)) : new ResourceNotFoundException(\sprintf('No routes found for "%s".', $pathinfo));
}
public function matchRequest(Request $request) : array {
$this->request = $request;
$ret = $this->match($request->getPathInfo());
$this->request = null;
return $ret;
}
public function addExpressionLanguageProvider(ExpressionFunctionProviderInterface $provider) : void {
$this->expressionLanguageProviders[] = $provider;
}
/**
* Tries to match a URL with a set of routes.
*
* @param string $pathinfo The path info to be parsed
*
* @throws NoConfigurationException If no routing configuration could be found
* @throws ResourceNotFoundException If the resource could not be found
* @throws MethodNotAllowedException If the resource was found but the request method is not allowed
*/
protected function matchCollection(string $pathinfo, RouteCollection $routes) : array {
// HEAD and GET are equivalent as per RFC
if ('HEAD' === ($method = $this->context
->getMethod())) {
$method = 'GET';
}
$supportsTrailingSlash = 'GET' === $method && $this instanceof RedirectableUrlMatcherInterface;
$trimmedPathinfo = rtrim($pathinfo, '/') ?: '/';
foreach ($routes as $name => $route) {
$compiledRoute = $route->compile();
$staticPrefix = rtrim($compiledRoute->getStaticPrefix(), '/');
$requiredMethods = $route->getMethods();
// check the static prefix of the URL first. Only use the more expensive preg_match when it matches
if ('' !== $staticPrefix && !str_starts_with($trimmedPathinfo, $staticPrefix)) {
continue;
}
$regex = $compiledRoute->getRegex();
$pos = strrpos($regex, '$');
$hasTrailingSlash = '/' === $regex[$pos - 1];
$regex = substr_replace($regex, '/?$', $pos - $hasTrailingSlash, 1 + $hasTrailingSlash);
if (!preg_match($regex, $pathinfo, $matches)) {
continue;
}
$hasTrailingVar = $trimmedPathinfo !== $pathinfo && preg_match('#\\{[\\w\\x80-\\xFF]+\\}/?$#', $route->getPath());
if ($hasTrailingVar && ($hasTrailingSlash || null === ($m = $matches[\count($compiledRoute->getPathVariables())] ?? null) || '/' !== ($m[-1] ?? '/')) && preg_match($regex, $trimmedPathinfo, $m)) {
if ($hasTrailingSlash) {
$matches = $m;
}
else {
$hasTrailingVar = false;
}
}
$hostMatches = [];
if ($compiledRoute->getHostRegex() && !preg_match($compiledRoute->getHostRegex(), $this->context
->getHost(), $hostMatches)) {
continue;
}
$attributes = $this->getAttributes($route, $name, array_replace($matches, $hostMatches));
$status = $this->handleRouteRequirements($pathinfo, $name, $route, $attributes);
if (self::REQUIREMENT_MISMATCH === $status[0]) {
continue;
}
if ('/' !== $pathinfo && !$hasTrailingVar && $hasTrailingSlash === ($trimmedPathinfo === $pathinfo)) {
if ($supportsTrailingSlash && (!$requiredMethods || \in_array('GET', $requiredMethods, true))) {
return $this->allow = $this->allowSchemes = [];
}
continue;
}
if ($route->getSchemes() && !$route->hasScheme($this->context
->getScheme())) {
$this->allowSchemes = array_merge($this->allowSchemes, $route->getSchemes());
continue;
}
if ($requiredMethods && !\in_array($method, $requiredMethods, true)) {
$this->allow = array_merge($this->allow, $requiredMethods);
continue;
}
return array_replace($attributes, $status[1] ?? []);
}
return [];
}
/**
* Returns an array of values to use as request attributes.
*
* As this method requires the Route object, it is not available
* in matchers that do not have access to the matched Route instance
* (like the PHP and Apache matcher dumpers).
*/
protected function getAttributes(Route $route, string $name, array $attributes) : array {
$defaults = $route->getDefaults();
if (isset($defaults['_canonical_route'])) {
$name = $defaults['_canonical_route'];
unset($defaults['_canonical_route']);
}
$attributes['_route'] = $name;
if ($mapping = $route->getOption('mapping')) {
$attributes['_route_mapping'] = $mapping;
}
return $this->mergeDefaults($attributes, $defaults);
}
/**
* Handles specific route requirements.
*
* @return array The first element represents the status, the second contains additional information
*/
protected function handleRouteRequirements(string $pathinfo, string $name, Route $route, array $routeParameters) : array {
// expression condition
if ($route->getCondition() && !$this->getExpressionLanguage()
->evaluate($route->getCondition(), [
'context' => $this->context,
'request' => $this->request ?: $this->createRequest($pathinfo),
'params' => $routeParameters,
])) {
return [
self::REQUIREMENT_MISMATCH,
null,
];
}
return [
self::REQUIREMENT_MATCH,
null,
];
}
/**
* Get merged default parameters.
*/
protected function mergeDefaults(array $params, array $defaults) : array {
foreach ($params as $key => $value) {
if (!\is_int($key) && null !== $value) {
$defaults[$key] = $value;
}
}
return $defaults;
}
protected function getExpressionLanguage() : ExpressionLanguage {
if (!isset($this->expressionLanguage)) {
if (!class_exists(ExpressionLanguage::class)) {
throw new \LogicException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed. Try running "composer require symfony/expression-language".');
}
$this->expressionLanguage = new ExpressionLanguage(null, $this->expressionLanguageProviders);
}
return $this->expressionLanguage;
}
/**
* @internal
*/
protected function createRequest(string $pathinfo) : ?Request {
if (!class_exists(Request::class)) {
return null;
}
return Request::create($this->context
->getScheme() . '://' . $this->context
->getHost() . $this->context
->getBaseUrl() . $pathinfo, $this->context
->getMethod(), $this->context
->getParameters(), [], [], [
'SCRIPT_FILENAME' => $this->context
->getBaseUrl(),
'SCRIPT_NAME' => $this->context
->getBaseUrl(),
]);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
UrlMatcher::$allow | protected | property | Collects HTTP methods that would be allowed for the request. | ||
UrlMatcher::$allowSchemes | protected | property | Collects URI schemes that would be allowed for the request. | ||
UrlMatcher::$expressionLanguage | protected | property | |||
UrlMatcher::$expressionLanguageProviders | protected | property | |||
UrlMatcher::$request | protected | property | |||
UrlMatcher::addExpressionLanguageProvider | public | function | |||
UrlMatcher::createRequest | protected | function | @internal | ||
UrlMatcher::getAttributes | protected | function | Returns an array of values to use as request attributes. | 1 | |
UrlMatcher::getContext | public | function | Gets the request context. | Overrides RequestContextAwareInterface::getContext | |
UrlMatcher::getExpressionLanguage | protected | function | |||
UrlMatcher::handleRouteRequirements | protected | function | Handles specific route requirements. | ||
UrlMatcher::match | public | function | Tries to match a URL path with a set of routes. | Overrides UrlMatcherInterface::match | 2 |
UrlMatcher::matchCollection | protected | function | Tries to match a URL with a set of routes. | 2 | |
UrlMatcher::matchRequest | public | function | Tries to match a request with a set of routes. | Overrides RequestMatcherInterface::matchRequest | 1 |
UrlMatcher::mergeDefaults | protected | function | Get merged default parameters. | ||
UrlMatcher::REQUIREMENT_MATCH | public | constant | |||
UrlMatcher::REQUIREMENT_MISMATCH | public | constant | |||
UrlMatcher::ROUTE_MATCH | public | constant | |||
UrlMatcher::setContext | public | function | Sets the request context. | Overrides RequestContextAwareInterface::setContext | |
UrlMatcher::__construct | public | function | 2 |