Skip to content

Commit f2bdca4

Browse files
atailouloutefabpot
authored andcommitted
[Routing][FrameworkBundle] Allow using env() in route conditions
1 parent 7c63551 commit f2bdca4

File tree

7 files changed

+31
-13
lines changed

7 files changed

+31
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ CHANGELOG
1212
* Deprecated passing a `RouteCollectionBuiler` to `MicroKernelTrait::configureRoutes()`, type-hint `RoutingConfigurator` instead
1313
* The `TemplateController` now accepts context argument
1414
* Deprecated *not* setting the "framework.router.utf8" configuration option as it will default to `true` in Symfony 6.0
15+
* Added tag `routing.expression_language_function` to define functions available in route conditions
1516

1617
5.0.0
1718
-----

Command/RouterMatchCommand.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ class RouterMatchCommand extends Command
3333
protected static $defaultName = 'router:match';
3434

3535
private $router;
36+
private $expressionLanguageProviders;
3637

37-
public function __construct(RouterInterface $router)
38+
public function __construct(RouterInterface $router, iterable $expressionLanguageProviders = [])
3839
{
3940
parent::__construct();
4041

4142
$this->router = $router;
43+
$this->expressionLanguageProviders = $expressionLanguageProviders;
4244
}
4345

4446
/**
@@ -87,6 +89,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8789
}
8890

8991
$matcher = new TraceableUrlMatcher($this->router->getRouteCollection(), $context);
92+
foreach ($this->expressionLanguageProviders as $provider) {
93+
$matcher->addExpressionLanguageProvider($provider);
94+
}
9095

9196
$traces = $matcher->getTraces($input->getArgument('path_info'));
9297

DependencyInjection/Compiler/UnusedTagsPass.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class UnusedTagsPass implements CompilerPassInterface
4949
'mime.mime_type_guesser',
5050
'monolog.logger',
5151
'proxy',
52+
'routing.expression_language_function',
5253
'routing.expression_language_provider',
5354
'routing.loader',
5455
'routing.route_loader',

Resources/config/console.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145

146146
<service id="console.command.router_match" class="Symfony\Bundle\FrameworkBundle\Command\RouterMatchCommand">
147147
<argument type="service" id="router" />
148+
<argument type="tagged_iterator" tag="routing.expression_language_provider" />
148149
<tag name="console.command" command="router:match" />
149150
</service>
150151

Resources/config/routing.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,18 @@
8686
<argument></argument> <!-- scheme -->
8787
<argument>%request_listener.http_port%</argument>
8888
<argument>%request_listener.https_port%</argument>
89+
<call method="setParameter">
90+
<argument>_functions</argument>
91+
<argument type="service" id="router.expression_language_provider" />
92+
</call>
8993
</service>
9094
<service id="Symfony\Component\Routing\RequestContext" alias="router.request_context" />
9195

96+
<service id="router.expression_language_provider" class="Symfony\Component\Routing\Matcher\ExpressionLanguageProvider">
97+
<argument type="tagged_locator" tag="routing.expression_language_function" index-by="function" />
98+
<tag name="routing.expression_language_provider" />
99+
</service>
100+
92101
<service id="router.cache_warmer" class="Symfony\Bundle\FrameworkBundle\CacheWarmer\RouterCacheWarmer">
93102
<tag name="container.service_subscriber" id="router" />
94103
<tag name="kernel.cache_warmer" />

Resources/config/secrets.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
<argument type="service" id="secrets.decryption_key" on-invalid="ignore" />
1212
</service>
1313

14-
<service id="secrets.decryption_key" parent="getenv">
15-
<argument />
14+
<service id="secrets.decryption_key" parent="container.env">
15+
<argument /><!-- the name of the env var to read -->
1616
</service>
1717

1818
<service id="secrets.local_vault" class="Symfony\Bundle\FrameworkBundle\Secrets\DotenvVault">

Resources/config/services.xml

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,19 @@
130130
</service>
131131
<service id="Symfony\Component\String\Slugger\SluggerInterface" alias="slugger" />
132132

133+
<service id="container.getenv" class="Closure">
134+
<factory class="Closure" method="fromCallable" />
135+
<argument type="collection">
136+
<argument type="service" id="service_container" />
137+
<argument>getEnv</argument>
138+
</argument>
139+
<tag name="routing.expression_language_function" function="env" />
140+
</service>
141+
133142
<!-- inherit from this service to lazily access env vars -->
134-
<service id="getenv" class="Symfony\Component\String\LazyString" abstract="true">
143+
<service id="container.env" class="Symfony\Component\String\LazyString" abstract="true">
135144
<factory class="Symfony\Component\String\LazyString" method="fromCallable" />
136-
<argument type="service">
137-
<service class="Closure">
138-
<factory class="Closure" method="fromCallable" />
139-
<argument type="collection">
140-
<argument type="service" id="service_container" />
141-
<argument>getEnv</argument>
142-
</argument>
143-
</service>
144-
</argument>
145+
<argument type="service" id="container.getenv" />
145146
</service>
146147
</services>
147148
</container>

0 commit comments

Comments
 (0)