Skip to content

Commit 61646a2

Browse files
author
renanbr
committed
[Routing] Allow to inject service to expression condition
Gather routing variables into a service locator Add service() function for route condition Add missing file Fix psalm remove routing variable concept
1 parent 86e87a2 commit 61646a2

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class UnusedTagsPass implements CompilerPassInterface
7575
'routing.expression_language_provider',
7676
'routing.loader',
7777
'routing.route_loader',
78+
'routing.service',
7879
'security.authenticator.login_linker',
7980
'security.expression_language_provider',
8081
'security.remember_me_aware',

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@
180180
use Symfony\Component\RateLimiter\LimiterInterface;
181181
use Symfony\Component\RateLimiter\RateLimiterFactory;
182182
use Symfony\Component\RateLimiter\Storage\CacheStorage;
183+
use Symfony\Component\Routing\Attribute\AsRoutingService;
183184
use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader;
184185
use Symfony\Component\Routing\Loader\AnnotationFileLoader;
185186
use Symfony\Component\Security\Core\Exception\AuthenticationException;
@@ -657,6 +658,10 @@ public function load(array $configs, ContainerBuilder $container)
657658
'kernel.locale_aware',
658659
'kernel.reset',
659660
]);
661+
662+
$container->registerAttributeForAutoconfiguration(AsRoutingService::class, static function (ChildDefinition $definition, AsRoutingService $attribute): void {
663+
$definition->addTag('routing.service', (array) $attribute);
664+
});
660665
}
661666

662667
/**

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@ class_exists(WorkflowEvents::class) ? WorkflowEvents::ALIASES : []
200200
])
201201
->tag('routing.expression_language_function', ['function' => 'env'])
202202

203+
->set('container.get_routing_service', \Closure::class)
204+
->public()
205+
->factory([\Closure::class, 'fromCallable'])
206+
->args([
207+
[tagged_locator('routing.service', 'name'), 'get'],
208+
])
209+
->tag('routing.expression_language_function', ['function' => 'service'])
210+
203211
// inherit from this service to lazily access env vars
204212
->set('container.env', LazyString::class)
205213
->abstract()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Routing\Attribute;
13+
14+
#[\Attribute(\Attribute::TARGET_CLASS)]
15+
class AsRoutingService
16+
{
17+
public function __construct(
18+
public string $name,
19+
public int $priority = 0,
20+
) {
21+
}
22+
}

0 commit comments

Comments
 (0)