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

Skip to content

Commit 3f9eee2

Browse files
committed
Implement clearable containers
This allows to release remove references to all services during shutdown, giving much more chances to destruct services and the container through refcounting rather than waiting GC, as it will break cycles between the container and container-aware services.
1 parent 86e77eb commit 3f9eee2

File tree

5 files changed

+89
-1
lines changed

5 files changed

+89
-1
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* allowed specifying a directory to recursively load all configuration files it contains
88
* deprecated the concept of scopes
99
* added `Definition::setShared()` and `Definition::isShared()`
10+
* added ClearableContainerInterface to be able to clear the container to release memory on shutdown
1011

1112
2.7.0
1213
-----
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection;
13+
14+
/**
15+
* ClearableContainerInterface defines additional clearing functionality
16+
* for containers, allowing to release shared services when the container is
17+
* not needed anymore.
18+
*
19+
* @author Christophe Coevoet <[email protected]>
20+
*/
21+
interface ClearableContainerInterface extends ContainerInterface
22+
{
23+
/**
24+
* Clears shared services from the container.
25+
*/
26+
public function clear();
27+
}

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
*
6161
* @api
6262
*/
63-
class Container implements IntrospectableContainerInterface
63+
class Container implements IntrospectableContainerInterface, ClearableContainerInterface
6464
{
6565
/**
6666
* @var ParameterBagInterface
@@ -375,6 +375,18 @@ public function initialized($id)
375375
return isset($this->services[$id]) || array_key_exists($id, $this->services);
376376
}
377377

378+
/**
379+
* Clears shared services from the container.
380+
*/
381+
public function clear()
382+
{
383+
if (!empty($this->scopedServices)) {
384+
throw new RuntimeException('Clearing the container is not allowed when a scope is active.');
385+
}
386+
387+
$this->services = array();
388+
}
389+
378390
/**
379391
* Gets all service ids.
380392
*

src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,49 @@ public function testInitialized()
320320
$this->assertTrue($sc->initialized('alias'), '->initialized() returns true for alias if aliased service is initialized');
321321
}
322322

323+
public function testClear()
324+
{
325+
$c = new Container();
326+
$c->set('bar', new \stdClass());
327+
328+
$c->clear();
329+
330+
$this->assertNull($c->get('bar', ContainerInterface::NULL_ON_INVALID_REFERENCE));
331+
}
332+
333+
/**
334+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
335+
* @expectedExceptionMessage Clearing the container is not allowed when a scope is active.
336+
* @group legacy
337+
*/
338+
public function testCannotClearInActiveScope()
339+
{
340+
$c = new Container();
341+
$c->addScope(new Scope('foo'));
342+
$c->set('bar', new \stdClass());
343+
344+
$c->enterScope('foo');
345+
346+
$c->clear();
347+
}
348+
349+
/**
350+
* @group legacy
351+
*/
352+
public function testClearAfterLeavingScope()
353+
{
354+
$c = new Container();
355+
$c->addScope(new Scope('foo'));
356+
$c->set('bar', new \stdClass());
357+
358+
$c->enterScope('foo');
359+
$c->leaveScope('foo');
360+
361+
$c->clear();
362+
363+
$this->assertNull($c->get('bar', ContainerInterface::NULL_ON_INVALID_REFERENCE));
364+
}
365+
323366
/**
324367
* @group legacy
325368
*/

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator;
1515
use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper;
16+
use Symfony\Component\DependencyInjection\ClearableContainerInterface;
1617
use Symfony\Component\DependencyInjection\ContainerInterface;
1718
use Symfony\Component\DependencyInjection\ContainerBuilder;
1819
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
@@ -180,6 +181,10 @@ public function shutdown()
180181
$bundle->setContainer(null);
181182
}
182183

184+
if ($this->container instanceof ClearableContainerInterface) {
185+
$this->container->clear();
186+
}
187+
183188
$this->container = null;
184189
}
185190

0 commit comments

Comments
 (0)