Skip to content

Commit eafc9c5

Browse files
author
renanbr
committed
Gather routing variables into a service locator
1 parent cc6df1c commit eafc9c5

File tree

8 files changed

+32
-62
lines changed

8 files changed

+32
-62
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ExpressionConditionVariableResolverPass.php

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/UnusedTagsPass.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class UnusedTagsPass implements CompilerPassInterface
7474
'routing.expression_language_provider',
7575
'routing.loader',
7676
'routing.route_loader',
77+
'routing.variable',
7778
'security.authenticator.login_linker',
7879
'security.expression_language_provider',
7980
'security.remember_me_aware',

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@
170170
use Symfony\Component\RateLimiter\LimiterInterface;
171171
use Symfony\Component\RateLimiter\RateLimiterFactory;
172172
use Symfony\Component\RateLimiter\Storage\CacheStorage;
173-
use Symfony\Component\Routing\Attribute\AsRouteExpressionConditionVariable;
173+
use Symfony\Component\Routing\Attribute\AsRoutingVariable;
174174
use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader;
175175
use Symfony\Component\Routing\Loader\AnnotationFileLoader;
176176
use Symfony\Component\Security\Core\Exception\AuthenticationException;
@@ -618,8 +618,8 @@ public function load(array $configs, ContainerBuilder $container)
618618
'kernel.reset',
619619
]);
620620

