Skip to content

Commit

Permalink
Fixed self-reference in 'service_container' service breaks garbage co…
Browse files Browse the repository at this point in the history
…llection (and clone).
  • Loading branch information
sun authored and fabpot committed Aug 2, 2014
1 parent 497c875 commit 440322e
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/Symfony/Component/DependencyInjection/Container.php
Expand Up @@ -93,8 +93,6 @@ public function __construct(ParameterBagInterface $parameterBag = null)
$this->scopeChildren = array();
$this->scopedServices = array();
$this->scopeStacks = array();

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

/**
Expand Down Expand Up @@ -204,6 +202,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 @@ -240,6 +244,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 @@ -276,6 +284,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 @@ -347,6 +358,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 @@ -364,6 +381,7 @@ public function getServiceIds()
$ids[] = self::underscore($match[1]);
}
}
$ids[] = 'service_container';

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

0 comments on commit 440322e

Please sign in to comment.