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