Skip to content

[DependencyInjection] Resolve ChildDefinition in AbstractRecursivePass #44418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\LogicException;
Expand Down Expand Up @@ -131,25 +132,35 @@ protected function getConstructor(Definition $definition, $required)

if ($factory) {
[$class, $method] = $factory;

if ('__construct' === $method) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's throw as soon as possible?

throw new RuntimeException(sprintf('Invalid service "%s": "__construct()" cannot be used as a factory method.', $this->currentId));
}

if ($class instanceof Reference) {
$class = $this->container->findDefinition((string) $class)->getClass();
$factoryDefinition = $this->container->findDefinition((string) $class);
while ((null === $class = $factoryDefinition->getClass()) && $factoryDefinition instanceof ChildDefinition) {
$factoryDefinition = $this->container->findDefinition($factoryDefinition->getParent());
}
} elseif ($class instanceof Definition) {
$class = $class->getClass();
} elseif (null === $class) {
$class = $definition->getClass();
}

if ('__construct' === $method) {
throw new RuntimeException(sprintf('Invalid service "%s": "__construct()" cannot be used as a factory method.', $this->currentId));
}

return $this->getReflectionMethod(new Definition($class), $method);
}

$class = $definition->getClass();
while ((null === $class = $definition->getClass()) && $definition instanceof ChildDefinition) {
$definition = $this->container->findDefinition($definition->getParent());
}

try {
if (!$r = $this->container->getReflectionClass($class)) {
if (null === $class) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a minor DX improvement. If the class is null this message looks better to me than Invalid service "foo": class "" does not exist. (see getReflectionMethod() below where we already check if a class is set or not).

throw new RuntimeException(sprintf('Invalid service "%s": the class is not set.', $this->currentId));
}

throw new RuntimeException(sprintf('Invalid service "%s": class "%s" does not exist.', $this->currentId, $class));
}
} catch (\ReflectionException $e) {
Expand Down Expand Up @@ -179,7 +190,11 @@ protected function getReflectionMethod(Definition $definition, $method)
return $this->getConstructor($definition, true);
}

if (!$class = $definition->getClass()) {
while ((null === $class = $definition->getClass()) && $definition instanceof ChildDefinition) {
$definition = $this->container->findDefinition($definition->getParent());
}

if (null === $class) {
throw new RuntimeException(sprintf('Invalid service "%s": the class is not set.', $this->currentId));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?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\Compiler;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Tests\Fixtures\Bar;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FactoryDummy;

class AbstractRecursivePassTest extends TestCase
{
public function testGetConstructorResolvesFactoryChildDefinitionsClass()
{
$container = new ContainerBuilder();
$container->setParameter('factory_dummy_class', FactoryDummy::class);
$container
->register('parent', '%factory_dummy_class%')
->setAbstract(true);
$container->setDefinition('child', new ChildDefinition('parent'));
$container
->register('foo', \stdClass::class)
->setFactory([new Reference('child'), 'createFactory']);

$pass = new class() extends AbstractRecursivePass {
public $actual;

protected function processValue($value, $isRoot = false)
{
if ($value instanceof Definition && 'foo' === $this->currentId) {
$this->actual = $this->getConstructor($value, true);
}

return parent::processValue($value, $isRoot);
}
};
$pass->process($container);

$this->assertInstanceOf(\ReflectionMethod::class, $pass->actual);
$this->assertSame(FactoryDummy::class, $pass->actual->class);
}

public function testGetConstructorResolvesChildDefinitionsClass()
{
$container = new ContainerBuilder();
$container
->register('parent', Bar::class)
->setAbstract(true);
$container->setDefinition('foo', new ChildDefinition('parent'));

$pass = new class() extends AbstractRecursivePass {
public $actual;

protected function processValue($value, $isRoot = false)
{
if ($value instanceof Definition && 'foo' === $this->currentId) {
$this->actual = $this->getConstructor($value, true);
}

return parent::processValue($value, $isRoot);
}
};
$pass->process($container);

$this->assertInstanceOf(\ReflectionMethod::class, $pass->actual);
$this->assertSame(Bar::class, $pass->actual->class);
}

public function testGetReflectionMethodResolvesChildDefinitionsClass()
{
$container = new ContainerBuilder();
$container
->register('parent', Bar::class)
->setAbstract(true);
$container->setDefinition('foo', new ChildDefinition('parent'));

$pass = new class() extends AbstractRecursivePass {
public $actual;

protected function processValue($value, $isRoot = false)
{
if ($value instanceof Definition && 'foo' === $this->currentId) {
$this->actual = $this->getReflectionMethod($value, 'create');
}

return parent::processValue($value, $isRoot);
}
};
$pass->process($container);

$this->assertInstanceOf(\ReflectionMethod::class, $pass->actual);
$this->assertSame(Bar::class, $pass->actual->class);
}

public function testGetConstructorDefinitionNoClass()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Invalid service "foo": the class is not set.');

$container = new ContainerBuilder();
$container->register('foo');

(new class() extends AbstractRecursivePass {
protected function processValue($value, $isRoot = false)
{
if ($value instanceof Definition && 'foo' === $this->currentId) {
$this->getConstructor($value, true);
}

return parent::processValue($value, $isRoot);
}
})->process($container);
}
}