diff --git a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php index 46fcdc873ea0e..9ebfb27322d68 100644 --- a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php +++ b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php @@ -759,6 +759,10 @@ public function setDefinition($id, Definition $definition) throw new BadMethodCallException('Adding definition to a frozen container is not allowed'); } + if (null === $definition->getClass() && class_exists($id)) { + $definition->setClass($id); + } + $id = strtolower($id); unset($this->aliasDefinitions[$id]); diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php index e59b0826b5631..f80d5e5f3063d 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php @@ -29,6 +29,7 @@ use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag; use Symfony\Component\Config\Resource\FileResource; use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition; +use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass; use Symfony\Component\ExpressionLanguage\Expression; class ContainerBuilderTest extends \PHPUnit_Framework_TestCase @@ -808,6 +809,19 @@ public function testAutowiring() $this->assertEquals('a', (string) $container->getDefinition('b')->getArgument(0)); } + + public function testClassFromId() + { + $container = new ContainerBuilder(); + + $unknown = $container->register('unknown_class'); + $class = $container->register(\stdClass::class); + $autoloadClass = $container->register(CaseSensitiveClass::class); + + $this->assertNull($unknown->getClass()); + $this->assertEquals(\stdClass::class, $class->getClass()); + $this->assertEquals(CaseSensitiveClass::class, $autoloadClass->getClass()); + } } class FooClass diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/CaseSensitiveClass.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/CaseSensitiveClass.php new file mode 100644 index 0000000000000..87b258429e6a3 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/CaseSensitiveClass.php @@ -0,0 +1,16 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Tests\Fixtures; + +class CaseSensitiveClass +{ +} diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/class_from_id.xml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/class_from_id.xml new file mode 100644 index 0000000000000..45415cce472f0 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/class_from_id.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/class_from_id.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/class_from_id.yml new file mode 100644 index 0000000000000..33f0b2ea6821f --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/class_from_id.yml @@ -0,0 +1,3 @@ +services: + Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass: + autowire: true diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php index 5a166d963dc67..96159a3acea48 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php @@ -19,6 +19,7 @@ use Symfony\Component\DependencyInjection\Loader\IniFileLoader; use Symfony\Component\Config\Loader\LoaderResolver; use Symfony\Component\Config\FileLocator; +use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass; use Symfony\Component\ExpressionLanguage\Expression; class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase @@ -556,6 +557,15 @@ public function testAutowire() $this->assertTrue($container->getDefinition('bar')->isAutowired()); } + public function testClassFromId() + { + $container = new ContainerBuilder(); + $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml')); + $loader->load('class_from_id.xml'); + + $this->assertEquals(CaseSensitiveClass::class, $container->getDefinition(CaseSensitiveClass::class)->getClass()); + } + /** * @group legacy * @expectedDeprecation Using the attribute "class" is deprecated for the service "bar" which is defined as an alias %s. diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php index fabaa4859c578..19861c2524d4c 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php @@ -19,6 +19,7 @@ use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; use Symfony\Component\Config\Loader\LoaderResolver; use Symfony\Component\Config\FileLocator; +use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass; use Symfony\Component\ExpressionLanguage\Expression; class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase @@ -326,6 +327,15 @@ public function testAutowire() $this->assertTrue($container->getDefinition('bar_service')->isAutowired()); } + public function testClassFromId() + { + $container = new ContainerBuilder(); + $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')); + $loader->load('class_from_id.yml'); + + $this->assertEquals(CaseSensitiveClass::class, $container->getDefinition(CaseSensitiveClass::class)->getClass()); + } + /** * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException * @expectedExceptionMessage The value of the "decorates" option for the "bar" service must be the id of the service without the "@" prefix (replace "@foo" with "foo").