From a1b6d4c46b43d8a798eeec5807670bf675917cb8 Mon Sep 17 00:00:00 2001 From: lsmith77 Date: Thu, 2 Feb 2012 18:09:32 +0100 Subject: [PATCH 1/4] Added a failing testcase for escaped % in array parameters --- .../DependencyInjection/ParameterBag/ParameterBagTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Symfony/Tests/Component/DependencyInjection/ParameterBag/ParameterBagTest.php b/tests/Symfony/Tests/Component/DependencyInjection/ParameterBag/ParameterBagTest.php index 1499927f8c..13908393aa 100644 --- a/tests/Symfony/Tests/Component/DependencyInjection/ParameterBag/ParameterBagTest.php +++ b/tests/Symfony/Tests/Component/DependencyInjection/ParameterBag/ParameterBagTest.php @@ -94,6 +94,7 @@ class ParameterBagTest extends \PHPUnit_Framework_TestCase $this->assertEquals(array('bar' => array('bar' => array('bar' => 'bar'))), $bag->resolveValue(array('%foo%' => array('%foo%' => array('%foo%' => '%foo%')))), '->resolveValue() replaces placeholders in nested arrays'); $this->assertEquals('I\'m a %%foo%%', $bag->resolveValue('I\'m a %%foo%%'), '->resolveValue() supports % escaping by doubling it'); $this->assertEquals('I\'m a bar %%foo bar', $bag->resolveValue('I\'m a %foo% %%foo %foo%'), '->resolveValue() supports % escaping by doubling it'); + $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'); $bag = new ParameterBag(array('foo' => true)); $this->assertSame(true, $bag->resolveValue('%foo%'), '->resolveValue() replaces arguments that are just a placeholder by their value without casting them to strings'); From 045f93603810a1c386ba717dbf4b3c02d150a847 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Thu, 2 Feb 2012 18:24:41 +0100 Subject: [PATCH 2/4] Changed the testcase to expect the unescaping only after the resolution String values are not unescaped either in resolveValue() because it can be called several times for the same parameter. --- .../ParameterBag/ParameterBagTest.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/Symfony/Tests/Component/DependencyInjection/ParameterBag/ParameterBagTest.php b/tests/Symfony/Tests/Component/DependencyInjection/ParameterBag/ParameterBagTest.php index 13908393aa..29eeb7489e 100644 --- a/tests/Symfony/Tests/Component/DependencyInjection/ParameterBag/ParameterBagTest.php +++ b/tests/Symfony/Tests/Component/DependencyInjection/ParameterBag/ParameterBagTest.php @@ -94,7 +94,7 @@ class ParameterBagTest extends \PHPUnit_Framework_TestCase $this->assertEquals(array('bar' => array('bar' => array('bar' => 'bar'))), $bag->resolveValue(array('%foo%' => array('%foo%' => array('%foo%' => '%foo%')))), '->resolveValue() replaces placeholders in nested arrays'); $this->assertEquals('I\'m a %%foo%%', $bag->resolveValue('I\'m a %%foo%%'), '->resolveValue() supports % escaping by doubling it'); $this->assertEquals('I\'m a bar %%foo bar', $bag->resolveValue('I\'m a %foo% %%foo %foo%'), '->resolveValue() supports % escaping by doubling it'); - $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'); + $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'); $bag = new ParameterBag(array('foo' => true)); $this->assertSame(true, $bag->resolveValue('%foo%'), '->resolveValue() replaces arguments that are just a placeholder by their value without casting them to strings'); @@ -166,6 +166,22 @@ class ParameterBagTest extends \PHPUnit_Framework_TestCase } } + /** + * @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::resolve + */ + public function testResolveUnespacesValue() + { + $bag = new ParameterBag(array( + 'foo' => array('bar' => array('ding' => 'I\'m a bar %%foo %%bar')), + 'bar' => 'I\'m a %%foo%%', + )); + + $bag->resolve(); + + $this->assertEquals('I\'m a %foo%', $bag->get('bar'), '->resolveValue() supports % escaping by doubling it'); + $this->assertEquals(array('bar' => array('ding' => 'I\'m a bar %foo %bar')), $bag->get('foo'), '->resolveValue() supports % escaping by doubling it'); + } + /** * @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::resolve * @dataProvider stringsWithSpacesProvider From 8e13095e5c584e3f693c3789148440411df816a0 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Thu, 2 Feb 2012 18:26:12 +0100 Subject: [PATCH 3/4] Fixed the unescaping of parameters to handle arrays --- .../ParameterBag/ParameterBag.php | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/DependencyInjection/ParameterBag/ParameterBag.php b/src/Symfony/Component/DependencyInjection/ParameterBag/ParameterBag.php index c1eb9622c5..c895888bc9 100644 --- a/src/Symfony/Component/DependencyInjection/ParameterBag/ParameterBag.php +++ b/src/Symfony/Component/DependencyInjection/ParameterBag/ParameterBag.php @@ -139,7 +139,7 @@ class ParameterBag implements ParameterBagInterface foreach ($this->parameters as $key => $value) { try { $value = $this->resolveValue($value); - $parameters[$key] = is_string($value) ? str_replace('%%', '%', $value) : $value; + $parameters[$key] = $this->unescapeString($value); } catch (ParameterNotFoundException $e) { $e->setSourceKey($key); @@ -235,4 +235,22 @@ class ParameterBag implements ParameterBagInterface { return $this->resolved; } + + private function unescapeString($value) + { + if (is_string($value)) { + return str_replace('%%', '%', $value); + } + + if (is_array($value)) { + $result = array(); + foreach ($value as $k => $v) { + $result[$k] = $this->unescapeString($v); + } + + return $result; + } + + return $value; + } } From a7b48c058b50e221d469b7c137c05bfec7118688 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Thu, 2 Feb 2012 18:59:53 +0100 Subject: [PATCH 4/4] Renamed the method --- .../DependencyInjection/ParameterBag/ParameterBag.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/ParameterBag/ParameterBag.php b/src/Symfony/Component/DependencyInjection/ParameterBag/ParameterBag.php index c895888bc9..a6162aaf06 100644 --- a/src/Symfony/Component/DependencyInjection/ParameterBag/ParameterBag.php +++ b/src/Symfony/Component/DependencyInjection/ParameterBag/ParameterBag.php @@ -139,7 +139,7 @@ class ParameterBag implements ParameterBagInterface foreach ($this->parameters as $key => $value) { try { $value = $this->resolveValue($value); - $parameters[$key] = $this->unescapeString($value); + $parameters[$key] = $this->unescapeValue($value); } catch (ParameterNotFoundException $e) { $e->setSourceKey($key); @@ -236,7 +236,7 @@ class ParameterBag implements ParameterBagInterface return $this->resolved; } - private function unescapeString($value) + private function unescapeValue($value) { if (is_string($value)) { return str_replace('%%', '%', $value); @@ -245,7 +245,7 @@ class ParameterBag implements ParameterBagInterface if (is_array($value)) { $result = array(); foreach ($value as $k => $v) { - $result[$k] = $this->unescapeString($v); + $result[$k] = $this->unescapeValue($v); } return $result;