@@ -108,6 +108,10 @@ private function completeDefinition($id, Definition $definition, array $autowire
108
108
$ methodsCalled [$ methodCall [0 ]] = true ;
109
109
}
110
110
111
+ foreach ($ definition ->getOverriddenGetters () as $ overriddenGetter => $ returnValue ) {
112
+ $ methodsCalled [$ overriddenGetter ] = true ;
113
+ }
114
+
111
115
foreach ($ this ->getMethodsToAutowire ($ id , $ reflectionClass , $ autowiredMethods ) as $ reflectionMethod ) {
112
116
if (!isset ($ methodsCalled [$ reflectionMethod ->name ])) {
113
117
$ this ->autowireMethod ($ id , $ definition , $ reflectionMethod );
@@ -132,7 +136,7 @@ private function getMethodsToAutowire($id, \ReflectionClass $reflectionClass, ar
132
136
$ regexList [] = '/^ ' .str_replace ('\* ' , '.* ' , preg_quote ($ pattern , '/ ' )).'$/i ' ;
133
137
}
134
138
135
- foreach ($ reflectionClass ->getMethods (\ReflectionMethod::IS_PUBLIC ) as $ reflectionMethod ) {
139
+ foreach ($ reflectionClass ->getMethods (\ReflectionMethod::IS_PUBLIC | \ReflectionMethod:: IS_PROTECTED ) as $ reflectionMethod ) {
136
140
if ($ reflectionMethod ->isStatic ()) {
137
141
continue ;
138
142
}
@@ -164,6 +168,19 @@ private function getMethodsToAutowire($id, \ReflectionClass $reflectionClass, ar
164
168
*/
165
169
private function autowireMethod ($ id , Definition $ definition , \ReflectionMethod $ reflectionMethod )
166
170
{
171
+ if (null === $ this ->types ) {
172
+ $ this ->populateAvailableTypes ();
173
+ }
174
+
175
+ if ($ this ->overrideGetter ($ id , $ definition , $ reflectionMethod )) {
176
+ return ;
177
+ }
178
+
179
+ if ($ reflectionMethod ->isProtected ()) {
180
+ // Only getter overriding is supported for protected methods
181
+ return ;
182
+ }
183
+
167
184
if ($ isConstructor = $ reflectionMethod ->isConstructor ()) {
168
185
$ arguments = $ definition ->getArguments ();
169
186
} else {
@@ -193,10 +210,6 @@ private function autowireMethod($id, Definition $definition, \ReflectionMethod $
193
210
continue ;
194
211
}
195
212
196
- if (null === $ this ->types ) {
197
- $ this ->populateAvailableTypes ();
198
- }
199
-
200
213
if (isset ($ this ->types [$ typeHint ->name ])) {
201
214
$ value = new Reference ($ this ->types [$ typeHint ->name ]);
202
215
$ addMethodCall = true ;
@@ -247,6 +260,43 @@ private function autowireMethod($id, Definition $definition, \ReflectionMethod $
247
260
}
248
261
}
249
262
263
+ /**
264
+ * Getter injection.
265
+ *
266
+ * @param string $id
267
+ * @param Definition $definition
268
+ * @param \ReflectionMethod $reflectionMethod
269
+ *
270
+ * @return bool
271
+ */
272
+ private function overrideGetter ($ id , Definition $ definition , \ReflectionMethod $ reflectionMethod )
273
+ {
274
+ if (!method_exists ($ reflectionMethod , 'getReturnType ' )) {
275
+ return false ;
276
+ }
277
+
278
+ if (0 !== $ reflectionMethod ->getNumberOfParameters () || $ reflectionMethod ->isFinal () || $ reflectionMethod ->returnsReference () || !($ returnType = $ reflectionMethod ->getReturnType ())) {
279
+ return false ;
280
+ }
281
+
282
+ $ class = (string ) $ returnType ;
283
+ if (isset ($ this ->types [$ class ])) {
284
+ $ value = new Reference ($ this ->types [$ class ]);
285
+ } else {
286
+ try {
287
+ $ value = $ this ->createAutowiredDefinition (new \ReflectionClass ($ class ), $ id );
288
+ } catch (\ReflectionException $ e ) {
289
+ return false ;
290
+ } catch (RuntimeException $ e ) {
291
+ return false ;
292
+ }
293
+ }
294
+
295
+ $ definition ->setOverriddenGetter ($ reflectionMethod ->name , $ value );
296
+
297
+ return true ;
298
+ }
299
+
250
300
/**
251
301
* Populates the list of available types.
252
302
*/
0 commit comments