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

Commit2fb77e3

Browse files
committed
[DependencyInjection] added a way to reference dynamic environment variables in service definitions
1 parenta15c9eb commit2fb77e3

File tree

12 files changed

+99
-8
lines changed

12 files changed

+99
-8
lines changed

‎src/Symfony/Component/DependencyInjection/Dumper/GraphvizDumper.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
useSymfony\Component\DependencyInjection\Definition;
1515
useSymfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
16+
useSymfony\Component\DependencyInjection\EnvVariable;
1617
useSymfony\Component\DependencyInjection\Reference;
1718
useSymfony\Component\DependencyInjection\Parameter;
1819
useSymfony\Component\DependencyInjection\ContainerInterface;
@@ -136,6 +137,8 @@ private function findEdges($id, $arguments, $required, $name)
136137
foreach ($argumentsas$argument) {
137138
if ($argumentinstanceof Parameter) {
138139
$argument =$this->container->hasParameter($argument) ?$this->container->getParameter($argument) :null;
140+
}elseif ($argumentinstanceof EnvVariable) {
141+
$argument ='$'.$argument.'$';
139142
}elseif (is_string($argument) &&preg_match('/^%([^%]+)%$/',$argument,$match)) {
140143
$argument =$this->container->hasParameter($match[1]) ?$this->container->getParameter($match[1]) :null;
141144
}

‎src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
useSymfony\Component\DependencyInjection\ContainerInterface;
1919
useSymfony\Component\DependencyInjection\Reference;
2020
useSymfony\Component\DependencyInjection\Parameter;
21+
useSymfony\Component\DependencyInjection\EnvVariable;
2122
useSymfony\Component\DependencyInjection\Exception\InvalidArgumentException;
2223
useSymfony\Component\DependencyInjection\Exception\RuntimeException;
2324
useSymfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
@@ -1370,13 +1371,17 @@ private function dumpValue($value, $interpolate = true)
13701371
return$this->getServiceCall((string)$value,$value);
13711372
}elseif ($valueinstanceof Expression) {
13721373
return$this->getExpressionLanguage()->compile((string)$value,array('this' =>'container'));
1374+
}elseif ($valueinstanceof EnvVariable) {
1375+
return$this->dumpEnvVariable($value);
13731376
}elseif ($valueinstanceof Parameter) {
13741377
return$this->dumpParameter($value);
13751378
}elseif (true ===$interpolate &&is_string($value)) {
13761379
if (preg_match('/^%([^%]+)%$/',$value,$match)) {
13771380
// we do this to deal with non string values (Boolean, integer, ...)
13781381
// the preg_replace_callback converts them to strings
13791382
return$this->dumpParameter(strtolower($match[1]));
1383+
}elseif (preg_match('/^\$([^\$]+)\$$/',$value,$match)) {
1384+
return$this->dumpEnvVariable($match[1]);
13801385
}else {
13811386
$that =$this;
13821387
$replaceParameters =function ($match)use ($that) {
@@ -1422,6 +1427,11 @@ public function dumpParameter($name)
14221427
returnsprintf("\$this->getParameter('%s')",strtolower($name));
14231428
}
14241429

1430+
privatefunctiondumpEnvVariable($name)
1431+
{
1432+
returnsprintf("getenv('%s')",$name);
1433+
}
1434+
14251435
/**
14261436
* @deprecated since version 2.6.2, to be removed in 3.0.
14271437
* Use \Symfony\Component\DependencyInjection\ContainerBuilder::addExpressionLanguageProvider instead.

‎src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php‎

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

1414
useSymfony\Component\DependencyInjection\ContainerInterface;
15+
useSymfony\Component\DependencyInjection\EnvVariable;
1516
useSymfony\Component\DependencyInjection\Parameter;
1617
useSymfony\Component\DependencyInjection\Reference;
1718
useSymfony\Component\DependencyInjection\Definition;
@@ -360,6 +361,8 @@ public static function phpToXml($value)
360361
return'false';
361362
case$valueinstanceof Parameter:
362363
return'%'.$value.'%';
364+
case$valueinstanceof EnvVariable:
365+
return'$'.$value.'$';
363366
caseis_object($value) ||is_resource($value):
364367
thrownewRuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
365368
default:

‎src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
useSymfony\Component\DependencyInjection\Alias;
1616
useSymfony\Component\DependencyInjection\ContainerInterface;
1717
useSymfony\Component\DependencyInjection\Definition;
18+
useSymfony\Component\DependencyInjection\EnvVariable;
1819
useSymfony\Component\DependencyInjection\Parameter;
1920
useSymfony\Component\DependencyInjection\Reference;
2021
useSymfony\Component\DependencyInjection\Exception\RuntimeException;
@@ -262,6 +263,8 @@ private function dumpValue($value)
262263
return$code;
263264
}elseif ($valueinstanceof Reference) {
264265
return$this->getServiceCall((string)$value,$value);
266+
}elseif ($valueinstanceof EnvVariable) {
267+
returnsprintf('$%s$', (string)$value);
265268
}elseif ($valueinstanceof Parameter) {
266269
return$this->getParameterCall((string)$value);
267270
}elseif ($valueinstanceof Expression) {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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;
13+
14+
/**
15+
* Represents an environment variable value.
16+
*
17+
* @author Fabien Potencier <fabien@symfony.com>
18+
*/
19+
class EnvVariable
20+
{
21+
private$id;
22+
23+
/**
24+
* Constructor.
25+
*
26+
* @param string $id The environment variable name
27+
*/
28+
publicfunction__construct($id)
29+
{
30+
$this->id =$id;
31+
}
32+
33+
/**
34+
* __toString.
35+
*
36+
* @return string The environment variable name
37+
*/
38+
publicfunction__toString()
39+
{
40+
return (string)$this->id;
41+
}
42+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ public function resolveString($value, array $resolving = array())
214214
return$this->resolved ?$this->get($key) :$this->resolveValue($this->get($key),$resolving);
215215
}
216216

217+
if (preg_match('/^\$([^\$\s]+)\$$/',$value,$match)) {
218+
returngetenv($match[1]);
219+
}
220+
217221
$self =$this;
218222

219223
returnpreg_replace_callback('/%%|%([^%\s]+)%/',function ($match)use ($self,$resolving,$value) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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\Tests;
13+
14+
useSymfony\Component\DependencyInjection\EnvVariable;
15+
16+
class EnvVariableTestextends \PHPUnit_Framework_TestCase
17+
{
18+
publicfunctiontestConstructor()
19+
{
20+
$ref =newEnvVariable('foo');
21+
$this->assertEquals('foo', (string)$ref,'__construct() sets the id of the env variable, which is used for the __toString() method');
22+
}
23+
}

‎src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
useSymfony\Component\DependencyInjection\ContainerInterface;
66
useSymfony\Component\DependencyInjection\ContainerBuilder;
7+
useSymfony\Component\DependencyInjection\EnvVariable;
78
useSymfony\Component\DependencyInjection\Reference;
89
useSymfony\Component\DependencyInjection\Parameter;
910
useSymfony\Component\ExpressionLanguage\Expression;
@@ -14,7 +15,7 @@
1415
->addTag('foo',array('foo' =>'foo'))
1516
->addTag('foo',array('bar' =>'bar','baz' =>'baz'))
1617
->setFactory(array('Bar\\FooClass','getInstance'))
17-
->setArguments(array('foo',newReference('foo.baz'),array('%foo%' =>'foo is %foo%','foobar' =>'%foo%'),true,newReference('service_container')))
18+
->setArguments(array('foo',newReference('foo.baz'),array('%foo%' =>'foo is %foo%','foobar' =>'%foo%','foo_env' =>newEnvVariable('FOO')),true,newReference('service_container')))
1819
->setProperties(array('foo' =>'bar','moo' =>newReference('foo.baz'),'qux' =>array('%foo%' =>'foo is %foo%','foobar' =>'%foo%')))
1920
->addMethodCall('setBar',array(newReference('bar')))
2021
->addMethodCall('initialize')
@@ -27,7 +28,7 @@
2728
;
2829
$container
2930
->register('bar','Bar\FooClass')
30-
->setArguments(array('foo',newReference('foo.baz'),newParameter('foo_bar')))
31+
->setArguments(array('foo',newReference('foo.baz'),newParameter('foo_bar'),newEnvVariable('FOO')))
3132
->setConfigurator(array(newReference('foo.baz'),'configure'))
3233
;
3334
$container

‎src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ protected function getBarService()
6464
{
6565
$a =$this->get('foo.baz');
6666

67-
$this->services['bar'] =$instance =new \Bar\FooClass('foo',$a,$this->getParameter('foo_bar'));
67+
$this->services['bar'] =$instance =new \Bar\FooClass('foo',$a,$this->getParameter('foo_bar'),getenv('FOO'));
6868

6969
$a->configure($instance);
7070

@@ -186,7 +186,7 @@ protected function getFooService()
186186
{
187187
$a =$this->get('foo.baz');
188188

189-
$this->services['foo'] =$instance = \Bar\FooClass::getInstance('foo',$a,array($this->getParameter('foo') =>'foo is'.$this->getParameter('foo').'','foobar' =>$this->getParameter('foo')),true,$this);
189+
$this->services['foo'] =$instance = \Bar\FooClass::getInstance('foo',$a,array($this->getParameter('foo') =>'foo is'.$this->getParameter('foo').'','foobar' =>$this->getParameter('foo'),'foo_env' =>getenv('FOO')),true,$this);
190190

191191
$instance->setBar($this->get('bar'));
192192
$instance->initialize();

‎src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected function getBarService()
7575
{
7676
$a =$this->get('foo.baz');
7777

78-
$this->services['bar'] =$instance =new \Bar\FooClass('foo',$a,$this->getParameter('foo_bar'));
78+
$this->services['bar'] =$instance =new \Bar\FooClass('foo',$a,$this->getParameter('foo_bar'),getenv('FOO'));
7979

8080
$a->configure($instance);
8181

@@ -187,7 +187,7 @@ protected function getFooService()
187187
{
188188
$a =$this->get('foo.baz');
189189

190-
$this->services['foo'] =$instance = \Bar\FooClass::getInstance('foo',$a,array('bar' =>'foo is bar','foobar' =>'bar'),true,$this);
190+
$this->services['foo'] =$instance = \Bar\FooClass::getInstance('foo',$a,array('bar' =>'foo is bar','foobar' =>'bar','foo_env' =>getenv('FOO')),true,$this);
191191

192192
$instance->setBar($this->get('bar'));
193193
$instance->initialize();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp