Ver código fonte

merged branch stof/unescape_paramaters (PR #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 #3241 but with a working fix this time :)
Fabien Potencier 13 anos atrás
pai
commit
c9e87491b4

+ 19 - 1
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->unescapeValue($value);
             } catch (ParameterNotFoundException $e) {
                 $e->setSourceKey($key);
 
@@ -235,4 +235,22 @@ class ParameterBag implements ParameterBagInterface
     {
         return $this->resolved;
     }
+
+    private function unescapeValue($value)
+    {
+        if (is_string($value)) {
+            return str_replace('%%', '%', $value);
+        }
+
+        if (is_array($value)) {
+            $result = array();
+            foreach ($value as $k => $v) {
+                $result[$k] = $this->unescapeValue($v);
+            }
+
+            return $result;
+        }
+
+        return $value;
+    }
 }

+ 17 - 0
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');
@@ -165,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