Skip to content

Commit c59f1dd

Browse files
committed
bug #11639 [DependencyInjection] Fixed factory service not within the ServiceReferenceGraph. (boekkooi)
This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes #11639). Discussion ---------- [DependencyInjection] Fixed factory service not within the ServiceReferenceGraph. | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Fixed a problem where Factory services are not added to the ServiceReferenceGraph. Commits ------- e992f8e Fixed Factory services not within the ServiceReferenceGraph.
2 parents 3cb9d7a + e992f8e commit c59f1dd

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

src/Symfony/Component/DependencyInjection/Compiler/AnalyzeServiceReferencesPass.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ public function process(ContainerBuilder $container)
6969

7070
$this->currentId = $id;
7171
$this->currentDefinition = $definition;
72+
7273
$this->processArguments($definition->getArguments());
74+
if ($definition->getFactoryService()) {
75+
$this->processArguments(array(new Reference($definition->getFactoryService())));
76+
}
7377

7478
if (!$this->onlyConstructorArguments) {
7579
$this->processArguments($definition->getMethodCalls());

src/Symfony/Component/DependencyInjection/Tests/Compiler/AnalyzeServiceReferencesPassTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,26 @@ public function testProcessDoesNotSaveDuplicateReferences()
9797
$this->assertCount(2, $graph->getNode('a')->getInEdges());
9898
}
9999

100+
public function testProcessDetectsFactoryReferences()
101+
{
102+
$container = new ContainerBuilder();
103+
104+
$container
105+
->register('foo', 'stdClass')
106+
->setFactoryClass('stdClass')
107+
->setFactoryMethod('getInstance');
108+
109+
$container
110+
->register('bar', 'stdClass')
111+
->setFactoryService('foo')
112+
->setFactoryMethod('getInstance');
113+
114+
$graph = $this->process($container);
115+
116+
$this->assertTrue($graph->hasNode('foo'));
117+
$this->assertCount(1, $graph->getNode('foo')->getInEdges());
118+
}
119+
100120
protected function process(ContainerBuilder $container)
101121
{
102122
$pass = new RepeatedPass(array(new AnalyzeServiceReferencesPass()));

src/Symfony/Component/DependencyInjection/Tests/Compiler/CheckCircularReferencesPassTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ public function testProcessWithAliases()
4848
$this->process($container);
4949
}
5050

51+
/**
52+
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
53+
*/
54+
public function testProcessWithFactory()
55+
{
56+
$container = new ContainerBuilder();
57+
58+
$container
59+
->register('a', 'stdClass')
60+
->setFactoryService('b')
61+
->setFactoryMethod('getInstance');
62+
63+
$container
64+
->register('b', 'stdClass')
65+
->setFactoryService('a')
66+
->setFactoryMethod('getInstance');
67+
68+
$this->process($container);
69+
}
70+
5171
/**
5272
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
5373
*/
@@ -61,6 +81,25 @@ public function testProcessDetectsIndirectCircularReference()
6181
$this->process($container);
6282
}
6383

84+
/**
85+
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
86+
*/
87+
public function testProcessDetectsIndirectCircularReferenceWithFactory()
88+
{
89+
$container = new ContainerBuilder();
90+
91+
$container->register('a')->addArgument(new Reference('b'));
92+
93+
$container
94+
->register('b', 'stdClass')
95+
->setFactoryService('c')
96+
->setFactoryMethod('getInstance');
97+
98+
$container->register('c')->addArgument(new Reference('a'));
99+
100+
$this->process($container);
101+
}
102+
64103
/**
65104
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
66105
*/

src/Symfony/Component/DependencyInjection/Tests/Compiler/RemoveUnusedDefinitionsPassTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,33 @@ public function testProcessWorksWithInlinedDefinitions()
8181
$this->assertTrue($container->hasDefinition('bar'));
8282
}
8383

84+
public function testProcessWontRemovePrivateFactory()
85+
{
86+
$container = new ContainerBuilder();
87+
88+
$container
89+
->register('foo', 'stdClass')
90+
->setFactoryClass('stdClass')
91+
->setFactoryMethod('getInstance')
92+
->setPublic(false);
93+
94+
$container
95+
->register('bar', 'stdClass')
96+
->setFactoryService('foo')
97+
->setFactoryMethod('getInstance')
98+
->setPublic(false);
99+
100+
$container
101+
->register('foobar')
102+
->addArgument(new Reference('bar'));
103+
104+
$this->process($container);
105+
106+
$this->assertTrue($container->hasDefinition('foo'));
107+
$this->assertTrue($container->hasDefinition('bar'));
108+
$this->assertTrue($container->hasDefinition('foobar'));
109+
}
110+
84111
protected function process(ContainerBuilder $container)
85112
{
86113
$repeatedPass = new RepeatedPass(array(new AnalyzeServiceReferencesPass(), new RemoveUnusedDefinitionsPass()));

0 commit comments

Comments
 (0)