Skip to content

Commit b928133

Browse files
Merge branch '2.7' into 2.8
* 2.7: [DI] Fixed custom services definition BC break introduced in ec7e70fb… [DI] Aliases should preserve the aliased invalid behavior
2 parents 5912413 + 3eb8a34 commit b928133

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV
449449
}
450450

451451
if (!array_key_exists($id, $this->definitions) && isset($this->aliasDefinitions[$id])) {
452-
return $this->get($this->aliasDefinitions[$id]);
452+
return $this->get($this->aliasDefinitions[$id], $invalidBehavior);
453453
}
454454

455455
try {
@@ -859,8 +859,8 @@ public function findDefinition($id)
859859
*/
860860
public function createService(Definition $definition, $id, $tryProxy = true)
861861
{
862-
if ('Symfony\Component\DependencyInjection\Definition' !== get_class($definition)) {
863-
throw new RuntimeException(sprintf('Constructing service "%s" from a %s is not supported at build time.', $id, get_class($definition)));
862+
if ($definition instanceof DefinitionDecorator) {
863+
throw new RuntimeException(sprintf('Constructing service "%s" from a parent definition is not supported at build time.', $id));
864864
}
865865

866866
if ($definition->isSynthetic()) {

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

+22-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
3030
use Symfony\Component\DependencyInjection\Scope;
3131
use Symfony\Component\Config\Resource\FileResource;
32+
use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
3233
use Symfony\Component\ExpressionLanguage\Expression;
3334

3435
class ContainerBuilderTest extends \PHPUnit_Framework_TestCase
@@ -257,6 +258,18 @@ public function testSetReplacesAlias()
257258
$this->assertSame($foo, $builder->get('alias'), '->set() replaces an existing alias');
258259
}
259260

261+
public function testAliasesKeepInvalidBehavior()
262+
{
263+
$builder = new ContainerBuilder();
264+
265+
$aliased = new Definition('stdClass');
266+
$aliased->addMethodCall('setBar', array(new Reference('bar', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)));
267+
$builder->setDefinition('aliased', $aliased);
268+
$builder->setAlias('alias', 'aliased');
269+
270+
$this->assertEquals(new \stdClass(), $builder->get('alias'));
271+
}
272+
260273
public function testAddGetCompilerPass()
261274
{
262275
$builder = new ContainerBuilder();
@@ -433,7 +446,7 @@ public function testResolveServices()
433446

434447
/**
435448
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
436-
* @expectedExceptionMessage Constructing service "foo" from a Symfony\Component\DependencyInjection\DefinitionDecorator is not supported at build time.
449+
* @expectedExceptionMessage Constructing service "foo" from a parent definition is not supported at build time.
437450
*/
438451
public function testResolveServicesWithDecoratedDefinition()
439452
{
@@ -445,6 +458,14 @@ public function testResolveServicesWithDecoratedDefinition()
445458
$builder->get('foo');
446459
}
447460

461+
public function testResolveServicesWithCustomDefinitionClass()
462+
{
463+
$builder = new ContainerBuilder();
464+
$builder->setDefinition('foo', new CustomDefinition('stdClass'));
465+
466+
$this->assertInstanceOf('stdClass', $builder->get('foo'));
467+
}
468+
448469
public function testMerge()
449470
{
450471
$container = new ContainerBuilder(new ParameterBag(array('bar' => 'foo')));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
use Symfony\Component\DependencyInjection\Definition;
15+
16+
class CustomDefinition extends Definition
17+
{
18+
}

0 commit comments

Comments
 (0)