merged branch schmittjoh/lazyParameterBag (PR #1712)

Commits
-------

d37ff15 removed unused code
2d3051f tabs -> spaces
2c224ce improves the exception message, and removes unnecessary constraint to only allow strings inside strings
d0b056c fixes a bug where getParameterBag() always returns null

Discussion
----------

Fixes a bug in PHPDumper, and in parameter resolving
This commit is contained in:
Fabien Potencier 2011-07-16 18:30:54 +02:00
commit d9048658d9
3 changed files with 21 additions and 5 deletions

View File

@ -601,7 +601,7 @@ EOF;
*/
private function startClass($class, $baseClass)
{
$bagClass = $this->container->isFrozen() ? '' : 'use Symfony\Component\DependencyInjection\ParameterBag\\ParameterBag;';
$bagClass = $this->container->isFrozen() ? 'use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;' : 'use Symfony\Component\DependencyInjection\ParameterBag\\ParameterBag;';
return <<<EOF
<?php
@ -743,6 +743,18 @@ EOF;
{
throw new \LogicException('Impossible to call set() on a frozen ParameterBag.');
}
/**
* {@inheritDoc}
*/
public function getParameterBag()
{
if (null === \$this->parameterBag) {
\$this->parameterBag = new FrozenParameterBag(\$this->parameters);
}
return \$this->parameterBag;
}
EOF;
}

View File

@ -195,7 +195,7 @@ class ParameterBag implements ParameterBagInterface
$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]);
if (isset($resolving[$key])) {
throw new ParameterCircularReferenceException(array_keys($resolving));
@ -203,10 +203,11 @@ class ParameterBag implements ParameterBagInterface
$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)) {
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;
return $self->isResolved() ? $resolved : $self->resolveString($resolved, $resolving);

View File

@ -120,7 +120,7 @@ class ParameterBagTest extends \PHPUnit_Framework_TestCase
$bag->resolveValue('%foo%');
$this->fail('->resolveValue() throws a RuntimeException when a parameter embeds another non-string parameter');
} 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%'));
@ -138,6 +138,9 @@ class ParameterBagTest extends \PHPUnit_Framework_TestCase
} 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');
}
$bag = new ParameterBag(array('host' => 'foo.bar', 'port' => 1337));
$this->assertEquals('foo.bar:1337', $bag->resolveValue('%host%:%port%'));
}
/**