Skip to content

[DependencyInjection] Problem with unescaping class arguments #4665

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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ private function createService(Definition $definition, $id)
require_once $this->getParameterBag()->resolveValue($definition->getFile());
}

$arguments = $this->resolveServices($this->getParameterBag()->resolveValue($definition->getArguments()));
$arguments = $this->resolveServices($this->getParameterBag()->resolveAndUnescapeValue($definition->getArguments()));

if (null !== $definition->getFactoryMethod()) {
if (null !== $definition->getFactoryClass()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,14 @@ private function unescapeValue($value)

return $value;
}

/**
* Resolves parameters inside a string and unescape result
*
* @see resolveValue, unescapeValue
*/
public function resolveAndUnescapeValue($value, array $resolving = array())
{
return $this->unescapeValue($this->resolveValue($value, $resolving));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,15 @@ function resolveValue($value);
* @return mixed
*/
function escapeValue($value);

/**
* Replaces parameter placeholders (%name%) by their values and unescape (%%) result.
*
* @param mixed $value A value
*
* @throws ParameterNotFoundException if a placeholder references a parameter that does not exist
* @throws ParameterCircularReferenceException if a circular reference if detected
* @throws RuntimeException when a given parameter has a type problem.
*/
function resolveAndUnescapeValue($value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,9 @@ public function testCreateServiceArguments()
{
$builder = new ContainerBuilder();
$builder->register('bar', 'stdClass');
$builder->register('foo1', 'FooClass')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar')));
$builder->register('foo1', 'FooClass')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar'), '%%unescape_it%%'));
$builder->setParameter('value', 'bar');
$this->assertEquals(array('foo' => 'bar', 'bar' => 'foo', $builder->get('bar')), $builder->get('foo1')->arguments, '->createService() replaces parameters and service references in the arguments provided by the service definition');
$this->assertEquals(array('foo' => 'bar', 'bar' => 'foo', $builder->get('bar'), '%unescape_it%'), $builder->get('foo1')->arguments, '->createService() replaces parameters and service references in the arguments provided by the service definition');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,14 @@ public function stringsWithSpacesProvider()
array('50% is less than 100%', '50% is less than 100%', 'Text between % signs is allowed, if there are spaces.'),
);
}

/**
* @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::resolveAndUnescapeValue
*/
public function testResolveAndUnescapeValue()
{
$bag = new ParameterBag();
$this->assertEquals('foo', $bag->resolveAndUnescapeValue('foo'), 'There is nothing to unescape.');
$this->assertEquals('%foo%', $bag->resolveAndUnescapeValue('%%foo%%'), 'String should be unescaped.');
}
}