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

Appearance settings

Commit 6bad580

Browse filesBrowse files
committed
[DependencyInjection] moved ContainerBuilder::resolveValue() to ParameterBag
1 parent 7796eb2 commit 6bad580
Copy full SHA for 6bad580

File tree

Expand file treeCollapse file tree

5 files changed

+88
-91
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+88
-91
lines changed

‎src/Symfony/Components/DependencyInjection/ContainerBuilder.php

Copy file name to clipboardExpand all lines: src/Symfony/Components/DependencyInjection/ContainerBuilder.php
+8-52Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public function commit()
234234
$this->parameterBag->add($parameters);
235235

236236
foreach ($this->parameterBag->all() as $key => $value) {
237-
$this->parameterBag->set($key, self::resolveValue($value, $this->getParameterBag()->all()));
237+
$this->parameterBag->set($key, $this->getParameterBag()->resolveValue($value));
238238
}
239239
}
240240

@@ -449,21 +449,21 @@ public function findDefinition($id)
449449
protected function createService(Definition $definition, $id)
450450
{
451451
if (null !== $definition->getFile()) {
452-
require_once self::resolveValue($definition->getFile(), $this->getParameterBag()->all());
452+
require_once $this->getParameterBag()->resolveValue($definition->getFile());
453453
}
454454

455-
$arguments = $this->resolveServices(self::resolveValue($definition->getArguments(), $this->getParameterBag()->all()));
455+
$arguments = $this->resolveServices($this->getParameterBag()->resolveValue($definition->getArguments()));
456456

457457
if (null !== $definition->getFactoryMethod()) {
458458
if (null !== $definition->getFactoryService()) {
459-
$factory = $this->get(self::resolveValue($definition->getFactoryService(), $this->getParameterBag()->all()));
459+
$factory = $this->get($this->getParameterBag()->resolveValue($definition->getFactoryService()));
460460
} else {
461-
$factory = self::resolveValue($definition->getClass(), $this->getParameterBag()->all());
461+
$factory = $this->getParameterBag()->resolveValue($definition->getClass());
462462
}
463463

464464
$service = call_user_func_array(array($factory, $definition->getFactoryMethod()), $arguments);
465465
} else {
466-
$r = new \ReflectionClass(self::resolveValue($definition->getClass(), $this->getParameterBag()->all()));
466+
$r = new \ReflectionClass($this->getParameterBag()->resolveValue($definition->getClass()));
467467

468468
$service = null === $r->getConstructor() ? $r->newInstance() : $r->newInstanceArgs($arguments);
469469
}
@@ -484,15 +484,15 @@ protected function createService(Definition $definition, $id)
484484
}
485485

486486
if ($ok) {
487-
call_user_func_array(array($service, $call[0]), $this->resolveServices(self::resolveValue($call[1], $this->getParameterBag()->all())));
487+
call_user_func_array(array($service, $call[0]), $this->resolveServices($this->getParameterBag()->resolveValue($call[1])));
488488
}
489489
}
490490

491491
if ($callable = $definition->getConfigurator()) {
492492
if (is_array($callable) && is_object($callable[0]) && $callable[0] instanceof Reference) {
493493
$callable[0] = $this->get((string) $callable[0]);
494494
} elseif (is_array($callable)) {
495-
$callable[0] = self::resolveValue($callable[0], $this->getParameterBag()->all());
495+
$callable[0] = $this->getParameterBag()->resolveValue($callable[0]);
496496
}
497497

498498
if (!is_callable($callable)) {
@@ -505,50 +505,6 @@ protected function createService(Definition $definition, $id)
505505
return $service;
506506
}
507507

508-
/**
509-
* Replaces parameter placeholders (%name%) by their values.
510-
*
511-
* @param mixed $value A value
512-
*
513-
* @return mixed The same value with all placeholders replaced by their values
514-
*
515-
* @throws \RuntimeException if a placeholder references a parameter that does not exist
516-
*/
517-
static public function resolveValue($value, $parameters)
518-
{
519-
if (is_array($value)) {
520-
$args = array();
521-
foreach ($value as $k => $v) {
522-
$args[self::resolveValue($k, $parameters)] = self::resolveValue($v, $parameters);
523-
}
524-
525-
$value = $args;
526-
} else if (is_string($value)) {
527-
if (preg_match('/^%([^%]+)%$/', $value, $match)) {
528-
// we do this to deal with non string values (boolean, integer, ...)
529-
// the preg_replace_callback converts them to strings
530-
if (!array_key_exists($name = strtolower($match[1]), $parameters)) {
531-
throw new \RuntimeException(sprintf('The parameter "%s" must be defined.', $name));
532-
}
533-
534-
$value = $parameters[$name];
535-
} else {
536-
$replaceParameter = function ($match) use ($parameters, $value)
537-
{
538-
if (!array_key_exists($name = strtolower($match[2]), $parameters)) {
539-
throw new \RuntimeException(sprintf('The parameter "%s" must be defined (used in the following expression: "%s").', $name, $value));
540-
}
541-
542-
return $parameters[$name];
543-
};
544-
545-
$value = str_replace('%%', '%', preg_replace_callback('/(?<!%)(%)([^%]+)\1/', $replaceParameter, $value));
546-
}
547-
}
548-
549-
return $value;
550-
}
551-
552508
/**
553509
* Replaces service references by the real service instance.
554510
*

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

Copy file name to clipboardExpand all lines: src/Symfony/Components/DependencyInjection/Dumper/GraphvizDumper.php
+1-6Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ protected function findNodes()
137137
$container = clone $this->container;
138138

139139
foreach ($container->getDefinitions() as $id => $definition) {
140-
$nodes[$id] = array('class' => str_replace('\\', '\\\\', $this->getValue($definition->getClass())), 'attributes' => array_merge($this->options['node.definition'], array('style' => $definition->isShared() ? 'filled' : 'dotted')));
140+
$nodes[$id] = array('class' => str_replace('\\', '\\\\', $this->container->getParameterBag()->resolveValue($definition->getClass())), 'attributes' => array_merge($this->options['node.definition'], array('style' => $definition->isShared() ? 'filled' : 'dotted')));
141141

142142
$container->setDefinition($id, new Definition('stdClass'));
143143
}
@@ -157,11 +157,6 @@ protected function findNodes()
157157
return $nodes;
158158
}
159159

160-
protected function getValue($value, $default = '')
161-
{
162-
return ContainerBuilder::resolveValue($value, $this->container->getParameterBag()->all());
163-
}
164-
165160
protected function startDot()
166161
{
167162
$parameters = var_export($this->container->getParameterBag()->all(), true);

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

Copy file name to clipboardExpand all lines: src/Symfony/Components/DependencyInjection/ParameterBag/ParameterBag.php
+43Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,47 @@ public function has($name)
104104
{
105105
return array_key_exists(strtolower($name), $this->parameters);
106106
}
107+
108+
/**
109+
* Replaces parameter placeholders (%name%) by their values.
110+
*
111+
* @param mixed $value A value
112+
*
113+
* @throws \RuntimeException if a placeholder references a parameter that does not exist
114+
*/
115+
public function resolveValue($value)
116+
{
117+
if (is_array($value)) {
118+
$args = array();
119+
foreach ($value as $k => $v) {
120+
$args[$this->resolveValue($k)] = $this->resolveValue($v);
121+
}
122+
123+
$value = $args;
124+
} else if (is_string($value)) {
125+
if (preg_match('/^%([^%]+)%$/', $value, $match)) {
126+
// we do this to deal with non string values (boolean, integer, ...)
127+
// the preg_replace_callback converts them to strings
128+
if (!array_key_exists($name = strtolower($match[1]), $this->parameters)) {
129+
throw new \RuntimeException(sprintf('The parameter "%s" must be defined.', $name));
130+
}
131+
132+
$value = $this->parameters[$name];
133+
} else {
134+
$parameters = $this->parameters;
135+
$replaceParameter = function ($match) use ($parameters, $value)
136+
{
137+
if (!array_key_exists($name = strtolower($match[2]), $parameters)) {
138+
throw new \RuntimeException(sprintf('The parameter "%s" must be defined (used in the following expression: "%s").', $name, $value));
139+
}
140+
141+
return $parameters[$name];
142+
};
143+
144+
$value = str_replace('%%', '%', preg_replace_callback('/(?<!%)(%)([^%]+)\1/', $replaceParameter, $value));
145+
}
146+
}
147+
148+
return $value;
149+
}
107150
}

‎tests/Symfony/Tests/Components/DependencyInjection/ContainerBuilderTest.php

Copy file name to clipboardExpand all lines: tests/Symfony/Tests/Components/DependencyInjection/ContainerBuilderTest.php
-33Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -288,39 +288,6 @@ public function testCreateServiceConfigurator()
288288
}
289289
}
290290

291-
/**
292-
* @covers Symfony\Components\DependencyInjection\ContainerBuilder::resolveValue
293-
*/
294-
public function testResolveValue()
295-
{
296-
$this->assertEquals('foo', ContainerBuilder::resolveValue('foo', array()), '->resolveValue() returns its argument unmodified if no placeholders are found');
297-
$this->assertEquals('I\'m a bar', ContainerBuilder::resolveValue('I\'m a %foo%', array('foo' => 'bar')), '->resolveValue() replaces placeholders by their values');
298-
$this->assertTrue(ContainerBuilder::resolveValue('%foo%', array('foo' => true)) === true, '->resolveValue() replaces arguments that are just a placeholder by their value without casting them to strings');
299-
300-
$this->assertEquals(array('bar' => 'bar'), ContainerBuilder::resolveValue(array('%foo%' => '%foo%'), array('foo' => 'bar')), '->resolveValue() replaces placeholders in keys and values of arrays');
301-
302-
$this->assertEquals(array('bar' => array('bar' => array('bar' => 'bar'))), ContainerBuilder::resolveValue(array('%foo%' => array('%foo%' => array('%foo%' => '%foo%'))), array('foo' => 'bar')), '->resolveValue() replaces placeholders in nested arrays');
303-
304-
$this->assertEquals('I\'m a %foo%', ContainerBuilder::resolveValue('I\'m a %%foo%%', array('foo' => 'bar')), '->resolveValue() supports % escaping by doubling it');
305-
$this->assertEquals('I\'m a bar %foo bar', ContainerBuilder::resolveValue('I\'m a %foo% %%foo %foo%', array('foo' => 'bar')), '->resolveValue() supports % escaping by doubling it');
306-
307-
try {
308-
ContainerBuilder::resolveValue('%foobar%', array());
309-
$this->fail('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter');
310-
} catch (\Exception $e) {
311-
$this->assertInstanceOf('\RuntimeException', $e, '->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter');
312-
$this->assertEquals('The parameter "foobar" must be defined.', $e->getMessage(), '->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter');
313-
}
314-
315-
try {
316-
ContainerBuilder::resolveValue('foo %foobar% bar', array());
317-
$this->fail('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter');
318-
} catch (\Exception $e) {
319-
$this->assertInstanceOf('\RuntimeException', $e, '->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter');
320-
$this->assertEquals('The parameter "foobar" must be defined (used in the following expression: "foo %foobar% bar").', $e->getMessage(), '->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter');
321-
}
322-
}
323-
324291
/**
325292
* @covers Symfony\Components\DependencyInjection\ContainerBuilder::resolveServices
326293
*/

‎tests/Symfony/Tests/Components/DependencyInjection/ParameterBag/ParameterBagTest.php

Copy file name to clipboardExpand all lines: tests/Symfony/Tests/Components/DependencyInjection/ParameterBag/ParameterBagTest.php
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,40 @@ public function testHas()
7575
$this->assertTrue($bag->has('Foo'), '->has() converts the key to lowercase');
7676
$this->assertFalse($bag->has('bar'), '->has() returns false if a parameter is not defined');
7777
}
78+
79+
/**
80+
* @covers Symfony\Components\DependencyInjection\ParameterBag\ParameterBag::resolveValue
81+
*/
82+
public function testResolveValue()
83+
{
84+
$bag = new ParameterBag(array());
85+
$this->assertEquals('foo', $bag->resolveValue('foo'), '->resolveValue() returns its argument unmodified if no placeholders are found');
86+
87+
$bag = new ParameterBag(array('foo' => 'bar'));
88+
$this->assertEquals('I\'m a bar', $bag->resolveValue('I\'m a %foo%'), '->resolveValue() replaces placeholders by their values');
89+
$this->assertEquals(array('bar' => 'bar'), $bag->resolveValue(array('%foo%' => '%foo%')), '->resolveValue() replaces placeholders in keys and values of arrays');
90+
$this->assertEquals(array('bar' => array('bar' => array('bar' => 'bar'))), $bag->resolveValue(array('%foo%' => array('%foo%' => array('%foo%' => '%foo%')))), '->resolveValue() replaces placeholders in nested arrays');
91+
$this->assertEquals('I\'m a %foo%', $bag->resolveValue('I\'m a %%foo%%'), '->resolveValue() supports % escaping by doubling it');
92+
$this->assertEquals('I\'m a bar %foo bar', $bag->resolveValue('I\'m a %foo% %%foo %foo%'), '->resolveValue() supports % escaping by doubling it');
93+
94+
$bag = new ParameterBag(array('foo' => true));
95+
$this->assertTrue($bag->resolveValue('%foo%') === true, '->resolveValue() replaces arguments that are just a placeholder by their value without casting them to strings');
96+
97+
$bag = new ParameterBag(array());
98+
try {
99+
$bag->resolveValue('%foobar%', array());
100+
$this->fail('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter');
101+
} catch (\Exception $e) {
102+
$this->assertInstanceOf('\RuntimeException', $e, '->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter');
103+
$this->assertEquals('The parameter "foobar" must be defined.', $e->getMessage(), '->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter');
104+
}
105+
106+
try {
107+
$bag->resolveValue('foo %foobar% bar', array());
108+
$this->fail('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter');
109+
} catch (\Exception $e) {
110+
$this->assertInstanceOf('\RuntimeException', $e, '->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter');
111+
$this->assertEquals('The parameter "foobar" must be defined (used in the following expression: "foo %foobar% bar").', $e->getMessage(), '->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter');
112+
}
113+
}
78114
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.