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

Commit401a026

Browse files
[DI] Added an env expression language function.
1 parent68da357 commit401a026

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

‎src/Symfony/Component/DependencyInjection/Container.php‎

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

1212
namespaceSymfony\Component\DependencyInjection;
1313

14+
useSymfony\Component\DependencyInjection\Exception\EnvironmentVariableNotFoundException;
1415
useSymfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1516
useSymfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
1617
useSymfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
@@ -373,6 +374,23 @@ public static function underscore($id)
373374
returnstrtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/','/([a-z\d])([A-Z])/'),array('\\1_\\2','\\1_\\2'),str_replace('_','.',$id)));
374375
}
375376

377+
protectedfunctiongetEnvironmentVariable($name,$default =null)
378+
{
379+
if (isset($_ENV[$name])) {
380+
return$_ENV[$name];
381+
}
382+
383+
if (false !==$value =getenv($name)) {
384+
return$value;
385+
}
386+
387+
if (2 >func_num_args()) {
388+
thrownewEnvironmentVariableNotFoundException($name);
389+
}
390+
391+
return$default;
392+
}
393+
376394
privatefunction__clone()
377395
{
378396
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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\DependencyInjection\Exception;
13+
14+
/**
15+
* This exception is thrown when an environment variable was not found.
16+
*
17+
* @author Magnus Nordlander <magnus@fervo.se>
18+
*/
19+
class EnvironmentVariableNotFoundExceptionextends InvalidArgumentException
20+
{
21+
publicfunction__construct($name)
22+
{
23+
parent::__construct(sprintf('You have requested a non-existent environment variable "%s".',$name));
24+
}
25+
}

‎src/Symfony/Component/DependencyInjection/ExpressionLanguageProvider.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*
2020
* To get a service, use service('request').
2121
* To get a parameter, use parameter('kernel.debug').
22+
* To get an environment variable, use env('UID').
2223
*
2324
* @author Fabien Potencier <fabien@symfony.com>
2425
*/
@@ -38,6 +39,20 @@ public function getFunctions()
3839
},function (array$variables,$value) {
3940
return$variables['container']->getParameter($value);
4041
}),
42+
43+
newExpressionFunction('env',function ($arg,$default =null) {
44+
if (2 >func_num_args()) {
45+
returnsprintf('$this->getEnvironmentVariable(%s)',$arg);
46+
}
47+
48+
returnsprintf('$this->getEnvironmentVariable(%s, %s)',$arg,$default);
49+
}, \Closure::bind(function (array$variables,$value,$default =null) {
50+
if (3 >func_num_args()) {
51+
return$variables['container']->getEnvironmentVariable($value);
52+
}
53+
54+
return$variables['container']->getEnvironmentVariable($value,$default);
55+
},null, Container::class)),
4156
);
4257
}
4358
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp