Skip to content

Commit

Permalink
bug #30048 [DependencyInjection] Fix serialization of \Closure in Rem…
Browse files Browse the repository at this point in the history
…oveUnusedDefinitionsPass (XuruDragon)

This PR was merged into the 4.3-dev branch.

Discussion
----------

[DependencyInjection] Fix serialization of \Closure in RemoveUnusedDefinitionsPass

| 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

Commits
-------

b092502 [DependencyInjection] Fix serialization of \Closure in RemoveUnusedDefinitionsPass
  • Loading branch information
nicolas-grekas committed Feb 1, 2019
2 parents 9429fac + b092502 commit bd123e4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
Expand Up @@ -70,7 +70,7 @@ public function process(ContainerBuilder $container)
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));
}
}
Expand Down
Expand Up @@ -1394,6 +1394,10 @@ public function resolveEnvPlaceholders($value, $format = null, array &$usedEnvs
$value = $bag->resolveValue($value);
}

if ($value instanceof Definition) {
$value = (array) $value;
}

if (\is_array($value)) {
$result = [];
foreach ($value as $k => $v) {
Expand Down
Expand Up @@ -142,6 +142,24 @@ public function testProcessDoesNotErrorOnServicesThatDoNotHaveDefinitions()
$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);
Expand Down

0 comments on commit bd123e4

Please sign in to comment.