Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bd66434

Browse files
committed
minor #15091 [2.8] Make service not shared when prototype scope is set (WouterJ)
This PR was merged into the 2.8 branch. Discussion ---------- [2.8] Make service not shared when prototype scope is set | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Deprecating the scopes was introducing a BC break in Symfony 2.8 when using the prototype scope. As `$shared` wasn't updated if the scope was set to `prototype`, people will always get an error because of [the `CheckDefinitionValidityPass`](https://github.com/symfony/DependencyInjection/blob/04d5d925a987f35854f51b1c468c0ca81e1d61b9/Compiler/CheckDefinitionValidityPass.php#L54-L57). Commits ------- b7030cc Make service not shared when prototype scope is set
2 parents 50e752b + b7030cc commit bd66434

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

src/Symfony/Component/DependencyInjection/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;

src/Symfony/Component/DependencyInjection/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

src/Symfony/Component/DependencyInjection/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>

src/Symfony/Component/DependencyInjection/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)