8000 Unescape paramaters by stof · Pull Request #3260 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Unescape paramaters #3260

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

Merged
merged 4 commits into from
Feb 4, 2012
Merged
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
8000 Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function resolve()
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);

Expand Down Expand Up @@ -235,4 +235,22 @@ public function isResolved()
{
return $this->resolved;
}

private function unescapeValue($value)
{
if (is_string($value)) {
return str_replace('%%', '%', $value);
}

if (is_array($value)) {
$result = array();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a local variable here ? (What a pleasure to comment @stof code !)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure. I wrote it the way it is done in resolve() but resolve requires it because of the resolveValue call

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, this made me think about a possible issue with the unescaping. I will try to confirm it with a testcase so wait a bit before merging

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, the case I thought about does not break things. I tried too invent edge cases that don't exist.

foreach ($value as $k => $v) {
$result[$k] = $this->unescapeValue($v);
}

return $result;
}

return $value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public function testResolveValue()
$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() supp 8000 orts % 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 Expand Up @@ -165,6 +166,22 @@ public function testResolveIndicatesWhyAParameterIsNeeded()
}
}

/**
* @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
Expand Down
0