@@ -42,7 +42,7 @@ public function process(ContainerBuilder $container)
42
42
} finally {
43
43
spl_autoload_unregister ($ throwingAutoloader );
44
44
45
- // Free memory and remove circular reference to container
45
+ // Free memory
46
46
$ this ->reflectionClasses = array ();
47
47
$ this ->definedTypes = array ();
48
48
$ this ->types = null ;
@@ -97,6 +97,7 @@ protected function processValue($value, $isRoot = false)
97
97
}
98
98
99
99
$ methodCalls = $ this ->autowireMethodCalls ($ reflectionClass , $ methodCalls , $ autowiredMethods );
100
+ $ overriddenGetters = $ this ->autowireOverridenGetters ($ value ->getOverriddenGetters (), $ autowiredMethods );
100
101
101
102
if ($ constructor ) {
102
103
list (, $ arguments ) = array_shift ($ methodCalls );
@@ -110,6 +111,10 @@ protected function processValue($value, $isRoot = false)
110
111
$ value ->setMethodCalls ($ methodCalls );
111
112
}
112
113
114
+ if ($ overriddenGetters !== $ value ->getOverriddenGetters ()) {
115
+ $ value ->setOverriddenGetters ($ overriddenGetters );
116
+ }
117
+
113
118
return parent ::processValue ($ value , $ isRoot );
114
119
}
115
120
@@ -131,7 +136,7 @@ private function getMethodsToAutowire(\ReflectionClass $reflectionClass, array $
131
136
$ regexList [] = '/^ ' .str_replace ('\* ' , '.* ' , preg_quote ($ pattern , '/ ' )).'$/i ' ;
132
137
}
133
138
134
- foreach ($ reflectionClass ->getMethods (\ReflectionMethod::IS_PUBLIC ) as $ reflectionMethod ) {
139
+ foreach ($ reflectionClass ->getMethods (\ReflectionMethod::IS_PUBLIC | \ReflectionMethod:: IS_PROTECTED ) as $ reflectionMethod ) {
135
140
if ($ reflectionMethod ->isStatic ()) {
136
141
continue ;
137
142
}
@@ -171,7 +176,7 @@ private function autowireMethodCalls(\ReflectionClass $reflectionClass, array $m
171
176
list ($ method , $ arguments ) = $ call ;
172
177
$ method = $ parameterBag ->resolveValue ($ method );
173
178
174
- if (isset ($ autowiredMethods [$ lcMethod = strtolower ($ method )])) {
179
+ if (isset ($ autowiredMethods [$ lcMethod = strtolower ($ method )]) && $ autowiredMethods [ $ lcMethod ]-> isPublic () ) {
175
180
$ reflectionMethod = $ autowiredMethods [$ lcMethod ];
176
181
unset($ autowiredMethods [$ lcMethod ]);
177
182
} else {
@@ -184,15 +189,15 @@ private function autowireMethodCalls(\ReflectionClass $reflectionClass, array $m
184
189
}
185
190
}
186
191
187
- $ arguments = $ this ->autowireMethod ($ reflectionMethod , $ arguments , true );
192
+ $ arguments = $ this ->autowireMethodCall ($ reflectionMethod , $ arguments , true );
188
193
189
194
if ($ arguments !== $ call [1 ]) {
190
195
$ methodCalls [$ i ][1 ] = $ arguments ;
191
196
}
192
197
}
193
198
194
199
foreach ($ autowiredMethods as $ reflectionMethod ) {
195
- if ($ arguments = $ this ->autowireMethod ($ reflectionMethod , array (), false )) {
200
+ if ($ reflectionMethod -> isPublic () && $ arguments = $ this ->autowireMethodCall ($ reflectionMethod , array (), false )) {
196
201
$ methodCalls [] = array ($ reflectionMethod ->name , $ arguments );
197
202
}
198
203
}
@@ -201,7 +206,7 @@ private function autowireMethodCalls(\ReflectionClass $reflectionClass, array $m
201
206
}
202
207
203
208
/**
204
- * Autowires the constructor or a setter .
209
+ * Autowires the constructor or a method .
205
210
*
206
211
* @param \ReflectionMethod $reflectionMethod
207
212
* @param array $arguments
@@ -211,7 +216,7 @@ private function autowireMethodCalls(\ReflectionClass $reflectionClass, array $m
211
216
*
212
217
* @throws RuntimeException
213
218
*/
214
- private function autowireMethod (\ReflectionMethod $ reflectionMethod , array $ arguments , $ mustAutowire )
219
+ private function autowireMethodCall (\ReflectionMethod $ reflectionMethod , array $ arguments , $ mustAutowire )
215
220
{
216
221
$ didAutowire = false ; // Whether any arguments have been autowired or not
217
222
foreach ($ reflectionMethod ->getParameters () as $ index => $ parameter ) {
@@ -292,6 +297,52 @@ private function autowireMethod(\ReflectionMethod $reflectionMethod, array $argu
292
297
return $ arguments ;
293
298
}
294
299
300
+ /**
301
+ * Autowires getters.
302
+ *
303
+ * @param array $overridenGetters
304
+ * @param array $autowiredMethod
305
+ *
306
+ * @return array
307
+ */
308
+ private function autowireOverridenGetters (array $ overridenGetters , array $ autowiredMethod )
309
+ {
310
+ foreach ($ autowiredMethod as $ reflectionMethod ) {
311
+ if (
312
+ isset ($ overridenGetters [$ reflectionMethod ->name ]) ||
313
+ !method_exists ($ reflectionMethod , 'getReturnType ' ) ||
314
+ 0 !== $ reflectionMethod ->getNumberOfParameters () ||
315
+ $ reflectionMethod ->isFinal () ||
316
+ $ reflectionMethod ->returnsReference () ||
317
+ !($ returnType = $ reflectionMethod ->getReturnType ())
318
+ ) {
319
+ continue ;
320
+ }
321
+
322
+
323
+ if (null === $ this ->types ) {
324
+ $ this ->populateAvailableTypes ();
325
+ }
326
+
327
+ $ class = $ returnType ->__toString ();
328
+ if (isset ($ this ->types [$ class ])) {
329
+ $ value = new Reference ($ this ->types [$ class ]);
330
+ } else {
331
+ try {
332
+ $ value = $ this ->createAutowiredDefinition (new \ReflectionClass ($ class ));
333
+ } catch (\ReflectionException $ e ) {
334
+ continue ;
335
+ } catch (RuntimeException $ e ) {
336
+ continue ;
337
+ }
338
+ }
339
+
340
+ $ overridenGetters [$ reflectionMethod ->name ] = $ value ;
341
+ }
342
+
343
+ return $ overridenGetters ;
344
+ }
345
+
295
346
/**
296
347
* Populates the list of available types.
297
348
*/
0 commit comments