[DependencyInjection] Simplified code in AutowirePass

This commit is contained in:
Martin Hasoň 2016-02-23 10:13:51 +01:00
parent 7cab4f24ae
commit ce0357e2a5
2 changed files with 19 additions and 37 deletions

View File

@ -148,45 +148,17 @@ class AutowirePass implements CompilerPassInterface
$this->types[$type] = $id;
}
// Cannot use reflection if the class isn't set
if (!$definition->getClass()) {
if (!$reflectionClass = $this->getReflectionClass($id, $definition)) {
return;
}
if ($reflectionClass = $this->getReflectionClass($id, $definition)) {
$this->extractInterfaces($id, $reflectionClass);
$this->extractAncestors($id, $reflectionClass);
foreach ($reflectionClass->getInterfaces() as $reflectionInterface) {
$this->set($reflectionInterface->name, $id);
}
}
/**
* Extracts the list of all interfaces implemented by a class.
*
* @param string $id
* @param \ReflectionClass $reflectionClass
*/
private function extractInterfaces($id, \ReflectionClass $reflectionClass)
{
foreach ($reflectionClass->getInterfaces() as $interfaceName => $reflectionInterface) {
$this->set($interfaceName, $id);
$this->extractInterfaces($id, $reflectionInterface);
}
}
/**
* Extracts all inherited types of a class.
*
* @param string $id
* @param \ReflectionClass $reflectionClass
*/
private function extractAncestors($id, \ReflectionClass $reflectionClass)
{
$this->set($reflectionClass->name, $id);
if ($reflectionParentClass = $reflectionClass->getParentClass()) {
$this->extractAncestors($id, $reflectionParentClass);
}
do {
$this->set($reflectionClass->name, $id);
} while ($reflectionClass = $reflectionClass->getParentClass());
}
/**
@ -256,6 +228,7 @@ class AutowirePass implements CompilerPassInterface
return $this->reflectionClasses[$id];
}
// Cannot use reflection if the class isn't set
if (!$class = $definition->getClass()) {
return;
}

View File

@ -61,9 +61,10 @@ class AutowirePassTest extends \PHPUnit_Framework_TestCase
$pass = new AutowirePass();
$pass->process($container);
$this->assertCount(2, $container->getDefinition('g')->getArguments());
$this->assertCount(3, $container->getDefinition('g')->getArguments());
$this->assertEquals('f', (string) $container->getDefinition('g')->getArgument(0));
$this->assertEquals('f', (string) $container->getDefinition('g')->getArgument(1));
$this->assertEquals('f', (string) $container->getDefinition('g')->getArgument(2));
}
public function testCompleteExistingDefinition()
@ -317,13 +318,21 @@ interface EInterface extends DInterface
{
}
class F implements EInterface
interface IInterface
{
}
class I implements IInterface
{
}
class F extends I implements EInterface
{
}
class G
{
public function __construct(DInterface $d, EInterface $e)
public function __construct(DInterface $d, EInterface $e, IInterface $i)
{
}
}