Skip to content

Commit 5f9f981

Browse files
committed
[DependencyInjection] Optional class for class named services
1 parent bda7e6b commit 5f9f981

File tree

7 files changed

+63
-0
lines changed

7 files changed

+63
-0
lines changed

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

+4
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,10 @@ public function setDefinition($id, Definition $definition)
760760
throw new BadMethodCallException('Adding definition to a frozen container is not allowed');
761761
}
762762

763+
if (null === $definition->getClass() && class_exists($id)) {
764+
$definition->setClass($id);
765+
}
766+
763767
$id = strtolower($id);
764768

765769
unset($this->aliasDefinitions[$id]);

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

+14
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
2828
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
2929
use Symfony\Component\Config\Resource\FileResource;
30+
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
3031
use Symfony\Component\ExpressionLanguage\Expression;
3132

3233
class ContainerBuilderTest extends \PHPUnit_Framework_TestCase
@@ -823,6 +824,19 @@ public function testAutowiring()
823824

824825
$this->assertEquals('a', (string) $container->getDefinition('b')->getArgument(0));
825826
}
827+
828+
public function testClassFromId()
829+
{
830+
$container = new ContainerBuilder();
831+
832+
$unknown = $container->register('unknown_class');
833+
$class = $container->register(\stdClass::class);
834+
$autoloadClass = $container->register(CaseSensitiveClass::class);
835+
836+
$this->assertNull($unknown->getClass());
837+
$this->assertEquals(\stdClass::class, $class->getClass());
838+
$this->assertEquals(CaseSensitiveClass::class, $autoloadClass->getClass());
839+
}
826840
}
827841

828842
class FooClass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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\Fixtures;
13+
14+
class CaseSensitiveClass
15+
{
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
3+
<services>
4+
<service id="Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass" />
5+
</services>
6+
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
services:
2+
Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass:
3+
autowire: true

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

+10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
2020
use Symfony\Component\Config\Loader\LoaderResolver;
2121
use Symfony\Component\Config\FileLocator;
22+
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
2223
use Symfony\Component\ExpressionLanguage\Expression;
2324

2425
class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
@@ -555,6 +556,15 @@ public function testAutowire()
555556
$this->assertTrue($container->getDefinition('bar')->isAutowired());
556557
}
557558

559+
public function testClassFromId()
560+
{
561+
$container = new ContainerBuilder();
562+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
563+
$loader->load('class_from_id.xml');
564+
565+
$this->assertEquals(CaseSensitiveClass::class, $container->getDefinition(CaseSensitiveClass::class)->getClass());
566+
}
567+
558568
/**
559569
* @group legacy
560570
* @expectedDeprecation Using the attribute "class" is deprecated for the service "bar" which is defined as an alias %s.

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

+10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
2020
use Symfony\Component\Config\Loader\LoaderResolver;
2121
use Symfony\Component\Config\FileLocator;
22+
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
2223
use Symfony\Component\ExpressionLanguage\Expression;
2324

2425
class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
@@ -325,6 +326,15 @@ public function testAutowire()
325326
$this->assertTrue($container->getDefinition('bar_service')->isAutowired());
326327
}
327328

329+
public function testClassFromId()
330+
{
331+
$container = new ContainerBuilder();
332+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
333+
$loader->load('class_from_id.yml');
334+
335+
$this->assertEquals(CaseSensitiveClass::class, $container->getDefinition(CaseSensitiveClass::class)->getClass());
336+
}
337+
328338
/**
329339
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
330340
* @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").

0 commit comments

Comments
 (0)