Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit84a22c6

Browse files
committed
[Routing] Use env() in route condition
1 parentf46ab58 commit84a22c6

File tree

7 files changed

+149
-2
lines changed

7 files changed

+149
-2
lines changed

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@
6868
<argumenttype="service"id="parameter_bag"on-invalid="ignore" />
6969
<argumenttype="service"id="logger"on-invalid="ignore" />
7070
<argument>%kernel.default_locale%</argument>
71+
<argumenttype="collection">
72+
<argumenttype="service"key="get_env">
73+
<serviceclass="Closure">
74+
<factoryclass="Closure"method="fromCallable" />
75+
<argumenttype="collection">
76+
<argumenttype="service"id="service_container" />
77+
<argument>getEnv</argument>
78+
</argument>
79+
</service>
80+
</argument>
81+
</argument>
7182
<callmethod="setConfigCacheFactory">
7283
<argumenttype="service"id="config_cache_factory" />
7384
</call>
@@ -115,5 +126,10 @@
115126
<serviceid="Symfony\Bundle\FrameworkBundle\Controller\TemplateController"public="true">
116127
<argumenttype="service"id="twig"on-invalid="ignore" />
117128
</service>
129+
130+
<serviceid="Symfony\Component\Routing\Matcher\ExpressionLanguageProvider"public="false">
131+
<tagname="routing.expression_language_provider" />
132+
<argumenttype="service"id="router" />
133+
</service>
118134
</services>
119135
</container>

‎src/Symfony/Bundle/FrameworkBundle/Routing/Router.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ class Router extends BaseRouter implements WarmableInterface, ServiceSubscriberI
3838
/**
3939
* @param mixed $resource The main resource to load
4040
*/
41-
publicfunction__construct(ContainerInterface$container,$resource,array$options = [],RequestContext$context =null,ContainerInterface$parameters =null,LoggerInterface$logger =null,string$defaultLocale =null)
41+
publicfunction__construct(ContainerInterface$container,$resource,array$options = [],RequestContext$context =null,ContainerInterface$parameters =null,LoggerInterface$logger =null,string$defaultLocale =null,array$defaultParameters = [])
4242
{
4343
$this->container =$container;
4444
$this->resource =$resource;
4545
$this->context =$context ?:newRequestContext();
46+
$this->context->addParameters($defaultParameters);
4647
$this->logger =$logger;
4748
$this->setOptions($options);
4849

‎src/Symfony/Component/Routing/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* deprecated`RouteCollectionBuilder` in favor of`RoutingConfigurator`.
99
* added "priority" option to annotated routes
1010
* added argument`$priority` to`RouteCollection::add()`
11+
* added`ExpressionLanguageProvider` that provides`env` function.
1112

1213
5.0.0
1314
-----
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
namespaceSymfony\Component\Routing\Matcher;
13+
14+
useSymfony\Component\ExpressionLanguage\ExpressionFunction;
15+
useSymfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
16+
useSymfony\Component\Routing\RequestContext;
17+
useSymfony\Component\Routing\RouterInterface;
18+
19+
/**
20+
* Defines some ExpressionLanguage functions.
21+
*
22+
* @author Ahmed TAILOULOUTE <ahmed.tailouloute@gmail.com>
23+
*/
24+
class ExpressionLanguageProviderimplements ExpressionFunctionProviderInterface
25+
{
26+
/** @var RequestContext */
27+
private$context;
28+
29+
publicfunction__construct(RouterInterface$router)
30+
{
31+
$this->context =$router->getContext();
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
publicfunctiongetFunctions()
38+
{
39+
return [
40+
newExpressionFunction(
41+
'env',
42+
function ($str,$default ='null') {
43+
if (false ===$this->context->hasParameter('get_env')) {
44+
thrownew \LogicException('You cannot use function "env" as no "get_env" is not available.');
45+
}
46+
47+
returnsprintf('(($context->getParameter(\'get_env\'))(%s) ?? %s)',$str,$default);
48+
},
49+
function ($arguments,$str,$default ='null') {
50+
if (false ===$this->context->hasParameter('get_env')) {
51+
thrownew \LogicException('You cannot use function "env" as no "get_env" is not available.');
52+
}
53+
54+
return ($this->context->getParameter('get_env'))($str) ??$default;
55+
}
56+
),
57+
];
58+
}
59+
}

‎src/Symfony/Component/Routing/RequestContext.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespaceSymfony\Component\Routing;
1313

1414
useSymfony\Component\HttpFoundation\Request;
15+
useSymfony\Component\Routing\Matcher\EnvVarResolverInterface;
1516

1617
/**
1718
* Holds information about the current request.
@@ -269,6 +270,20 @@ public function setParameters(array $parameters)
269270
return$this;
270271
}
271272

273+
/**
274+
* Adds some parameters.
275+
*
276+
* @param array $parameters The parameters
277+
*
278+
* @return $this
279+
*/
280+
publicfunctionaddParameters(array$parameters)
281+
{
282+
$this->parameters =array_merge($parameters,$this->parameters);
283+
284+
return$this;
285+
}
286+
272287
/**
273288
* Gets a parameter value.
274289
*

‎src/Symfony/Component/Routing/Router.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,13 @@ class Router implements RouterInterface, RequestMatcherInterface
9797
/**
9898
* @param mixed $resource The main resource to load
9999
*/
100-
publicfunction__construct(LoaderInterface$loader,$resource,array$options = [],RequestContext$context =null,LoggerInterface$logger =null,string$defaultLocale =null)
100+
publicfunction__construct(LoaderInterface$loader,$resource,array$options = [],RequestContext$context =null,LoggerInterface$logger =null,string$defaultLocale =null,array$defaultParameters = [])
101101
{
102102
$this->loader =$loader;
103103
$this->resource =$resource;
104104
$this->logger =$logger;
105105
$this->context =$context ?:newRequestContext();
106+
$this->context->addParameters($defaultParameters);
106107
$this->setOptions($options);
107108
$this->defaultLocale =$defaultLocale;
108109
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
namespaceSymfony\Component\Routing\Tests\Matcher;
13+
14+
usePHPUnit\Framework\TestCase;
15+
useSymfony\Component\ExpressionLanguage\ExpressionLanguage;
16+
useSymfony\Component\Routing\Matcher\ExpressionLanguageProvider;
17+
18+
class ExpressionLanguageTestextends TestCase
19+
{
20+
private$envVarResolver;
21+
22+
publicfunctionsetUp()
23+
{
24+
// $this->envVarResolver = $this->getMockBuilder(EnvVarResolverInterface::class)->getMock();
25+
// $this->envVarResolver->method('getEnv')
26+
// ->willReturnMap([
27+
// ['APP_ENV', 'test'],
28+
// ['PHP_VERSION', '7.2'],
29+
// ]);
30+
}
31+
32+
/**
33+
* @dataProvider provider
34+
*/
35+
publicfunctiontestEnv(string$expression,$expected):void
36+
{
37+
$expressionLanguage =newExpressionLanguage();
38+
$expressionLanguageProvider =newExpressionLanguageProvider();
39+
$expressionLanguageProvider->setEnvVarResolver($this->envVarResolver);
40+
$expressionLanguage->registerProvider($expressionLanguageProvider);
41+
42+
$this->assertEquals($expected,$expressionLanguage->evaluate($expression));
43+
}
44+
45+
publicfunctionprovider():array
46+
{
47+
return [
48+
['env("APP_ENV")','test'],
49+
['env("PHP_VERSION")','7.2'],
50+
['env("unknown_env_variable")',null],
51+
['env("unknown_env_variable", "default")','default'],
52+
];
53+
}
54+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp