8000 merged branch stof/unescape_paramaters (PR #3260) · helmer/symfony@c9e8749 · GitHub
[go: up one dir, main page]

Skip to content

Commit c9e8749

Browse files
committed
merged branch stof/unescape_paramaters (PR symfony#3260)
Commits ------- a7b48c0 Renamed the method 8e13095 Fixed the unescaping of parameters to handle arrays 045f936 Changed the testcase to expect the unescaping only after the resolution a1b6d4c Added a failing testcase for escaped % in array parameters Discussion ---------- Unescape paramaters This fixes the unescaping of % in parameters when it is used in an array. It is a replacement for @lsmith77's work done in symfony#3241 but with a working fix this time :)
2 parents 9fa3201 + a7b48c0 commit c9e8749

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public function resolve()
139139
foreach ($this->parameters as $key => $value) {
140140
try {
141141
$value = $this->resolveValue($value);
142-
$parameters[$key] = is_string($value) ? str_replace('%%', '%', $value) : $value;
142+
$parameters[$key] = $this->unescapeValue($value);
143143
} catch (ParameterNotFoundException $e) {
144144
$e->setSourceKey($key);
145145

@@ -235,4 +235,22 @@ public function isResolved()
235235
{
236236
return $this->resolved;
237237
}
238+
239+
private function unescapeValue($value)
240+
{
241+
if (is_string($value)) {
242+
return str_replace('%%', '%', $value);
243+
}
244+
245+
if (is_array($value)) {
246+
$result = array();
247+
foreach ($value as $k => $v) {
248+
$result[$k] = $this->unescapeValue($v);
249+
}
250+
251+
return $result;
252+
}
253+
254+
return $value;
255+
}
238256
}

tests/Symfony/Tests/Component/DependencyInjection/ParameterBag/ParameterBagTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public function testResolveValue()
9494
$this->assertEquals(array('bar' => array('bar' => array('bar' => 'bar'))), $bag->resolveValue(array('%foo%' => array('%foo%' => array('%foo%' => '%foo%')))), '->resolveValue() replaces placeholders in nested arrays');
9595
$this->assertEquals('I\'m a %%foo%%', $bag->resolveValue('I\'m a %%foo%%'), '->resolveValue() supports % escaping by doubling it');
9696
$this->assertEquals('I\'m a bar %%foo bar', $bag->resolveValue('I\'m a %foo% %%foo %foo%'), '->resolveValue() supports % escaping by doubling it');
97+
$this->assertEquals(array('foo' => array('bar' => array('ding' => 'I\'m a bar %%foo %%bar'))), $bag->resolveValue(array('foo' => array('bar' => array('ding' => 'I\'m a bar %%foo %%bar')))), '->resolveValue() supports % escaping by doubling it');
9798

9899
$bag = new ParameterBag(array('foo' => true));
99100
$this->assertSame(true, $bag->resolveValue('%foo%'), '->resolveValue() replaces arguments that are just a placeholder by their value without ca C12A sting them to strings');
@@ -165,6 +166,22 @@ public function testResolveIndicatesWhyAParameterIsNeeded()
165166
}
166167
}
167168

169+
/**
170+
* @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::resolve
171+
*/
172+
public function testResolveUnespacesValue()
173+
{
174+
$bag = new ParameterBag(array(
175+
'foo' => array('bar' => array('ding' => 'I\'m a bar %%foo %%bar')),
176+
'bar' => 'I\'m a %%foo%%',
177+
));
178+
179+
$bag->resolve();
180+
181+
$this->assertEquals('I\'m a %foo%', $bag->get('bar'), '->resolveValue() supports % escaping by doubling it');
182+
$this->assertEquals(array('bar' => array('ding' => 'I\'m a bar %foo %bar')), $bag->get('foo'), '->resolveValue() supports % escaping by doubling it');
183+
}
184+
168185
/**
169186
* @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::resolve
170187
* @dataProvider stringsWithSpacesProvider

0 commit comments

Comments
 (0)
0