Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

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
Apr 11, 2017
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 @@ -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
Original file line number Diff line number Diff line change
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);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.