Skip to content

Commit 3918fff

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

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
@@ -399,7 +399,7 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV
399399
}
400400

401401
if (!isset($this->definitions[$id]) && isset($this->aliasDefinitions[$id])) {
402-
return $this->get($this->aliasDefinitions[$id]);
402+
return $this->get($this->aliasDefinitions[$id], $invalidBehavior);
403403
}
404404

405405
try {
@@ -794,8 +794,8 @@ public function findDefinition($id)
794794
*/
795795
private function createService(Definition $definition, $id, $tryProxy = true)
796796
{
797-
if ('Symfony\Component\DependencyInjection\Definition' !== get_class($definition)) {
798-
throw new RuntimeException(sprintf('Constructing service "%s" from a %s is not supported at build time.', $id, get_class($definition)));
797+
if ($definition instanceof DefinitionDecorator) {
798+
throw new RuntimeException(sprintf('Constructing service "%s" from a parent definition is not supported at build time.', $id));
799799
}
800800

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

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

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

3233
class ContainerBuilderTest extends \PHPUnit_Framework_TestCase
@@ -236,6 +237,18 @@ public function testSetReplacesAlias()
236237
$this->assertSame($foo, $builder->get('alias'), '->set() replaces an existing alias');
237238
}
238239

240+
public function testAliasesKeepInvalidBehavior()
241+
{
242+
$builder = new ContainerBuilder();
243+
244+
$aliased = new Definition('stdClass');
245+
$aliased->addMethodCall('setBar', array(new Reference('bar', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)));
246+
$builder->setDefinition('aliased', $aliased);
247+
$builder->setAlias('alias', 'aliased');
248+
249+
$this->assertEquals(new \stdClass(), $builder->get('alias'));
250+
}
251+
239252
public function testAddGetCompilerPass()
240253
{
241254
$builder = new ContainerBuilder();
@@ -383,7 +396,7 @@ public function testResolveServices()
383396

384397
/**
385398
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
386-
* @expectedExceptionMessage Constructing service "foo" from a Symfony\Component\DependencyInjection\DefinitionDecorator is not supported at build time.
399+
* @expectedExceptionMessage Constructing service "foo" from a parent definition is not supported at build time.
387400
*/
388401
public function testResolveServicesWithDecoratedDefinition()
389402
{
@@ -395,6 +408,14 @@ public function testResolveServicesWithDecoratedDefinition()
395408
$builder->get('foo');
396409
}
397410

411+
public function testResolveServicesWithCustomDefinitionClass()
412+
{
413+
$builder = new ContainerBuilder();
414+
$builder->setDefinition('foo', new CustomDefinition('stdClass'));
415+
416+
$this->assertInstanceOf('stdClass', $builder->get('foo'));
417+
}
418+
398419
public function testMerge()
399420
{
400421
$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)