8000 Fix % parameter escaping by lsmith77 · Pull Request #3241 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Fix % parameter escaping #3241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to < 10000 /summary>
Jump to file
Failed to load files.
Loading
Diff view
Diff view
WIP fix handling of escaped % in parameters
  • Loading branch information
lsmith77 committed Feb 1, 2012
commit cd127627831c3eac312600fee58e1e3ebdbcda21
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ public function resolve()
$parameters = array();
foreach ($this->parameters as $key => $value) {
try {
$value = $this->resolveValue($value);
$parameters[$key] = is_string($value) ? str_replace('%%', '%', $value) : $value;
$parameters[$key] = $this->resolveValue($value);
} catch (ParameterNotFoundException $e) {
$e->setSourceKey($key);

Expand Down Expand Up @@ -212,7 +211,7 @@ public function resolveString($value, array $resolving = array())

$self = $this;

return preg_replace_callback('/(?<!%)%([^%\s]+)%/', function ($match) use ($self, $resolving, $value) {
$value = preg_replace_callback('/(?<!%)%([^%\s]+)%/', function ($match) use ($self, $resolving, $value) {
$key = strtolower($match[1]);
if (isset($resolving[$key])) {
throw new ParameterCircularReferenceException(array_keys($resolving));
Expand All @@ -229,6 +228,8 @@ public function resolveString($value, array $resolving = array())

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

return str_replace('%%', '%', $value);;
}

public function isResolved()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ 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');
$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');
Expand Down
0