Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e1b86ab

Browse filesBrowse files
committed
[DependencyInjection] Optional class for class named services
1 parent f2768dc commit e1b86ab
Copy full SHA for e1b86ab

File tree

7 files changed

+63
-0
lines changed
Filter options

7 files changed

+63
-0
lines changed

‎src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/ContainerBuilder.php
+4Lines changed: 4 additions & 0 deletions
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php
+14Lines changed: 14 additions & 0 deletions
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
+16Lines 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 <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+
}
+6Lines 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>
+3Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php
+10Lines 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
@@ -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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php
+10Lines 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
@@ -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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.