[DependencyInjection] Fix serialization of \Closure in RemoveUnusedDefinitionsPass

Signed-off-by: Anthony MARTIN <anthony.martin@sensiolabs.com>

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #29694
| License       | MIT
| Doc PR        | n/a

Fix the issue #29694
This commit is contained in:
Anthony MARTIN 2019-01-31 11:30:51 +01:00
parent 9429face97
commit b0925025f8
3 changed files with 23 additions and 1 deletions

View File

@ -70,7 +70,7 @@ class RemoveUnusedDefinitionsPass extends AbstractRecursivePass implements Repea
foreach ($container->getDefinitions() as $id => $definition) {
if (!isset($connectedIds[$id])) {
$container->removeDefinition($id);
$container->resolveEnvPlaceholders(serialize($definition));
$container->resolveEnvPlaceholders(!$definition->hasErrors() ? serialize($definition) : $definition);
$container->log($this, sprintf('Removed service "%s"; reason: unused.', $id));
}
}

View File

@ -1394,6 +1394,10 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
$value = $bag->resolveValue($value);
}
if ($value instanceof Definition) {
$value = (array) $value;
}
if (\is_array($value)) {
$result = [];
foreach ($value as $k => $v) {

View File

@ -142,6 +142,24 @@ class RemoveUnusedDefinitionsPassTest extends TestCase
$this->assertFalse($container->hasDefinition('not.defined'));
}
public function testProcessWorksWithClosureErrorsInDefinitions()
{
$definition = new Definition();
$definition->addError(function () {
return 'foo bar';
});
$container = new ContainerBuilder();
$container
->setDefinition('foo', $definition)
->setPublic(false)
;
$this->process($container);
$this->assertFalse($container->hasDefinition('foo'));
}
protected function process(ContainerBuilder $container)
{
(new RemoveUnusedDefinitionsPass())->process($container);