Преглед на файлове

improves the exception message, and removes unnecessary constraint to only allow strings inside strings

Johannes Schmitt преди 14 години
родител
ревизия
2c224ce42b

+ 6 - 3
src/Symfony/Component/DependencyInjection/ParameterBag/ParameterBag.php

@@ -195,7 +195,7 @@ class ParameterBag implements ParameterBagInterface
 
 
         $self = $this;
         $self = $this;
 
 
-        return preg_replace_callback('/(?<!%)%([^%]+)%/', function ($match) use ($self, $resolving) {
+        return preg_replace_callback('/(?<!%)%([^%]+)%/', function ($match) use ($self, $resolving, $value) {
             $key = strtolower($match[1]);
             $key = strtolower($match[1]);
             if (isset($resolving[$key])) {
             if (isset($resolving[$key])) {
                 throw new ParameterCircularReferenceException(array_keys($resolving));
                 throw new ParameterCircularReferenceException(array_keys($resolving));
@@ -203,10 +203,13 @@ class ParameterBag implements ParameterBagInterface
 
 
             $resolved = $self->get($key);
             $resolved = $self->get($key);
 
 
-            if (!is_string($resolved)) {
-                throw new RuntimeException('A parameter cannot contain a non-string parameter.');
+            if (!is_string($resolved) && !is_numeric($resolved)) {
+                $resolvingKeys = array_keys($resolving);
+
+                throw new RuntimeException(sprintf('A string value must be composed of strings and/or numbers, but found parameter "%s" of type %s inside string value "%s".', $key, gettype($resolved), $value));
             }
             }
 
 
+            $resolved = (string) $resolved;
             $resolving[$key] = true;
             $resolving[$key] = true;
 
 
             return $self->isResolved() ? $resolved : $self->resolveString($resolved, $resolving);
             return $self->isResolved() ? $resolved : $self->resolveString($resolved, $resolving);

+ 4 - 1
tests/Symfony/Tests/Component/DependencyInjection/ParameterBag/ParameterBagTest.php

@@ -120,7 +120,7 @@ class ParameterBagTest extends \PHPUnit_Framework_TestCase
             $bag->resolveValue('%foo%');
             $bag->resolveValue('%foo%');
             $this->fail('->resolveValue() throws a RuntimeException when a parameter embeds another non-string parameter');
             $this->fail('->resolveValue() throws a RuntimeException when a parameter embeds another non-string parameter');
         } catch (RuntimeException $e) {
         } catch (RuntimeException $e) {
-            $this->assertEquals('A parameter cannot contain a non-string parameter.', $e->getMessage(), '->resolveValue() throws a RuntimeException when a parameter embeds another non-string parameter');
+            $this->assertEquals('A string value must be composed of strings and/or numbers, but found parameter "bar" of type array inside string value "a %bar%".', $e->getMessage(), '->resolveValue() throws a RuntimeException when a parameter embeds another non-string parameter');
         }
         }
 
 
         $bag = new ParameterBag(array('foo' => '%bar%', 'bar' => '%foobar%', 'foobar' => '%foo%'));
         $bag = new ParameterBag(array('foo' => '%bar%', 'bar' => '%foobar%', 'foobar' => '%foo%'));
@@ -138,6 +138,9 @@ class ParameterBagTest extends \PHPUnit_Framework_TestCase
         } catch (ParameterCircularReferenceException $e) {
         } catch (ParameterCircularReferenceException $e) {
             $this->assertEquals('Circular reference detected for parameter "foo" ("foo" > "bar" > "foobar" > "foo").', $e->getMessage(), '->resolveValue() throws a ParameterCircularReferenceException when a parameter has a circular reference');
             $this->assertEquals('Circular reference detected for parameter "foo" ("foo" > "bar" > "foobar" > "foo").', $e->getMessage(), '->resolveValue() throws a ParameterCircularReferenceException when a parameter has a circular reference');
         }
         }
+
+        $bag = new ParameterBag(array('host' => 'foo.bar', 'port' => 1337));
+        $this->assertEquals('foo.bar:1337', $bag->resolveValue('%host%:%port%'));
     }
     }
 
 
     /**
     /**