[DI][DX] Do not map id to class for global classes

This commit is contained in:
Maxime Steinhausser 2017-01-29 16:34:49 +01:00
parent bcf8b68be9
commit bb870304f0
6 changed files with 81 additions and 11 deletions

View File

@ -30,7 +30,7 @@ class ResolveClassPass implements CompilerPassInterface
if ($definition instanceof ChildDefinition || $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 (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)) {
$this->changes[strtolower($id)] = $id;
$definition->setClass($id);
}

View File

@ -0,0 +1,60 @@
<?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 Symfony\Component\DependencyInjection\Compiler\ResolveClassPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
class ResolveClassPassTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider provideValidClassId
*/
public function testResolveClassFromId($serviceId)
{
$pass = new ResolveClassPass();
$container = new ContainerBuilder();
$def = $container->register($serviceId);
$pass->process($container);
$this->assertSame($serviceId, $def->getClass());
}
public function provideValidClassId()
{
yield array('Acme\UnknownClass');
yield array(CaseSensitiveClass::class);
}
/**
* @dataProvider provideInvalidClassId
*/
public function testWontResolveClassFromId($serviceId)
{
$pass = new ResolveClassPass();
$container = new ContainerBuilder();
$def = $container->register($serviceId);
$pass->process($container);
$this->assertNull($def->getClass());
}
public function provideInvalidClassId()
{
yield array(\stdClass::class);
yield array('bar');
yield array('\DateTime');
}
}

View File

@ -932,16 +932,26 @@ class ContainerBuilderTest extends \PHPUnit_Framework_TestCase
{
$container = new ContainerBuilder();
$unknown = $container->register('unknown_class');
$class = $container->register(\stdClass::class);
$unknown = $container->register('Acme\UnknownClass');
$autoloadClass = $container->register(CaseSensitiveClass::class);
$container->compile();
$this->assertSame('unknown_class', $unknown->getClass());
$this->assertEquals(\stdClass::class, $class->getClass());
$this->assertSame('Acme\UnknownClass', $unknown->getClass());
$this->assertEquals(CaseSensitiveClass::class, $autoloadClass->getClass());
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage The definition for "DateTime" has no class.
*/
public function testNoClassFromGlobalNamespaceClassId()
{
$container = new ContainerBuilder();
$definition = $container->register(\DateTime::class);
$container->compile();
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage The definition for "123_abc" has no class.

View File

@ -40,4 +40,4 @@ services:
with_defaults_aliased_short: '@with_defaults'
with_shortcut_args: [foo]
Acme\WithShortCutArgs: [foo]

View File

@ -40,4 +40,4 @@ services:
new_factory2: { class: FooBarClass, factory: ['@baz', getClass]}
new_factory3: { class: FooBarClass, factory: [BazClass, getInstance]}
new_factory4: { class: BazClass, factory: [~, getInstance]}
with_shortcut_args: [foo, '@baz']
Acme\WithShortCutArgs: [foo, '@baz']

View File

@ -154,7 +154,7 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array(new Reference('baz'), 'getClass'), $services['new_factory2']->getFactory(), '->load() parses the factory tag');
$this->assertEquals(array('BazClass', 'getInstance'), $services['new_factory3']->getFactory(), '->load() parses the factory tag');
$this->assertSame(array(null, 'getInstance'), $services['new_factory4']->getFactory(), '->load() accepts factory tag without class');
$this->assertEquals(array('foo', new Reference('baz')), $services['with_shortcut_args']->getArguments(), '->load() parses short service definition');
$this->assertEquals(array('foo', new Reference('baz')), $services['Acme\WithShortCutArgs']->getArguments(), '->load() parses short service definition');
$aliases = $container->getAliases();
$this->assertTrue(isset($aliases['alias_for_foo']), '->load() parses aliases');
@ -385,9 +385,9 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
$this->assertArrayNotHasKey('public', $container->getDefinition('no_defaults_child')->getChanges());
$this->assertArrayNotHasKey('autowire', $container->getDefinition('no_defaults_child')->getChanges());
$this->assertFalse($container->getDefinition('with_shortcut_args')->isPublic());
$this->assertSame(array('foo' => array(array())), $container->getDefinition('with_shortcut_args')->getTags());
$this->assertTrue($container->getDefinition('with_shortcut_args')->isAutowired());
$this->assertFalse($container->getDefinition('Acme\WithShortCutArgs')->isPublic());
$this->assertSame(array('foo' => array(array())), $container->getDefinition('Acme\WithShortCutArgs')->getTags());
$this->assertTrue($container->getDefinition('Acme\WithShortCutArgs')->isAutowired());
$container->compile();