File tree 4 files changed +78
-0
lines changed
Bundle/FrameworkBundle/Resources/config
4 files changed +78
-0
lines changed Original file line number Diff line number Diff line change 115
115
<service id =" Symfony\Bundle\FrameworkBundle\Controller\TemplateController" public =" true" >
116
116
<argument type =" service" id =" twig" on-invalid =" ignore" />
117
117
</service >
118
+
119
+ <service id =" Symfony\Component\Routing\Matcher\ExpressionLanguageProvider" public =" false" >
120
+ <tag name =" routing.expression_language_provider" />
121
+ </service >
118
122
</services >
119
123
</container >
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ CHANGELOG
8
8
* deprecated ` RouteCollectionBuilder ` in favor of ` RoutingConfigurator ` .
9
9
* added "priority" option to annotated routes
10
10
* added argument ` $priority ` to ` RouteCollection::add() `
11
+ * added ` ExpressionLanguageProvider ` that provides ` env ` function.
11
12
12
13
5.0.0
13
14
-----
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Symfony \Component \Routing \Matcher ;
4
+
5
+ use Symfony \Component \ExpressionLanguage \ExpressionFunction ;
6
+ use Symfony \Component \ExpressionLanguage \ExpressionFunctionProviderInterface ;
7
+
8
+ /**
9
+ * Define some ExpressionLanguage functions.
10
+ *
11
+ * @author Ahmed TAILOULOUTE <ahmed.tailouloute@gmail.com>
12
+ */
13
+ class ExpressionLanguageProvider implements ExpressionFunctionProviderInterface
14
+ {
15
+ /**
16
+ * @inheritDoc
17
+ */
18
+ public function getFunctions ()
19
+ {
20
+ return [
21
+ new ExpressionFunction (
22
+ 'env ' ,
23
+ static function ($ str ) {
24
+ return sprintf ('($_SERVER[%s] ?? null) ' , $ str );
25
+ },
26
+ static function ($ arguments , $ str ) {
27
+ return $ _SERVER [$ str ] ?? null ;
28
+ }
29
+ )
30
+ ];
31
+ }
32
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Symfony \Component \Routing \Tests \Matcher ;
4
+
5
+ use PHPUnit \Framework \TestCase ;
6
+ use Symfony \Component \ExpressionLanguage \ExpressionLanguage ;
7
+ use Symfony \Component \Routing \Matcher \ExpressionLanguageProvider ;
8
+
9
+ class ExpressionLanguageTest extends TestCase
10
+ {
11
+ public function setUp ()
12
+ {
13
+ $ _SERVER ['test__APP_ENV ' ] = 'test ' ;
14
+ $ _SERVER ['test__PHP_VERSION ' ] = '7.2 ' ;
15
+ }
16
+
17
+ /**
18
+ * @dataProvider provider
19
+ */
20
+ public function testEnv (string $ expression , $ expected ): void
21
+ {
22
+ $ expressionLanguage = new ExpressionLanguage ();
23
+ $ expressionLanguage ->registerProvider (new ExpressionLanguageProvider ());
24
+
25
+ $ this ->assertEquals ($ expected , $ expressionLanguage ->evaluate ($ expression ));
26
+ }
27
+
28
+ public function provider (): array
29
+ {
30
+ return [
31
+ ['env("test__APP_ENV") ' , 'test ' ],
32
+ ['env("test__PHP_VERSION") ' , '7.2 ' ],
33
+ ['env("test__unknown_env_variable") ' , null ],
34
+ ];
35
+ }
36
+
37
+ public function tearDown ()
38
+ {
39
+ unset($ _SERVER ['test__APP_ENV ' ], $ _SERVER ['test__PHP_VERSION ' ]);
40
+ }
41
+ }
You can’t perform that action at this time.