Skip to content

Commit 4da1865

Browse files
committed
Make service not shared when prototype scope is set
1 parent 04d5d92 commit 4da1865

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

Definition.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,10 @@ public function setScope($scope, $triggerDeprecationError = true)
649649
@trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
650650
}
651651

652+
if (ContainerInterface::SCOPE_PROTOTYPE === $scope) {
653+
$this->setShared(false);
654+
}
655+
652656
$this->scope = $scope;
653657

654658
return $this;

Tests/DefinitionTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\DependencyInjection\Tests;
1313

1414
use Symfony\Component\DependencyInjection\Definition;
15+
use Symfony\Component\DependencyInjection\ContainerInterface;
1516

1617
class DefinitionTest extends \PHPUnit_Framework_TestCase
1718
{
@@ -139,6 +140,18 @@ public function testSetIsShared()
139140
$this->assertFalse($def->isShared(), '->isShared() returns false if the instance must not be shared');
140141
}
141142

143+
/**
144+
* @group legacy
145+
*/
146+
public function testPrototypeScopedDefinitionAreNotShared()
147+
{
148+
$def = new Definition('stdClass');
149+
$def->setScope(ContainerInterface::SCOPE_PROTOTYPE);
150+
151+
$this->assertFalse($def->isShared());
152+
$this->assertEquals(ContainerInterface::SCOPE_PROTOTYPE, $def->getScope());
153+
}
154+
142155
/**
143156
* @covers Symfony\Component\DependencyInjection\Definition::setScope
144157
* @covers Symfony\Component\DependencyInjection\Definition::getScope

Tests/Fixtures/xml/legacy-services9.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@
3333
<configurator class="%baz_class%" method="configureStatic1"/>
3434
</service>
3535
<service id="factory_service" class="Bar" factory-method="getInstance" factory-service="foo.baz"/>
36-
<service id="foo_bar" class="%foo_class%" scope="prototype"/>
36+
<service id="foo_bar" class="%foo_class%" shared="false" scope="prototype"/>
3737
</services>
3838
</container>

Tests/Fixtures/yaml/legacy-services9.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ services:
2929
factory_service: foo.baz
3030
foo_bar:
3131
class: %foo_class%
32+
shared: false
3233
scope: prototype

0 commit comments

Comments
 (0)