@@ -36,7 +36,7 @@ public function process(ContainerBuilder $container)
36
36
try {
37
37
parent ::process ($ container );
38
38
} finally {
39
- // Free memory and remove circular reference to container
39
+ // Free memory
40
40
$ this ->definedTypes = array ();
41
41
$ this ->types = null ;
42
42
$ this ->ambiguousServiceTypes = array ();
@@ -90,6 +90,7 @@ protected function processValue($value, $isRoot = false)
90
90
}
91
91
92
92
$ methodCalls = $ this ->autowireMethodCalls ($ reflectionClass , $ methodCalls , $ autowiredMethods );
93
+ $ overriddenGetters = $ this ->autowireOverridenGetters ($ value ->getOverriddenGetters (), $ autowiredMethods );
93
94
94
95
if ($ constructor ) {
95
96
list (, $ arguments ) = array_shift ($ methodCalls );
@@ -103,6 +104,10 @@ protected function processValue($value, $isRoot = false)
103
104
$ value ->setMethodCalls ($ methodCalls );
104
105
}
105
106
107
+ if ($ overriddenGetters !== $ value ->getOverriddenGetters ()) {
108
+ $ value ->setOverriddenGetters ($ overriddenGetters );
109
+ }
110
+
106
111
return parent ::processValue ($ value , $ isRoot );
107
112
}
108
113
@@ -124,7 +129,7 @@ private function getMethodsToAutowire(\ReflectionClass $reflectionClass, array $
124
129
$ regexList [] = '/^ ' .str_replace ('\* ' , '.* ' , preg_quote ($ pattern , '/ ' )).'$/i ' ;
125
130
}
126
131
127
- foreach ($ reflectionClass ->getMethods (\ReflectionMethod::IS_PUBLIC ) as $ reflectionMethod ) {
132
+ foreach ($ reflectionClass ->getMethods (\ReflectionMethod::IS_PUBLIC | \ReflectionMethod:: IS_PROTECTED ) as $ reflectionMethod ) {
128
133
if ($ reflectionMethod ->isStatic ()) {
129
134
continue ;
130
135
}
@@ -164,7 +169,7 @@ private function autowireMethodCalls(\ReflectionClass $reflectionClass, array $m
164
169
list ($ method , $ arguments ) = $ call ;
165
170
$ method = $ parameterBag ->resolveValue ($ method );
166
171
167
- if (isset ($ autowiredMethods [$ lcMethod = strtolower ($ method )])) {
172
+ if (isset ($ autowiredMethods [$ lcMethod = strtolower ($ method )]) && $ autowiredMethods [ $ lcMethod ]-> isPublic () ) {
168
173
$ reflectionMethod = $ autowiredMethods [$ lcMethod ];
169
174
unset($ autowiredMethods [$ lcMethod ]);
170
175
} else {
@@ -177,15 +182,15 @@ private function autowireMethodCalls(\ReflectionClass $reflectionClass, array $m
177
182
}
178
183
}
179
184
180
- $ arguments = $ this ->autowireMethod ($ reflectionMethod , $ arguments , true );
185
+ $ arguments = $ this ->autowireMethodCall ($ reflectionMethod , $ arguments , true );
181
186
182
187
if ($ arguments !== $ call [1 ]) {
183
188
$ methodCalls [$ i ][1 ] = $ arguments ;
184
189
}
185
190
}
186
191
187
192
foreach ($ autowiredMethods as $ reflectionMethod ) {
188
- if ($ arguments = $ this ->autowireMethod ($ reflectionMethod , array (), false )) {
193
+ if ($ reflectionMethod -> isPublic () && $ arguments = $ this ->autowireMethodCall ($ reflectionMethod , array (), false )) {
189
194
$ methodCalls [] = array ($ reflectionMethod ->name , $ arguments );
190
195
}
191
196
}
@@ -194,7 +199,7 @@ private function autowireMethodCalls(\ReflectionClass $reflectionClass, array $m
194
199
}
195
200
196
201
/**
197
- * Autowires the constructor or a setter .
202
+ * Autowires the constructor or a method .
198
203
*
199
204
* @param \ReflectionMethod $reflectionMethod
200
205
* @param array $arguments
@@ -204,7 +209,7 @@ private function autowireMethodCalls(\ReflectionClass $reflectionClass, array $m
204
209
*
205
210
* @throws RuntimeException
206
211
*/
207
- private function autowireMethod (\ReflectionMethod $ reflectionMethod , array $ arguments , $ mustAutowire )
212
+ private function autowireMethodCall (\ReflectionMethod $ reflectionMethod , array $ arguments , $ mustAutowire )
208
213
{
209
214
$ didAutowire = false ; // Whether any arguments have been autowired or not
210
215
foreach ($ reflectionMethod ->getParameters () as $ index => $ parameter ) {
@@ -298,6 +303,55 @@ private function autowireMethod(\ReflectionMethod $reflectionMethod, array $argu
298
303
return $ arguments ;
299
304
}
300
305
306
+ /**
307
+ * Autowires getters.
308
+ *
309
+ * @param array $overridenGetters
310
+ * @param array $autowiredMethods
311
+ *
312
+ * @return array
313
+ */
314
+ private function autowireOverridenGetters (array $ overridenGetters , array $ autowiredMethods )
315
+ {
316
+ foreach ($ autowiredMethods as $ reflectionMethod ) {
317
+ if (isset ($ overridenGetters [strtolower ($ reflectionMethod ->name )])
318
+ || !method_exists ($ reflectionMethod , 'getReturnType ' )
319
+ || 0 !== $ reflectionMethod ->getNumberOfParameters ()
320
+ || $ reflectionMethod ->isFinal ()
321
+ || $ reflectionMethod ->returnsReference ()
322
+ || !$ returnType = $ reflectionMethod ->getReturnType ()
323
+ ) {
324
+ continue ;
325
+ }
326
+ $ typeName = $ returnType instanceof \ReflectionNamedType ? $ returnType ->getName () : $ returnType ->__toString ();
327
+
328
+ if ($ this ->container ->has ($ typeName ) && !$ this ->container ->findDefinition ($ typeName )->isAbstract ()) {
329
+ $ overridenGetters [$ reflectionMethod ->name ] = new Reference ($ typeName );
330
+ continue ;
331
+ }
332
+
333
+ if (null === $ this ->types ) {
334
+ $ this ->populateAvailableTypes ();
335
+ }
336
+
337
+ if (isset ($ this ->types [$ typeName ])) {
338
+ $ value = new Reference ($ this ->types [$ typeName ]);
339
+ } elseif ($ returnType = $ this ->container ->getReflectionClass ($ typeName , true )) {
340
+ try {
341
+ $ value = $ this ->createAutowiredDefinition ($ returnType );
342
+ } catch (RuntimeException $ e ) {
343
+ continue ;
344
+ }
345
+ } else {
346
+ continue ;
347
+ }
348
+
349
+ $ overridenGetters [$ reflectionMethod ->name ] = $ value ;
350
+ }
351
+
352
+ return $ overridenGetters ;
353
+ }
354
+
301
355
/**
302
356
* Populates the list of available types.
303
357
*/
0 commit comments