Skip to content

[DependencyInjection] Self-referenced 'service_container' service breaks garbage collection #11422

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

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 20 additions & 2 deletions src/Symfony/Component/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ class Container implements IntrospectableContainerInterface
public function __construct(ParameterBagInterface $parameterBag = null)
{
$this->parameterBag = $parameterBag ?: new ParameterBag();

$this->set('service_container', $this);
}

/**
Expand Down Expand Up @@ -197,6 +195,12 @@ public function set($id, $service, $scope = self::SCOPE_CONTAINER)

$id = strtolower($id);

if ('service_container' === $id) {
// BC: 'service_container' is no longer a self-reference but always
// $this, so ignore this call.
// @todo Throw InvalidArgumentException in next major release.
return;
}
if (self::SCOPE_CONTAINER !== $scope) {
if (!isset($this->scopedServices[$scope])) {
throw new RuntimeException(sprintf('You cannot set service "%s" of inactive scope.', $id));
Expand Down Expand Up @@ -233,6 +237,10 @@ public function has($id)
{
$id = strtolower($id);

if ('service_container' === $id) {
return true;
}

return isset($this->services[$id])
|| array_key_exists($id, $this->services)
|| isset($this->aliases[$id])
Expand Down Expand Up @@ -269,6 +277,9 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
if ($strtolower) {
$id = strtolower($id);
}
if ('service_container' === $id) {
return $this;
}
if (isset($this->aliases[$id])) {
$id = $this->aliases[$id];
}
Expand Down Expand Up @@ -340,6 +351,12 @@ public function initialized($id)
{
$id = strtolower($id);

if ('service_container' === $id) {
// BC: 'service_container' was a synthetic service previously.
// @todo Change to false in next major release.
return true;
}

return isset($this->services[$id]) || array_key_exists($id, $this->services);
}

Expand All @@ -357,6 +374,7 @@ public function getServiceIds()
$ids[] = self::underscore($match[1]);
}
}
$ids[] = 'service_container';

return array_unique(array_merge($ids, array_keys($this->services)));
}
Expand Down