Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 711fafc

Browse files
committed
[DependencyInjection] Optional class for class named services
1 parent d23fabf commit 711fafc

File tree

7 files changed

+63
-0
lines changed

7 files changed

+63
-0
lines changed

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,10 @@ public function setDefinition($id, Definition $definition)
759759
throw new BadMethodCallException('Adding definition to a frozen container is not allowed');
760760
}
761761

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

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

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
3030
use Symfony\Component\Config\Resource\FileResource;
3131
use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
32+
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
3233
use Symfony\Component\ExpressionLanguage\Expression;
3334

3435
class ContainerBuilderTest extends \PHPUnit_Framework_TestCase
@@ -808,6 +809,19 @@ public function testAutowiring()
808809

809810
$this->assertEquals('a', (string) $container->getDefinition('b')->getArgument(0));
810811
}
812+
813+
public function testClassFromId()
814+
{
815+
$container = new ContainerBuilder();
816+
817+
$unknown = $container->register('unknown_class');
818+
$class = $container->register(\stdClass::class);
819+
$autoloadClass = $container->register(CaseSensitiveClass::class);
820+
821+
$this->assertNull($unknown->getClass());
822+
$this->assertEquals(\stdClass::class, $class->getClass());
823+
$this->assertEquals(CaseSensitiveClass::class, $autoloadClass->getClass());
824+
}
811825
}
812826

813827
class FooClass
Lines changed: 16 additions & 0 deletions
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 <[email protected]>
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+
}
Lines changed: 6 additions & 0 deletions
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>
Lines changed: 3 additions & 0 deletions
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

Lines changed: 10 additions & 0 deletions
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
@@ -556,6 +557,15 @@ public function testAutowire()
556557
$this->assertTrue($container->getDefinition('bar')->isAutowired());
557558
}
558559

560+
public function testClassFromId()
561+
{
562+
$container = new ContainerBuilder();
563+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
564+
$loader->load('class_from_id.xml');
565+
566+
$this->assertEquals(CaseSensitiveClass::class, $container->getDefinition(CaseSensitiveClass::class)->getClass());
567+
}
568+
559569
/**
560570
* @group legacy
561571
* @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

Lines changed: 10 additions & 0 deletions
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
@@ -326,6 +327,15 @@ public function testAutowire()
326327
$this->assertTrue($container->getDefinition('bar_service')->isAutowired());
327328
}
328329

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