Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[DependencyInjection] inlined factory not referenced
  • Loading branch information
boekkooi authored and fabpot committed Nov 12, 2014
1 parent 1f55706 commit 7816a98
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
Expand Up @@ -111,6 +111,10 @@ private function processArguments(array $arguments)
$this->processArguments($argument->getArguments());
$this->processArguments($argument->getMethodCalls());
$this->processArguments($argument->getProperties());

if ($argument->getFactoryService()) {
$this->processArguments(array(new Reference($argument->getFactoryService())));
}
}
}
}
Expand Down
Expand Up @@ -138,6 +138,10 @@ private function isInlineableDefinition(ContainerBuilder $container, $id, Defini
return false;
}

if (count($ids) > 1 && $definition->getFactoryService()) {
return false;
}

return $container->getDefinition(reset($ids))->getScope() === $definition->getScope();
}
}
Expand Up @@ -79,6 +79,28 @@ public function testProcessDetectsReferencesFromInlinedDefinitions()
$this->assertSame($ref, $refs[0]->getValue());
}

public function testProcessDetectsReferencesFromInlinedFactoryDefinitions()
{
$container = new ContainerBuilder();

$container
->register('a')
;

$factory = new Definition();
$factory->setFactoryService('a');

$container
->register('b')
->addArgument($factory)
;

$graph = $this->process($container);

$this->assertTrue($graph->hasNode('a'));
$this->assertCount(1, $refs = $graph->getNode('a')->getInEdges());
}

public function testProcessDoesNotSaveDuplicateReferences()
{
$container = new ContainerBuilder();
Expand Down
Expand Up @@ -110,6 +110,84 @@ public function testProcessInlinesIfMultipleReferencesButAllFromTheSameDefinitio
$this->assertSame($a, $inlinedArguments[0]);
}

public function testProcessInlinesPrivateFactoryReference()
{
$container = new ContainerBuilder();

$container->register('a')->setPublic(false);
$b = $container
->register('b')
->setPublic(false)
->setFactoryService('a')
;

$container
->register('foo')
->setArguments(array(
$ref = new Reference('b'),
));

$this->process($container);

$inlinedArguments = $container->getDefinition('foo')->getArguments();
$this->assertSame($b, $inlinedArguments[0]);
}

public function testProcessDoesNotInlinePrivateFactoryIfReferencedMultipleTimesWithinTheSameDefinition()
{
$container = new ContainerBuilder();
$container
->register('a')
;
$container
->register('b')
->setPublic(false)
->setFactoryService('a')
;

$container
->register('foo')
->setArguments(array(
$ref1 = new Reference('b'),
$ref2 = new Reference('b'),
))
;
$this->process($container);

$args = $container->getDefinition('foo')->getArguments();
$this->assertSame($ref1, $args[0]);
$this->assertSame($ref2, $args[1]);
}

public function testProcessDoesNotInlineReferenceWhenUsedByInlineFactory()
{
$container = new ContainerBuilder();
$container
->register('a')
;
$container
->register('b')
->setPublic(false)
->setFactoryService('a')
;

$inlineFactory = new Definition();
$inlineFactory->setPublic(false);
$inlineFactory->setFactoryService('b');

$container
->register('foo')
->setArguments(array(
$ref = new Reference('b'),
$inlineFactory,
))
;
$this->process($container);

$args = $container->getDefinition('foo')->getArguments();
$this->assertSame($ref, $args[0]);
}

public function testProcessInlinesOnlyIfSameScope()
{
$container = new ContainerBuilder();
Expand Down

0 comments on commit 7816a98

Please sign in to comment.