Skip to content

[2.8] Make service not shared when prototype scope is set #15091

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Symfony/Component/DependencyInjection/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/DependencyInjection/Tests/DefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
<configurator class="%baz_class%" method="configureStatic1"/>
</service>
<service id="factory_service" class="Bar" factory-method="getInstance" factory-service="foo.baz"/>
<service id="foo_bar" class="%foo_class%" scope="prototype"/>
<service id="foo_bar" class="%foo_class%" shared="false" scope="prototype"/>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the additional prototype attribute?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is to test the output of the XmlDumper. As this service is a legacy service, it only sets the scope to prototype (which remains the same). The only thing this PR changed was also setting shared to false in these cases

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is needed, see here and here. All services of prototype scope are non-shared by default.

</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ services:
factory_service: foo.baz
foo_bar:
class: %foo_class%
shared: false
scope: prototype