621-
$container->registerAttributeForAutoconfiguration(AsRouteExpressionConditionVariable::class, static function (ChildDefinition $definition, AsRouteExpressionConditionVariable $attribute): void {
622-
$definition->addTag('routing.expression_condition_variable', [
621+
$container->registerAttributeForAutoconfiguration(AsRoutingVariable::class, static function (ChildDefinition $definition, AsRoutingVariable $attribute): void {
622+
$definition->addTag('routing.variable', [
623623
'name' => $attribute->name,
624624
]);
625625
});

src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
service('parameter_bag')->ignoreOnInvalid(),
113113
service('logger')->ignoreOnInvalid(),
114114
param('kernel.default_locale'),
115+
tagged_locator('routing.variable', 'name'),
115116
])
116117
->call('setConfigCacheFactory', [
117118
service('config_cache_factory'),

src/Symfony/Component/Routing/Attribute/AsRouteExpressionConditionVariable.php renamed to src/Symfony/Component/Routing/Attribute/AsRoutingVariable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\Routing\Attribute;
1313

1414
#[\Attribute(\Attribute::TARGET_CLASS)]
15-
class AsRouteExpressionConditionVariable
15+
class AsRoutingVariable
1616
{
1717
public function __construct(
1818
public string $name,

src/Symfony/Component/Routing/Matcher/CompiledUrlMatcher.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Routing\Matcher;
1313

14+
use Symfony\Component\DependencyInjection\ServiceLocator;
1415
use Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherTrait;
1516
use Symfony\Component\Routing\RequestContext;
1617

@@ -23,9 +24,10 @@ class CompiledUrlMatcher extends UrlMatcher
2324
{
2425
use CompiledUrlMatcherTrait;
2526

26-
public function __construct(array $compiledRoutes, RequestContext $context)
27+
public function __construct(array $compiledRoutes, RequestContext $context, ServiceLocator $expressionVariables = null)
2728
{
2829
$this->context = $context;
2930
[$this->matchHost, $this->staticRoutes, $this->regexpList, $this->dynamicRoutes, $this->checkCondition] = $compiledRoutes;
31+
$this->expressionVariables = $expressionVariables;
3032
}
3133
}

src/Symfony/Component/Routing/Matcher/UrlMatcher.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Routing\Matcher;
1313

14+
use Symfony\Component\DependencyInjection\ServiceLocator;
1415
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
1516
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
1617
use Symfony\Component\HttpFoundation\Request;
@@ -56,15 +57,13 @@ class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface
5657
*/
5758
protected $expressionLanguageProviders = [];
5859

59-
/**
60-
* @var array<string, mixed>
61-
*/
62-
private $expressionConditionVariables = [];
60+
protected ?ServiceLocator $expressionVariables;
6361

64-
public function __construct(RouteCollection $routes, RequestContext $context)
62+
public function __construct(RouteCollection $routes, RequestContext $context, ServiceLocator $expressionVariables = null)
6563
{
6664
$this->routes = $routes;
6765
$this->context = $context;
66+
$this->expressionVariables = $expressionVariables;
6867
}
6968

7069
/**
@@ -120,11 +119,6 @@ public function addExpressionLanguageProvider(ExpressionFunctionProviderInterfac
120119
$this->expressionLanguageProviders[] = $provider;
121120
}
122121

123-
public function setExpressionConditionVariable(string $name, mixed $value): void
124-
{
125-
$this->expressionConditionVariables[$name] = $value;
126-
}
127-
128122
/**
129123
* Tries to match a URL with a set of routes.
130124
*
@@ -233,14 +227,20 @@ protected function getAttributes(Route $route, string $name, array $attributes):
233227
protected function handleRouteRequirements(string $pathinfo, string $name, Route $route): array
234228
{
235229
// expression condition
236-
if (
237-
$route->getCondition() &&
238-
!$this->getExpressionLanguage()->evaluate($route->getCondition(), array_merge($this->expressionConditionVariables, [
230+
if ($route->getCondition()) {
231+
$values = [
239232
'context' => $this->context,
240233
'request' => $this->request ?: $this->createRequest($pathinfo),
241-
]))
242-
) {
243-
return [self::REQUIREMENT_MISMATCH, null];
234+
];
235+
236+
$variables = $this->expressionVariables?->getProvidedServices() ?: [];
237+
foreach (array_keys($variables) as $variable) {
238+
$values[$variable] = $this->expressionVariables->get($variable);
239+
}
240+
241+
if (!$this->getExpressionLanguage()->evaluate($route->getCondition(), $values)) {
242+
return [self::REQUIREMENT_MISMATCH, null];
243+
}
244244
}
245245

246246
return [self::REQUIREMENT_MATCH, null];

src/Symfony/Component/Routing/Router.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Config\ConfigCacheFactoryInterface;
1717
use Symfony\Component\Config\ConfigCacheInterface;
1818
use Symfony\Component\Config\Loader\LoaderInterface;
19+
use Symfony\Component\DependencyInjection\ServiceLocator;
1920
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
2021
use Symfony\Component\HttpFoundation\Request;
2122
use Symfony\Component\Routing\Generator\CompiledUrlGenerator;
@@ -91,14 +92,17 @@ class Router implements RouterInterface, RequestMatcherInterface
9192

9293
private static ?array $cache = [];
9394

94-
public function __construct(LoaderInterface $loader, mixed $resource, array $options = [], RequestContext $context = null, LoggerInterface $logger = null, string $defaultLocale = null)
95+
private ?ServiceLocator $matcherExpressionVariables = null;
96+
97+
public function __construct(LoaderInterface $loader, mixed $resource, array $options = [], RequestContext $context = null, LoggerInterface $logger = null, string $defaultLocale = null, ServiceLocator $matcherExpressionVariables = null)
9598
{
9699
$this->loader = $loader;
97100
$this->resource = $resource;
98101
$this->logger = $logger;
99102
$this->context = $context ?? new RequestContext();
100103
$this->setOptions($options);
101104
$this->defaultLocale = $defaultLocale;
105+
$this->matcherExpressionVariables = $matcherExpressionVariables;
102106
}
103107

104108
/**
@@ -262,7 +266,7 @@ public function getMatcher(): UrlMatcherInterface|RequestMatcherInterface
262266
if ($compiled) {
263267
$routes = (new CompiledUrlMatcherDumper($routes))->getCompiledRoutes();
264268
}
265-
$this->matcher = new $this->options['matcher_class']($routes, $this->context);
269+
$this->matcher = new $this->options['matcher_class']($routes, $this->context, $this->matcherExpressionVariables);
266270
if (method_exists($this->matcher, 'addExpressionLanguageProvider')) {
267271
foreach ($this->expressionLanguageProviders as $provider) {
268272
$this->matcher->addExpressionLanguageProvider($provider);
@@ -285,7 +289,7 @@ function (ConfigCacheInterface $cache) {
285289
}
286290
);
287291

288-
return $this->matcher = new $this->options['matcher_class'](self::getCompiledRoutes($cache->getPath()), $this->context);
292+
return $this->matcher = new $this->options['matcher_class'](self::getCompiledRoutes($cache->getPath()), $this->context, $this->matcherExpressionVariables);
289293
}
290294

291295
/**

0 commit comments

Comments
 (0)