From b7030cc19e8cf05257c8bd1f309783eedc647131 Mon Sep 17 00:00:00 2001 From: Wouter J Date: Wed, 24 Jun 2015 21:50:08 +0200 Subject: [PATCH] Make service not shared when prototype scope is set --- .../Component/DependencyInjection/Definition.php | 4 ++++ .../DependencyInjection/Tests/DefinitionTest.php | 13 +++++++++++++ .../Tests/Fixtures/xml/legacy-services9.xml | 2 +- .../Tests/Fixtures/yaml/legacy-services9.yml | 1 + 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/DependencyInjection/Definition.php b/src/Symfony/Component/DependencyInjection/Definition.php index e68e8b3cb813..4e99cca77ac0 100644 --- a/src/Symfony/Component/DependencyInjection/Definition.php +++ b/src/Symfony/Component/DependencyInjection/Definition.php @@ -649,6 +649,10 @@ public function setScope($scope, $triggerDeprecationError = true) @trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0.', E_USER_DEPRECATED); } + if (ContainerInterface::SCOPE_PROTOTYPE === $scope) { + $this->setShared(false); + } + $this->scope = $scope; return $this; diff --git a/src/Symfony/Component/DependencyInjection/Tests/DefinitionTest.php b/src/Symfony/Component/DependencyInjection/Tests/DefinitionTest.php index 4bac0e2a0baf..d67f1d7224a2 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/DefinitionTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/DefinitionTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\DependencyInjection\Tests; use Symfony\Component\DependencyInjection\Definition; +use Symfony\Component\DependencyInjection\ContainerInterface; class DefinitionTest extends \PHPUnit_Framework_TestCase { @@ -139,6 +140,18 @@ public function testSetIsShared() $this->assertFalse($def->isShared(), '->isShared() returns false if the instance must not be shared'); } + /** + * @group legacy + */ + public function testPrototypeScopedDefinitionAreNotShared() + { + $def = new Definition('stdClass'); + $def->setScope(ContainerInterface::SCOPE_PROTOTYPE); + + $this->assertFalse($def->isShared()); + $this->assertEquals(ContainerInterface::SCOPE_PROTOTYPE, $def->getScope()); + } + /** * @covers Symfony\Component\DependencyInjection\Definition::setScope * @covers Symfony\Component\DependencyInjection\Definition::getScope diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/legacy-services9.xml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/legacy-services9.xml index dc55972a287e..dcb312a1e9f4 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/legacy-services9.xml +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/legacy-services9.xml @@ -33,6 +33,6 @@ - + diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/legacy-services9.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/legacy-services9.yml index 6a5f03d9b87e..c7e80ea5b8c0 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/legacy-services9.yml +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/legacy-services9.yml @@ -29,4 +29,5 @@ services: factory_service: foo.baz foo_bar: class: %foo_class% + shared: false scope: prototype