diff --git a/src/Symfony/Component/DependencyInjection/Compiler/AnalyzeServiceReferencesPass.php b/src/Symfony/Component/DependencyInjection/Compiler/AnalyzeServiceReferencesPass.php index fc4047f902e1..8070920ff7ff 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/AnalyzeServiceReferencesPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/AnalyzeServiceReferencesPass.php @@ -122,7 +122,7 @@ protected function processValue($value, $isRoot = false) $this->lazy = false; $byConstructor = $this->byConstructor; - $this->byConstructor = true; + $this->byConstructor = $isRoot || $byConstructor; $this->processValue($value->getFactory()); $this->processValue($value->getArguments()); $this->byConstructor = $byConstructor; diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index 73c868f1e54c..a18d1665c539 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -302,10 +302,10 @@ private function getProxyDumper() return $this->proxyDumper; } - private function analyzeCircularReferences($sourceId, array $edges, &$checkedNodes, &$currentPath = []) + private function analyzeCircularReferences($sourceId, array $edges, &$checkedNodes, &$currentPath = [], $byConstructor = true) { $checkedNodes[$sourceId] = true; - $currentPath[$sourceId] = $sourceId; + $currentPath[$sourceId] = $byConstructor; foreach ($edges as $edge) { $node = $edge->getDestNode(); @@ -314,44 +314,52 @@ private function analyzeCircularReferences($sourceId, array $edges, &$checkedNod if (!$node->getValue() instanceof Definition || $sourceId === $id || $edge->isLazy() || $edge->isWeak()) { // no-op } elseif (isset($currentPath[$id])) { - $currentId = $id; - foreach (array_reverse($currentPath) as $parentId) { - $this->circularReferences[$parentId][$currentId] = $currentId; - if ($parentId === $id) { - break; - } - $currentId = $parentId; - } + $this->addCircularReferences($id, $currentPath, $edge->isReferencedByConstructor()); } elseif (!isset($checkedNodes[$id])) { - $this->analyzeCircularReferences($id, $node->getOutEdges(), $checkedNodes, $currentPath); + $this->analyzeCircularReferences($id, $node->getOutEdges(), $checkedNodes, $currentPath, $edge->isReferencedByConstructor()); } elseif (isset($this->circularReferences[$id])) { - $this->connectCircularReferences($id, $currentPath); + $this->connectCircularReferences($id, $currentPath, $edge->isReferencedByConstructor()); } } unset($currentPath[$sourceId]); } - private function connectCircularReferences($sourceId, &$currentPath, &$subPath = []) + private function connectCircularReferences($sourceId, &$currentPath, $byConstructor, &$subPath = []) { - $subPath[$sourceId] = $sourceId; - $currentPath[$sourceId] = $sourceId; + $currentPath[$sourceId] = $subPath[$sourceId] = $byConstructor; - foreach ($this->circularReferences[$sourceId] as $id) { + foreach ($this->circularReferences[$sourceId] as $id => $byConstructor) { if (isset($currentPath[$id])) { - $currentId = $id; - foreach (array_reverse($currentPath) as $parentId) { - $this->circularReferences[$parentId][$currentId] = $currentId; - if ($parentId === $id) { - break; - } - $currentId = $parentId; - } + $this->addCircularReferences($id, $currentPath, $byConstructor); } elseif (!isset($subPath[$id]) && isset($this->circularReferences[$id])) { - $this->connectCircularReferences($id, $currentPath, $subPath); + $this->connectCircularReferences($id, $currentPath, $byConstructor, $subPath); } } - unset($currentPath[$sourceId]); - unset($subPath[$sourceId]); + unset($currentPath[$sourceId], $subPath[$sourceId]); + } + + private function addCircularReferences($id, $currentPath, $byConstructor) + { + $currentPath[$id] = $byConstructor; + $circularRefs = []; + + foreach (array_reverse($currentPath) as $parentId => $v) { + $byConstructor = $byConstructor && $v; + $circularRefs[] = $parentId; + + if ($parentId === $id) { + break; + } + } + + $currentId = $id; + foreach ($circularRefs as $parentId) { + if (empty($this->circularReferences[$parentId][$currentId])) { + $this->circularReferences[$parentId][$currentId] = $byConstructor; + } + + $currentId = $parentId; + } } private function collectLineage($class, array &$lineage) @@ -661,7 +669,6 @@ private function addService($id, Definition $definition, &$file = null) $autowired = $definition->isAutowired() ? ' autowired' : ''; if ($definition->isLazy()) { - unset($this->circularReferences[$id]); $lazyInitialization = '$lazyLoad = true'; } else { $lazyInitialization = ''; @@ -736,12 +743,12 @@ private function addInlineVariables($id, Definition $definition, array $argument private function addInlineReference($id, Definition $definition, $targetId, $forConstructor) { - list($callCount, $behavior) = $this->serviceCalls[$targetId]; - while ($this->container->hasAlias($targetId)) { $targetId = (string) $this->container->getAlias($targetId); } + list($callCount, $behavior) = $this->serviceCalls[$targetId]; + if ($id === $targetId) { return $this->addInlineService($id, $definition, $definition); } @@ -750,9 +757,13 @@ private function addInlineReference($id, Definition $definition, $targetId, $for return ''; } - $hasSelfRef = isset($this->circularReferences[$id][$targetId]); - $forConstructor = $forConstructor && !isset($this->definitionVariables[$definition]); - $code = $hasSelfRef && !$forConstructor ? $this->addInlineService($id, $definition, $definition) : ''; + $hasSelfRef = isset($this->circularReferences[$id][$targetId]) && !isset($this->definitionVariables[$definition]); + + if ($hasSelfRef && !$forConstructor && !$forConstructor = !$this->circularReferences[$id][$targetId]) { + $code = $this->addInlineService($id, $definition, $definition); + } else { + $code = ''; + } if (isset($this->referenceVariables[$targetId]) || (2 > $callCount && (!$hasSelfRef || !$forConstructor))) { return $code; @@ -785,15 +796,23 @@ private function addInlineReference($id, Definition $definition, $targetId, $for private function addInlineService($id, Definition $definition, Definition $inlineDef = null, $forConstructor = true) { - $isSimpleInstance = $isRootInstance = null === $inlineDef; + $code = ''; + + if ($isSimpleInstance = $isRootInstance = null === $inlineDef) { + foreach ($this->serviceCalls as $targetId => list($callCount, $behavior, $byConstructor)) { + if ($byConstructor && isset($this->circularReferences[$id][$targetId]) && !$this->circularReferences[$id][$targetId]) { + $code .= $this->addInlineReference($id, $definition, $targetId, $forConstructor); + } + } + } if (isset($this->definitionVariables[$inlineDef = $inlineDef ?: $definition])) { - return ''; + return $code; } $arguments = [$inlineDef->getArguments(), $inlineDef->getFactory()]; - $code = $this->addInlineVariables($id, $definition, $arguments, $forConstructor); + $code .= $this->addInlineVariables($id, $definition, $arguments, $forConstructor); if ($arguments = array_filter([$inlineDef->getProperties(), $inlineDef->getMethodCalls(), $inlineDef->getConfigurator()])) { $isSimpleInstance = false; @@ -1550,7 +1569,7 @@ private function getServiceConditionals($value) return implode(' && ', $conditions); } - private function getDefinitionsFromArguments(array $arguments, \SplObjectStorage $definitions = null, array &$calls = []) + private function getDefinitionsFromArguments(array $arguments, \SplObjectStorage $definitions = null, array &$calls = [], $byConstructor = null) { if (null === $definitions) { $definitions = new \SplObjectStorage(); @@ -1558,12 +1577,16 @@ private function getDefinitionsFromArguments(array $arguments, \SplObjectStorage foreach ($arguments as $argument) { if (\is_array($argument)) { - $this->getDefinitionsFromArguments($argument, $definitions, $calls); + $this->getDefinitionsFromArguments($argument, $definitions, $calls, $byConstructor); } elseif ($argument instanceof Reference) { $id = $this->container->normalizeId($argument); + while ($this->container->hasAlias($id)) { + $id = (string) $this->container->getAlias($id); + } + if (!isset($calls[$id])) { - $calls[$id] = [0, $argument->getInvalidBehavior()]; + $calls[$id] = [0, $argument->getInvalidBehavior(), $byConstructor]; } else { $calls[$id][1] = min($calls[$id][1], $argument->getInvalidBehavior()); } @@ -1575,8 +1598,10 @@ private function getDefinitionsFromArguments(array $arguments, \SplObjectStorage $definitions[$argument] = 1 + $definitions[$argument]; } else { $definitions[$argument] = 1; - $arguments = [$argument->getArguments(), $argument->getFactory(), $argument->getProperties(), $argument->getMethodCalls(), $argument->getConfigurator()]; - $this->getDefinitionsFromArguments($arguments, $definitions, $calls); + $arguments = [$argument->getArguments(), $argument->getFactory()]; + $this->getDefinitionsFromArguments($arguments, $definitions, $calls, null === $byConstructor || $byConstructor); + $arguments = [$argument->getProperties(), $argument->getMethodCalls(), $argument->getConfigurator()]; + $this->getDefinitionsFromArguments($arguments, $definitions, $calls, null !== $byConstructor && $byConstructor); } } @@ -1717,6 +1742,11 @@ private function dumpValue($value, $interpolate = true) return '$'.$value; } elseif ($value instanceof Reference) { $id = $this->container->normalizeId($value); + + while ($this->container->hasAlias($id)) { + $id = (string) $this->container->getAlias($id); + } + if (null !== $this->referenceVariables && isset($this->referenceVariables[$id])) { return $this->dumpValue($this->referenceVariables[$id], $interpolate); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php index 46074a67a593..ec163609180e 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php @@ -1423,6 +1423,13 @@ public function testAlmostCircular($visibility) $this->assertEquals((object) ['bar6' => (object) []], $foo6); $this->assertInstanceOf(\stdClass::class, $container->get('root')); + + $manager3 = $container->get('manager3'); + $listener3 = $container->get('listener3'); + $this->assertSame($manager3, $listener3->manager, 'Both should identically be the manager3 service'); + + $listener4 = $container->get('listener4'); + $this->assertInstanceOf('stdClass', $listener4); } public function provideAlmostCircular() diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php index 926933bb0e41..0fca22cb694c 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php @@ -842,6 +842,13 @@ public function testAlmostCircular($visibility) $this->assertEquals((object) ['bar6' => (object) []], $foo6); $this->assertInstanceOf(\stdClass::class, $container->get('root')); + + $manager3 = $container->get('manager3'); + $listener3 = $container->get('listener3'); + $this->assertSame($manager3, $listener3->manager); + + $listener4 = $container->get('listener4'); + $this->assertInstanceOf('stdClass', $listener4); } public function provideAlmostCircular() diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container_almost_circular.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container_almost_circular.php index df136cfa5ddb..a1f885399bd5 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container_almost_circular.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container_almost_circular.php @@ -2,7 +2,6 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; -use Symfony\Component\DependencyInjection\Dumper\PhpDumper; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Tests\Fixtures\FooForCircularWithAddCalls; @@ -102,6 +101,35 @@ $container->register('subscriber2', 'stdClass')->setPublic(false) ->addArgument(new Reference('manager2')); +// doctrine-like event system with listener + +$container->register('manager3', 'stdClass') + ->setLazy(true) + ->setPublic(true) + ->addArgument(new Reference('connection3')); + +$container->register('connection3', 'stdClass') + ->setPublic($public) + ->setProperty('listener', [new Reference('listener3')]); + +$container->register('listener3', 'stdClass') + ->setPublic(true) + ->setProperty('manager', new Reference('manager3')); + +// doctrine-like event system with small differences + +$container->register('manager4', 'stdClass') + ->setLazy(true) + ->addArgument(new Reference('connection4')); + +$container->register('connection4', 'stdClass') + ->setPublic($public) + ->setProperty('listener', [new Reference('listener4')]); + +$container->register('listener4', 'stdClass') + ->setPublic(true) + ->addArgument(new Reference('manager4')); + // private service involved in a loop $container->register('foo6', 'stdClass') diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_almost_circular_private.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_almost_circular_private.php index 5345aa3b308a..5f9bf8cee630 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_almost_circular_private.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_almost_circular_private.php @@ -39,9 +39,13 @@ public function __construct() 'level4' => 'getLevel4Service', 'level5' => 'getLevel5Service', 'level6' => 'getLevel6Service', + 'listener3' => 'getListener3Service', + 'listener4' => 'getListener4Service', 'logger' => 'getLoggerService', 'manager' => 'getManagerService', 'manager2' => 'getManager2Service', + 'manager3' => 'getManager3Service', + 'manager4' => 'getManager4Service', 'multiuse1' => 'getMultiuse1Service', 'root' => 'getRootService', 'subscriber' => 'getSubscriberService', @@ -53,6 +57,7 @@ public function __construct() 'level4' => true, 'level5' => true, 'level6' => true, + 'manager4' => true, 'multiuse1' => true, ]; @@ -69,6 +74,8 @@ public function getRemovedIds() 'bar6' => true, 'config' => true, 'config2' => true, + 'connection3' => true, + 'connection4' => true, 'dispatcher' => true, 'dispatcher2' => true, 'foo4' => true, @@ -81,6 +88,7 @@ public function getRemovedIds() 'level5' => true, 'level6' => true, 'logger2' => true, + 'manager4' => true, 'multiuse1' => true, 'subscriber2' => true, ]; @@ -272,6 +280,36 @@ protected function getFoobar4Service() return $instance; } + /** + * Gets the public 'listener3' shared service. + * + * @return \stdClass + */ + protected function getListener3Service() + { + $this->services['listener3'] = $instance = new \stdClass(); + + $instance->manager = ${($_ = isset($this->services['manager3']) ? $this->services['manager3'] : $this->getManager3Service()) && false ?: '_'}; + + return $instance; + } + + /** + * Gets the public 'listener4' shared service. + * + * @return \stdClass + */ + protected function getListener4Service() + { + $a = ${($_ = isset($this->services['manager4']) ? $this->services['manager4'] : $this->getManager4Service()) && false ?: '_'}; + + if (isset($this->services['listener4'])) { + return $this->services['listener4']; + } + + return $this->services['listener4'] = new \stdClass($a); + } + /** * Gets the public 'logger' shared service. * @@ -324,6 +362,24 @@ protected function getManager2Service() return $this->services['manager2'] = new \stdClass($a); } + /** + * Gets the public 'manager3' shared service. + * + * @return \stdClass + */ + protected function getManager3Service($lazyLoad = true) + { + $a = ${($_ = isset($this->services['listener3']) ? $this->services['listener3'] : $this->getListener3Service()) && false ?: '_'}; + + if (isset($this->services['manager3'])) { + return $this->services['manager3']; + } + $b = new \stdClass(); + $b->listener = [0 => $a]; + + return $this->services['manager3'] = new \stdClass($b); + } + /** * Gets the public 'root' shared service. * @@ -430,6 +486,22 @@ protected function getLevel6Service() return $instance; } + /** + * Gets the private 'manager4' shared service. + * + * @return \stdClass + */ + protected function getManager4Service($lazyLoad = true) + { + $a = new \stdClass(); + + $this->services['manager4'] = $instance = new \stdClass($a); + + $a->listener = [0 => ${($_ = isset($this->services['listener4']) ? $this->services['listener4'] : $this->getListener4Service()) && false ?: '_'}]; + + return $instance; + } + /** * Gets the private 'multiuse1' shared service. * diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_almost_circular_public.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_almost_circular_public.php index b569b335fc85..f41f831b3c5d 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_almost_circular_public.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_almost_circular_public.php @@ -30,6 +30,8 @@ public function __construct() 'baz6' => 'getBaz6Service', 'connection' => 'getConnectionService', 'connection2' => 'getConnection2Service', + 'connection3' => 'getConnection3Service', + 'connection4' => 'getConnection4Service', 'dispatcher' => 'getDispatcherService', 'dispatcher2' => 'getDispatcher2Service', 'foo' => 'getFooService', @@ -46,9 +48,13 @@ public function __construct() 'level4' => 'getLevel4Service', 'level5' => 'getLevel5Service', 'level6' => 'getLevel6Service', + 'listener3' => 'getListener3Service', + 'listener4' => 'getListener4Service', 'logger' => 'getLoggerService', 'manager' => 'getManagerService', 'manager2' => 'getManager2Service', + 'manager3' => 'getManager3Service', + 'manager4' => 'getManager4Service', 'multiuse1' => 'getMultiuse1Service', 'root' => 'getRootService', 'subscriber' => 'getSubscriberService', @@ -60,6 +66,7 @@ public function __construct() 'level4' => true, 'level5' => true, 'level6' => true, + 'manager4' => true, 'multiuse1' => true, ]; @@ -81,6 +88,7 @@ public function getRemovedIds() 'level5' => true, 'level6' => true, 'logger2' => true, + 'manager4' => true, 'multiuse1' => true, 'subscriber2' => true, ]; @@ -212,6 +220,34 @@ protected function getConnection2Service() return $instance; } + /** + * Gets the public 'connection3' shared service. + * + * @return \stdClass + */ + protected function getConnection3Service() + { + $this->services['connection3'] = $instance = new \stdClass(); + + $instance->listener = [0 => ${($_ = isset($this->services['listener3']) ? $this->services['listener3'] : $this->getListener3Service()) && false ?: '_'}]; + + return $instance; + } + + /** + * Gets the public 'connection4' shared service. + * + * @return \stdClass + */ + protected function getConnection4Service() + { + $this->services['connection4'] = $instance = new \stdClass(); + + $instance->listener = [0 => ${($_ = isset($this->services['listener4']) ? $this->services['listener4'] : $this->getListener4Service()) && false ?: '_'}]; + + return $instance; + } + /** * Gets the public 'dispatcher' shared service. * @@ -372,6 +408,36 @@ protected function getFoobar4Service() return $instance; } + /** + * Gets the public 'listener3' shared service. + * + * @return \stdClass + */ + protected function getListener3Service() + { + $this->services['listener3'] = $instance = new \stdClass(); + + $instance->manager = ${($_ = isset($this->services['manager3']) ? $this->services['manager3'] : $this->getManager3Service()) && false ?: '_'}; + + return $instance; + } + + /** + * Gets the public 'listener4' shared service. + * + * @return \stdClass + */ + protected function getListener4Service() + { + $a = ${($_ = isset($this->services['manager4']) ? $this->services['manager4'] : $this->getManager4Service()) && false ?: '_'}; + + if (isset($this->services['listener4'])) { + return $this->services['listener4']; + } + + return $this->services['listener4'] = new \stdClass($a); + } + /** * Gets the public 'logger' shared service. * @@ -424,6 +490,22 @@ protected function getManager2Service() return $this->services['manager2'] = new \stdClass($a); } + /** + * Gets the public 'manager3' shared service. + * + * @return \stdClass + */ + protected function getManager3Service($lazyLoad = true) + { + $a = ${($_ = isset($this->services['connection3']) ? $this->services['connection3'] : $this->getConnection3Service()) && false ?: '_'}; + + if (isset($this->services['manager3'])) { + return $this->services['manager3']; + } + + return $this->services['manager3'] = new \stdClass($a); + } + /** * Gets the public 'root' shared service. * @@ -530,6 +612,22 @@ protected function getLevel6Service() return $instance; } + /** + * Gets the private 'manager4' shared service. + * + * @return \stdClass + */ + protected function getManager4Service($lazyLoad = true) + { + $a = ${($_ = isset($this->services['connection4']) ? $this->services['connection4'] : $this->getConnection4Service()) && false ?: '_'}; + + if (isset($this->services['manager4'])) { + return $this->services['manager4']; + } + + return $this->services['manager4'] = new \stdClass($a); + } + /** * Gets the private 'multiuse1' shared service. *