Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitffa556f

Browse files
committed
[DI] Add support for getter autowiring
1 parent9e6d6ba commitffa556f

File tree

3 files changed

+147
-5
lines changed

3 files changed

+147
-5
lines changed

‎src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php‎

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ private function completeDefinition($id, Definition $definition, array $autowire
108108
$methodsCalled[$methodCall[0]] =true;
109109
}
110110

111+
foreach ($definition->getOverriddenGetters()as$overriddenGetter =>$returnValue) {
112+
$methodsCalled[$overriddenGetter] =true;
113+
}
114+
111115
foreach ($this->getMethodsToAutowire($id,$reflectionClass,$autowiredMethods)as$reflectionMethod) {
112116
if (!isset($methodsCalled[$reflectionMethod->name])) {
113117
$this->autowireMethod($id,$definition,$reflectionMethod);
@@ -132,7 +136,7 @@ private function getMethodsToAutowire($id, \ReflectionClass $reflectionClass, ar
132136
$regexList[] ='/^'.str_replace('\*','.*',preg_quote($pattern,'/')).'$/i';
133137
}
134138

135-
foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC)as$reflectionMethod) {
139+
foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED)as$reflectionMethod) {
136140
if ($reflectionMethod->isStatic()) {
137141
continue;
138142
}
@@ -164,6 +168,19 @@ private function getMethodsToAutowire($id, \ReflectionClass $reflectionClass, ar
164168
*/
165169
privatefunctionautowireMethod($id,Definition$definition,\ReflectionMethod$reflectionMethod)
166170
{
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+
167184
if ($isConstructor =$reflectionMethod->isConstructor()) {
168185
$arguments =$definition->getArguments();
169186
}else {
@@ -193,10 +210,6 @@ private function autowireMethod($id, Definition $definition, \ReflectionMethod $
193210
continue;
194211
}
195212

196-
if (null ===$this->types) {
197-
$this->populateAvailableTypes();
198-
}
199-
200213
if (isset($this->types[$typeHint->name])) {
201214
$value =newReference($this->types[$typeHint->name]);
202215
$addMethodCall =true;
@@ -247,6 +260,43 @@ private function autowireMethod($id, Definition $definition, \ReflectionMethod $
247260
}
248261
}
249262

263+
/**
264+
* Getter injection.
265+
*
266+
* @param string $id
267+
* @param Definition $definition
268+
* @param \ReflectionMethod $reflectionMethod
269+
*
270+
* @return bool
271+
*/
272+
privatefunctionoverrideGetter($id,Definition$definition,\ReflectionMethod$reflectionMethod)
273+
{
274+
if (!method_exists($reflectionMethod,'getReturnType')) {
275+
returnfalse;
276+
}
277+
278+
if (0 !==$reflectionMethod->getNumberOfParameters() ||$reflectionMethod->isFinal() ||$reflectionMethod->returnsReference() || !($returnType =$reflectionMethod->getReturnType())) {
279+
returnfalse;
280+
}
281+
282+
$class = (string)$returnType;
283+
if (isset($this->types[$class])) {
284+
$value =newReference($this->types[$class]);
285+
}else {
286+
try {
287+
$value =$this->createAutowiredDefinition(new \ReflectionClass($class),$id);
288+
}catch (\ReflectionException$e) {
289+
returnfalse;
290+
}catch (RuntimeException$e) {
291+
returnfalse;
292+
}
293+
}
294+
295+
$definition->setOverriddenGetter($reflectionMethod->name,$value);
296+
297+
returntrue;
298+
}
299+
250300
/**
251301
* Populates the list of available types.
252302
*/

‎src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
useSymfony\Component\DependencyInjection\Compiler\AutowirePass;
1515
useSymfony\Component\DependencyInjection\ContainerBuilder;
1616
useSymfony\Component\DependencyInjection\Reference;
17+
useSymfony\Component\DependencyInjection\Tests\Fixtures\GetterOverriding;
18+
useSymfony\Component\HttpFoundation\RequestStack;
1719

1820
/**
1921
* @author Kévin Dunglas <dunglas@gmail.com>
@@ -497,6 +499,31 @@ public function testExplicitMethodInjection()
497499
);
498500
}
499501

502+
/**
503+
* @requires PHP 7.1
504+
*/
505+
publicfunctiontestGetterOverriding()
506+
{
507+
$container =newContainerBuilder();
508+
$container->register('b', B::class);
509+
510+
$container
511+
->register('getter_overriding', GetterOverriding::class)
512+
->setOverriddenGetter('getExplicitlyDefined',newReference('b'))
513+
->setAutowiredMethods(array('get*'))
514+
;
515+
516+
$pass =newAutowirePass();
517+
$pass->process($container);
518+
519+
$overridenGetters =$container->getDefinition('getter_overriding')->getOverriddenGetters();
520+
$this->assertEquals($overridenGetters,array(
521+
'getExplicitlyDefined' =>newReference('b'),
522+
'getFoo' =>newReference('autowired.symfony\component\dependencyinjection\tests\compiler\foo'),
523+
'getBar' =>newReference('autowired.symfony\component\dependencyinjection\tests\compiler\bar'),
524+
));
525+
}
526+
500527
/**
501528
* @dataProvider getCreateResourceTests
502529
*/
@@ -807,6 +834,11 @@ public function notASetter(A $a)
807834
{
808835
// should be called only when explicitly specified
809836
}
837+
838+
protectedfunctionsetProtectedMethod(A$a)
839+
{
840+
// should not be called
841+
}
810842
}
811843

812844
class SetterInjectionCollision
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\DependencyInjection\Tests\Fixtures;
13+
14+
/**
15+
* To test getter autowiring with PHP >= 7.1.
16+
*
17+
* @author Kévin Dunglas <dunglas@gmail.com>
18+
*/
19+
class GetterOverriding
20+
{
21+
publicfunctiongetFoo(): ?Foo
22+
{
23+
// should be called
24+
}
25+
26+
protectedfunctiongetBar():Bar
27+
{
28+
// should be called
29+
}
30+
31+
publicfunctiongetNoTypeHint()
32+
{
33+
// should not be called
34+
}
35+
36+
publicfunctiongetUnknown():NotExist
37+
{
38+
// should not be called
39+
}
40+
41+
publicfunctiongetExplicitlyDefined():B
42+
{
43+
// should be called but not autowired
44+
}
45+
46+
publicfunctiongetScalar():string
47+
{
48+
// should not be called
49+
}
50+
51+
finalpublicfunctiongetFinal():A
52+
{
53+
// should not be called
54+
}
55+
56+
publicfunction &getReference():A
57+
{
58+
// should not be called
59+
}
60+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp