Skip to content

Commit

Permalink
[DependencyInjection] fixed regression when a parameter contains an e…
Browse files Browse the repository at this point in the history
…scaped %

Notice that this is still not perfect, but I don't see how to fix the remaining
potential problems.
  • Loading branch information
fabpot committed May 26, 2011
1 parent ce79fa9 commit 3bdb7c2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
Expand Up @@ -31,7 +31,6 @@ class ResolveParameterPlaceHoldersPass implements CompilerPassInterface
public function process(ContainerBuilder $container)
{
$parameterBag = $container->getParameterBag();
$parameterBag->resolve();

foreach ($container->getDefinitions() as $id => $definition) {
try {
Expand All @@ -58,5 +57,7 @@ public function process(ContainerBuilder $container)
$aliases[$parameterBag->resolveValue($name)] = $parameterBag->resolveValue($target);
}
$container->setAliases($aliases);

$parameterBag->resolve();
}
}
Expand Up @@ -30,6 +30,7 @@ class FrozenParameterBag extends ParameterBag
public function __construct(array $parameters = array())
{
$this->parameters = $parameters;
$this->resolved = true;
}

/**
Expand Down
Expand Up @@ -22,6 +22,7 @@
class ParameterBag implements ParameterBagInterface
{
protected $parameters;
protected $resolved;

/**
* Constructor.
Expand All @@ -32,6 +33,7 @@ public function __construct(array $parameters = array())
{
$this->parameters = array();
$this->add($parameters);
$this->resolved = false;
}

/**
Expand Down Expand Up @@ -112,15 +114,24 @@ public function has($name)
*/
public function resolve()
{
if ($this->resolved) {
return;
}

$parameters = array();
foreach ($this->parameters as $key => $value) {
try {
$this->parameters[$key] = $this->resolveValue($value);
$value = $this->resolveValue($value);
$parameters[$key] = is_string($value) ? str_replace('%%', '%', $value) : $value;
} catch (ParameterNotFoundException $e) {
$e->setSourceKey($key);

throw $e;
}
}

$this->parameters = $parameters;
$this->resolved = true;
}

/**
Expand Down Expand Up @@ -179,13 +190,12 @@ public function resolveString($value, array $resolving = array())

$resolving[$key] = true;

return $this->resolveValue($this->get($key), $resolving);
return $this->resolved ? $this->get($key) : $this->resolveValue($this->get($key), $resolving);
}

$self = $this;
return str_replace('%%', '%', preg_replace_callback('/(?<!%)%([^%]+)%/', function ($match) use ($self, $resolving) {
return preg_replace_callback('/(?<!%)%([^%]+)%/', function ($match) use ($self, $resolving) {
$key = strtolower($match[1]);

if (isset($resolving[$key])) {
throw new ParameterCircularReferenceException(array_keys($resolving));
}
Expand All @@ -198,7 +208,12 @@ public function resolveString($value, array $resolving = array())

$resolving[$key] = true;

return $self->resolveString($resolved, $resolving);
}, $value));
return $self->isResolved() ? $resolved : $self->resolveString($resolved, $resolving);
}, $value);
}

public function isResolved()
{
return $this->resolved;
}
}
Expand Up @@ -92,8 +92,8 @@ public function testResolveValue()
$this->assertEquals('I\'m a bar', $bag->resolveValue('I\'m a %foo%'), '->resolveValue() replaces placeholders by their values');
$this->assertEquals(array('bar' => 'bar'), $bag->resolveValue(array('%foo%' => '%foo%')), '->resolveValue() replaces placeholders in keys and values of arrays');
$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('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');

$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');
Expand Down

0 comments on commit 3bdb7c2

Please sign in to comment.