8000 [DI][DX] Do not map id to class for global classes · symfony/symfony@bb87030 · GitHub
[go: up one dir, main page]

Skip to content

Commit bb87030

Browse files
committed
[DI][DX] Do not map id to class for global classes
1 parent bcf8b68 commit bb87030

File tree

6 files changed

+81
-11
lines changed

6 files changed

+81
-11
lines changed

src/Symfony/Component/DependencyInjection/Compiler/ResolveClassPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function process(ContainerBuilder $container)
3030
if ($definition instanceof ChildDefinition || $definition->isSynthetic() || null !== $definition->getClass()) {
3131
continue;
3232
}
33-
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)) {
33+
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)) {
3434
$this->changes[strtolower($id)] = $id;
3535
$definition->setClass($id);
3636
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\ResolveClassPass;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
17+
18+
class ResolveClassPassTest extends \PHPUnit_Framework_TestCase
19+
{
20+
/**
21+
* @dataProvider provideValidClassId
22+
*/
23+
public function testResolveClassFromId($serviceId)
24+
{
25+
$pass = new ResolveClassPass();
26+
$container = new ContainerBuilder();
27+
$def = $container->register($serviceId);
28+
29+
$pass->process($container);
30+
31+
$this->assertSame($serviceId, $def->getClass());
32+
}
33+
34+
public function provideValidClassId()
35+
{
36+
yield array('Acme\UnknownClass');
37+
yield array(CaseSensitiveClass::class);
38+
}
39+
40+
/**
41+
* @dataProvider provideInvalidClassId
42+
*/
43+
public function testWontResolveClassFromId($serviceId)
44+
{
45+
$pass = new ResolveClassPass();
46+
$container = new ContainerBuilder();
47+
$def = $container->register($serviceId);
48+
49+
$pass->process($container);
50+
51+
$this->assertNull($def->getClass());
52+
}
53+
54+
public function provideInvalidClassId()
55+
{
56+
yield array(\stdClass::class);
57+
yield array('bar');
58+
yield array('\DateTime');
59+
}
60+
}

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -932,16 +932,26 @@ public function testClassFromId()
932932
{
933933
$container = new ContainerBuilder();
934934

935-
$unknown = $container->register('unknown_class');
936-
$class = $container->register(\stdClass::class);
935+
$unknown = $container->register('Acme\UnknownClass');
937936
$autoloadClass = $container->register(CaseSensitiveClass::class);
938937
$container->compile();
939938

940-
$this->assertSame('unknown_class', $unknown->getClass());
941-
$this->assertEquals(\stdClass::class, $class->getClass());
939+
$this->assertSame('Acme\UnknownClass', $unknown->getClass());
942940
$this->assertEquals(CaseSensitiveClass::class, $autoloadClass->getClass());
943941
}
944942

943+
/**
944+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
945+
* @expectedExceptionMessage The definition for "DateTime" has no class.
946+
*/
947+
public function testNoClassFromGlobalNamespaceClassId()
948+
{
949+
$container = new ContainerBuilder();
950+
951+
$definition = $container->register(\DateTime::class);
952+
$container->compile();
953+
}
954+
945955
/**
946956
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
947957
* @expectedExceptionMessage The definition for "123_abc" has no class.

src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services28.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ services:
4040

4141
with_defaults_aliased_short: '@with_defaults'
4242

43-
with_shortcut_args: [foo]
43+
Acme\WithShortCutArgs: [foo]

src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services6.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ services:
4040
new_factory2: { class: FooBarClass, factory: ['@baz', getClass]}
4141
new_factory3: { class: FooBarClass, factory: [BazClass, getInstance]}
4242
new_factory4: { class: BazClass, factory: [~, getInstance]}
43-
with_shortcut_args: [foo, '@baz']
43+
Acme\WithShortCutArgs: [foo, '@baz']

src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public function testLoadServices()
154154
$this->assertEquals(array(new Reference('baz'), 'getClass'), $services['new_factory2']->getFactory(), '->load() parses the factory tag');
155155
$this->assertEquals(array('BazClass', 'getInstance'), $services['new_factory3']->getFactory(), '->load() parses the factory tag');
156156
$this->assertSame(array(null, 'getInstance'), $services['new_factory4']->getFactory(), '->load() accepts factory tag without class');
157-
$this->assertEquals(array('foo', new Reference('baz')), $services['with_shortcut_args']->getArguments(), '->load() parses short service definition');
157+
$this->assertEquals(array('foo', new Reference('baz')), $services['Acme\WithShortCutArgs']->getArguments(), '->load() parses short service definition');
158158

159159
$aliases = $container->getAliases();
160160
$this->assertTrue(isset($aliases['alias_for_foo']), '->load() parses aliases');
@@ -385,9 +385,9 @@ public function testDefaults()
385385
$this->assertArrayNotHasKey('public', $container->getDefinition('no_defaults_child')->getChanges());
386386
$this->assertArrayNotHasKey('autowire', $container->getDefinition('no_defaults_child')->getChanges());
387387

388-
$this->assertFalse($container->getDefinition('with_shortcut_args')->isPublic());
389-
$this->assertSame(array('foo' => array(array())), $container->getDefinition('with_shortcut_args')->getTags());
390-
$this->assertTrue($container->getDefinition('with_shortcut_args')->isAutowired());
388+
$this->assertFalse($container->getDefinition('Acme\WithShortCutArgs')->isPublic());
389+
$this->assertSame(array('foo' => array(array())), $container->getDefinition('Acme\WithShortCutArgs')->getTags());
390+
$this->assertTrue($container->getDefinition('Acme\WithShortCutArgs')->isAutowired());
391391

392392
$container->compile();
393393

0 commit comments

Comments
 (0)
0