[DI] Always consider abstract getters as autowiring candidates

This commit is contained in:
Nicolas Grekas 2017-02-13 20:55:21 +01:00
parent b5d9b3b99e
commit 8f246bde1d
3 changed files with 47 additions and 1 deletions

View File

@ -159,6 +159,10 @@ class AutowirePass extends AbstractRecursivePass
continue 2;
}
}
if ($reflectionMethod->isAbstract() && !$reflectionMethod->getNumberOfParameters()) {
$methodsToAutowire[strtolower($reflectionMethod->name)] = $reflectionMethod;
}
}
if ($notFound = array_diff($autowiredMethods, $found)) {
@ -478,7 +482,7 @@ class AutowirePass extends AbstractRecursivePass
$this->populateAvailableType($argumentId, $argumentDefinition);
try {
$this->processValue($argumentDefinition, true);
$this->processValue($argumentDefinition);
$this->currentId = $currentId;
} catch (RuntimeException $e) {
$classOrInterface = $typeHint->isInterface() ? 'interface' : 'class';

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\DependencyInjection\Tests\Compiler;
use Symfony\Component\DependencyInjection\Compiler\AutowirePass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Tests\Fixtures\AbstractGetterOverriding;
use Symfony\Component\DependencyInjection\Tests\Fixtures\includes\FooVariadic;
use Symfony\Component\DependencyInjection\Tests\Fixtures\GetterOverriding;
@ -517,6 +518,27 @@ class AutowirePassTest extends \PHPUnit_Framework_TestCase
);
}
/**
* @requires PHP 7.0
*/
public function testAbstractGetterOverriding()
{
$container = new ContainerBuilder();
$container
->register('getter_overriding', AbstractGetterOverriding::class)
->setAutowired(true)
;
$pass = new AutowirePass();
$pass->process($container);
$overridenGetters = $container->getDefinition('getter_overriding')->getOverriddenGetters();
$this->assertEquals(array(
'abstractgetfoo' => new Reference('autowired.Symfony\Component\DependencyInjection\Tests\Compiler\Foo'),
), $overridenGetters);
}
/**
* @requires PHP 7.1
*/

View File

@ -0,0 +1,20 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
use Symfony\Component\DependencyInjection\Tests\Compiler\Foo;
abstract class AbstractGetterOverriding
{
abstract public function abstractGetFoo(): Foo;
abstract public function abstractDoFoo($arg = null): Foo;
}