@@ -2121,4 +2121,45 @@ public function setDelegateLookupContainer(ContainerInterface $delegateLookupCon
2121
2121
return $ this ;
2122
2122
}
2123
2123
2124
+ /**
2125
+ * Check if there is no loop in constructor arguments
2126
+ * @throws MoufException
2127
+ */
2128
+ public function checkConstructorLoop () {
2129
+ foreach ($ this ->declaredInstances as $ instanceName => $ descriptor ) {
2130
+ $ this ->walkConstructorLoop ($ instanceName , []);
2131
+ }
2132
+ }
2133
+
2134
+ /**
2135
+ * This take a instance name and the path to access.
2136
+ * It throw an exception if a loop was detected
2137
+ *
2138
+ * @param string $instanceName
2139
+ * @param array $path
2140
+ * @throws MoufException
2141
+ */
2142
+ private function walkConstructorLoop ($ instanceName , array $ path ) {
2143
+ if (isset ($ path [$ instanceName ])) {
2144
+ $ instances = array_keys ($ path );
2145
+ $ instances = array_slice ($ instances , array_search ($ instanceName , $ instances ));
2146
+ throw new MoufException ('A loop was detected on constructor arguments ' .implode (' -> ' , $ instances ).' -> ' .$ instanceName );
2147
+ }
2148
+ $ path [$ instanceName ] = true ;
2149
+ $ descriptor = $ this ->declaredInstances [$ instanceName ];
2150
+ if (isset ($ descriptor ['constructor ' ])) {
2151
+ foreach ($ descriptor ['constructor ' ] as $ constructorArg ) {
2152
+ if ($ constructorArg ['parametertype ' ] == 'object ' ) {
2153
+ if (is_array ($ constructorArg ['value ' ])) {
2154
+ foreach ($ constructorArg ['value ' ] as $ subInstanceName ) {
2155
+ $ this ->walkConstructorLoop ($ subInstanceName , $ path );
2156
+ }
2157
+ }
2158
+ else {
2159
+ $ this ->walkConstructorLoop ($ constructorArg ['value ' ], $ path );
2160
+ }
2161
+ }
2162
+ }
2163
+ }
2164
+ }
2124
2165
}
0 commit comments