Skip to content

Commit

Permalink
[DependencyInjection] Fix #29694 - cannot serialize \Closure
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony MARTIN <anthony.martin@sensiolabs.com>

| Q             | A
| ------------- | ---
| Branch?       | 4.2
| 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
  • Loading branch information
Anthony MARTIN committed Feb 1, 2019
1 parent 4e4ebde commit dd32ace
Show file tree
Hide file tree
Showing 3 changed files with 28 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 @@ -1388,6 +1388,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,29 @@ public function testProcessDoesNotErrorOnServicesThatDoNotHaveDefinitions()
$this->assertFalse($container->hasDefinition('not.defined'));
}

public function testProcessWorksWithClosureErrorsInDefinitions()
{
$definition = new Definition(null, [new Reference('foo')]);
$definition->addError(function () {
return 'foo bar';
});

$container = new ContainerBuilder();
$container
->register('foo')
->setPublic(false)
;
$container
->register('bar')
->setArguments([$definition])
;

$this->process($container);

$this->assertTrue($container->hasDefinition('foo'));
$this->assertTrue($container->hasDefinition('bar'));
}

protected function process(ContainerBuilder $container)
{
(new RemoveUnusedDefinitionsPass())->process($container);
Expand Down

0 comments on commit dd32ace

Please sign in to comment.