Skip to content

Commit 3681dae

Browse files
committed
merged branch Seldaek/param_bag (PR symfony#3516)
Commits ------- a894431 [DependencyInjection] Allow parsing of parameters near escaped percent signs Discussion ---------- [DependencyInjection] Allow parsing of parameters near escaped percent signs Bug fix: yes Feature addition: no Backwards compatibility break: no (unless someone relied on the buggy behavior..) Symfony2 tests pass: yes
2 parents 8e53f79 + a894431 commit 3681dae

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,12 @@ public function resolveString($value, array $resolving = array())
212212

213213
$self = $this;
214214

215-
return preg_replace_callback('/(?<!%)%([^%\s]+)%/', function ($match) use ($self, $resolving, $value) {
215+
return preg_replace_callback('/%%|%([^%\s]+)%/', function ($match) use ($self, $resolving, $value) {
216+
// skip %%
217+
if (!isset($match[1])) {
218+
return '%%';
219+
}
220+
216221
$key = strtolower($match[1]);
217222
if (isset($resolving[$key])) {
218223
throw new ParameterCircularReferenceException(array_keys($resolving));

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ public function testResolveValue()
101101
$bag = new ParameterBag(array('foo' => null));
102102
$this->assertSame(null, $bag->resolveValue('%foo%'), '->resolveValue() replaces arguments that are just a placeholder by their value without casting them to strings');
103103

104+
$bag = new ParameterBag(array('foo' => 'bar', 'baz' => '%%%foo% %foo%%% %%foo%% %%%foo%%%'));
105+
$this->assertEquals('%%bar bar%% %%foo%% %%bar%%', $bag->resolveValue('%baz%'), '->resolveValue() replaces params placed besides escaped %');
106+
107+
$bag = new ParameterBag(array('baz' => '%%s?%%s'));
108+
$this->assertEquals('%%s?%%s', $bag->resolveValue('%baz%'), '->resolveValue() is not replacing greedily');
109+
104110
$bag = new ParameterBag(array());
105111
try {
106112
$bag->resolveValue('%foobar%');
@@ -169,7 +175,7 @@ public function testResolveIndicatesWhyAParameterIsNeeded()
169175
/**
170176
* @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::resolve
171177
*/
172-
public function testResolveUnespacesValue()
178+
public function testResolveUnescapesValue()
173179
{
174180
$bag = new ParameterBag(array(
175181
'foo' => array('bar' => array('ding' => 'I\'m a bar %%foo %%bar')),

0 commit comments

Comments
 (0)