15
15
use Symfony \Component \DependencyInjection \Definition ;
16
16
use Symfony \Component \DependencyInjection \Exception \ExceptionInterface ;
17
17
use Symfony \Component \DependencyInjection \Exception \RuntimeException ;
18
+ use Symfony \Component \DependencyInjection \Exception \ServiceCircularReferenceException ;
18
19
19
20
/**
20
21
* This replaces all ChildDefinition instances with their equivalent fully
25
26
*/
26
27
class ResolveChildDefinitionsPass extends AbstractRecursivePass
27
28
{
29
+ private $ currentPath ;
30
+
28
31
protected function processValue ($ value , $ isRoot = false )
29
32
{
30
33
if (!$ value instanceof Definition) {
@@ -36,6 +39,7 @@ protected function processValue($value, $isRoot = false)
36
39
$ value = $ this ->container ->getDefinition ($ this ->currentId );
37
40
}
38
41
if ($ value instanceof ChildDefinition) {
42
+ $ this ->currentPath = array ();
39
43
$ value = $ this ->resolveDefinition ($ value );
40
44
if ($ isRoot ) {
41
45
$ this ->container ->setDefinition ($ this ->currentId , $ value );
@@ -56,6 +60,8 @@ private function resolveDefinition(ChildDefinition $definition)
56
60
{
57
61
try {
58
62
return $ this ->doResolveDefinition ($ definition );
63
+ } catch (ServiceCircularReferenceException $ e ) {
64
+ throw $ e ;
59
65
} catch (ExceptionInterface $ e ) {
60
66
$ r = new \ReflectionProperty ($ e , 'message ' );
61
67
$ r ->setAccessible (true );
@@ -71,6 +77,13 @@ private function doResolveDefinition(ChildDefinition $definition)
71
77
throw new RuntimeException (sprintf ('Parent definition "%s" does not exist. ' , $ parent ));
72
78
}
73
79
80
+ $ searchKey = array_search ($ parent , $ this ->currentPath );
81
+ $ this ->currentPath [] = $ parent ;
82
+
83
+ if (false !== $ searchKey ) {
84
+ throw new ServiceCircularReferenceException ($ parent , \array_slice ($ this ->currentPath , $ searchKey ));
85
+ }
86
+
74
87
$ parentDef = $ this ->container ->findDefinition ($ parent );
75
88
if ($ parentDef instanceof ChildDefinition) {
76
89
$ id = $ this ->currentId ;
0 commit comments