8000 [DI] Always consider abstract getters as autowiring candidates · Deamon/symfony@8f246bd · GitHub
[go: up one dir, main page]

Skip to content

Commit 8f246bd

Browse files
[DI] Always consider abstract getters as autowiring candidates
1 parent b5d9b3b commit 8f246bd

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ private function getMethodsToAutowire(\ReflectionClass $reflectionClass, array $
159159
continue 2;
160160
}
161161
}
162+
163+
if ($reflectionMethod->isAbstract() && !$reflectionMethod->getNumberOfParameters()) {
164+
$methodsToAutowire[strtolower($reflectionMethod->name)] = $reflectionMethod;
165+
}
162166
}
163167

164168
if ($notFound = array_diff($autowiredMethods, $found)) {
@@ -478,7 +482,7 @@ private function createAutowiredDefinition(\ReflectionClass $typeHint)
478482
$this->populateAvailableType($argumentId, $argumentDefinition);
479483

480484
try {
481-
$this->processValue($argumentDefinition, true);
485+
$this->processValue($argumentDefinition);
482486
$this->currentId = $currentId;
483487
} catch (RuntimeException $e) {
484488
$classOrInterface = $typeHint->isInterface() ? 'interface' : 'class';

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\DependencyInjection\Compiler\AutowirePass;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\DependencyInjection\Reference;
17+
use Symfony\Component\DependencyInjection\Tests\Fixtures\AbstractGetterOverriding;
1718
use Symfony\Component\DependencyInjection\Tests\Fixtures\includes\FooVariadic;
1819
use Symfony\Component\DependencyInjection\Tests\Fixtures\GetterOverriding;
1920

@@ -517,6 +518,27 @@ public function testExplicitMethodInjection()
517518
);
518519
}
519520

521+
/**
522+
* @requires PHP 7.0
523+
*/
524+
public function testAbstractGetterOverriding()
525+
{
526+
$container = new ContainerBuilder();
527+
528+
$container
529+
->register('getter_overriding', AbstractGetterOverriding::class)
530+
->setAutowired(true)
531+
;
532+
533+
$pass = new AutowirePass();
534+
$pass->process($container);
535+
536+
$overridenGetters = $container->getDefinition('getter_overriding')->getOverriddenGetters();
537+
$this->assertEquals(array(
538+
'abstractgetfoo' => new Reference('autowired.Symfony\Component\DependencyInjection\Tests\Compiler\Foo'),
539+
), $overridenGetters);
540+
}
541+
520542
/**
521543
* @requires PHP 7.1
522544
*/
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
13+
14+
use Symfony\Component\DependencyInjection\Tests\Compiler\Foo;
15+
16+
abstract class AbstractGetterOverriding
17+
{
18+
abstract public function abstractGetFoo(): Foo;
19+
abstract public function abstractDoFoo($arg = null): Foo;
20+
}

0 commit comments

Comments
 (0)
0