Skip to content

Commit

Permalink
feature #22362 [DI] Populate class of ChildDefinition when its id mat…
Browse files Browse the repository at this point in the history
…ches an existing FQCN (nicolas-grekas)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[DI] Populate class of ChildDefinition when its id matches an existing FQCN

| Q             | A
| ------------- | ---
| Branch?       | 3.3
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | yes
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #22345
| License       | MIT
| Doc PR        | -

See linked issue for expected DX.
Instead of doing a "continue", let's throw to force the resolution of any ambiguities.
There is a minor potential BC Break, if one uses an ambiguous FQCN as child-definition id in 3.2 or below.
To me, this should be rare, and easy to fix (compile time only).
The DX enhancement this PR provides looks worth it.

> Service definition "App\Foo\Child" has a parent but no class, and its name looks like a FQCN. Either the class is missing or you want to inherit it from the parent service. To resolve this ambiguity, please rename this service to a non-FQCN (e.g. using dots), or create the missing class.

Commits
-------

3200d37 [DI] Populate class of ChildDefinition when its id matches an existing FQCN
  • Loading branch information
fabpot committed Apr 11, 2017
2 parents bbb0d5e + 3200d37 commit 8807eaf
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;

/**
* @author Nicolas Grekas <p@tchwork.com>
Expand All @@ -27,10 +28,13 @@ class ResolveClassPass implements CompilerPassInterface
public function process(ContainerBuilder $container)
{
foreach ($container->getDefinitions() as $id => $definition) {
if ($definition instanceof ChildDefinition || $definition->isSynthetic() || null !== $definition->getClass()) {
if ($definition->isSynthetic() || null !== $definition->getClass()) {
continue;
}
if (preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)++$/', $id)) {
if ($definition instanceof ChildDefinition && !class_exists($id)) {
throw new InvalidArgumentException(sprintf('Service definition "%s" has a parent but no class, and its name looks like a FQCN. Either the class is missing or you want to inherit it from the parent service. To resolve this ambiguity, please rename this service to a non-FQCN (e.g. using dots), or create the missing class.', $id));
}
$this->changes[strtolower($id)] = $id;
$definition->setClass($id);
}
Expand Down
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\DependencyInjection\Tests\Compiler;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\ResolveClassPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
Expand All @@ -23,11 +24,10 @@ class ResolveClassPassTest extends TestCase
*/
public function testResolveClassFromId($serviceId)
{
$pass = new ResolveClassPass();
$container = new ContainerBuilder();
$def = $container->register($serviceId);

$pass->process($container);
(new ResolveClassPass())->process($container);

$this->assertSame($serviceId, $def->getClass());
}
Expand All @@ -43,11 +43,10 @@ public function provideValidClassId()
*/
public function testWontResolveClassFromId($serviceId)
{
$pass = new ResolveClassPass();
$container = new ContainerBuilder();
$def = $container->register($serviceId);

$pass->process($container);
(new ResolveClassPass())->process($container);

$this->assertNull($def->getClass());
}
Expand All @@ -58,4 +57,41 @@ public function provideInvalidClassId()
yield array('bar');
yield array('\DateTime');
}

public function testNonFqcnChildDefinition()
{
$container = new ContainerBuilder();
$parent = $container->register('App\Foo', null);
$child = $container->setDefinition('App\Foo.child', new ChildDefinition('App\Foo'));

(new ResolveClassPass())->process($container);

$this->assertSame('App\Foo', $parent->getClass());
$this->assertNull($child->getClass());
}

public function testClassFoundChildDefinition()
{
$container = new ContainerBuilder();
$parent = $container->register('App\Foo', null);
$child = $container->setDefinition(self::class, new ChildDefinition('App\Foo'));

(new ResolveClassPass())->process($container);

$this->assertSame('App\Foo', $parent->getClass());
$this->assertSame(self::class, $child->getClass());
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage Service definition "App\Foo\Child" has a parent but no class, and its name looks like a FQCN. Either the class is missing or you want to inherit it from the parent service. To resolve this ambiguity, please rename this service to a non-FQCN (e.g. using dots), or create the missing class.
*/
public function testAmbiguousChildDefinition()
{
$container = new ContainerBuilder();
$parent = $container->register('App\Foo', null);
$child = $container->setDefinition('App\Foo\Child', new ChildDefinition('App\Foo'));

(new ResolveClassPass())->process($container);
}
}

0 comments on commit 8807eaf

Please sign in to comment.