Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4995900

Browse filesBrowse files
committed
feature #15185 Implement resettable containers (stof)
This PR was merged into the 2.8 branch. Discussion ---------- Implement resettable containers | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | small one | Deprecations? | no | Tests pass? | yes | Fixed tickets | #13448 | License | MIT | Doc PR | n/a 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. There is a small BC break for a very edge case: if someone keeps a reference to the container and then shutdowns the kernel, the container would now be cleared and so would not work as intended anymore. But I don't think it is a supported use case. If you shutdown the kernel, the container of this kernel is released by the kernel and should not be used anymore IMO. Thus, shutting down the kernel generally does not happen except during tests on teardown. I'm not sure a doc PR is needed here: users of the fullstack framework should never use this feature (the kernel is using it for them). What do you think @weaverryan ? Commits ------- 4457745 Implement resettable containers
2 parents 86b218e + 4457745 commit 4995900
Copy full SHA for 4995900

File tree

5 files changed

+94
-1
lines changed
Filter options

5 files changed

+94
-1
lines changed

‎src/Symfony/Component/DependencyInjection/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/CHANGELOG.md
+1Lines 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 ResettableContainerInterface to be able to reset the container to release memory on shutdown
1011

1112
2.7.0
1213
-----

‎src/Symfony/Component/DependencyInjection/Container.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Container.php
+14-1Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
1515
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
16+
use Symfony\Component\DependencyInjection\Exception\LogicException;
1617
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1718
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
1819
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
@@ -60,7 +61,7 @@
6061
*
6162
* @api
6263
*/
63-
class Container implements IntrospectableContainerInterface
64+
class Container implements IntrospectableContainerInterface, ResettableContainerInterface
6465
{
6566
/**
6667
* @var ParameterBagInterface
@@ -375,6 +376,18 @@ public function initialized($id)
375376
return isset($this->services[$id]) || array_key_exists($id, $this->services);
376377
}
377378

379+
/**
380+
* {@inheritdoc}
381+
*/
382+
public function reset()
383+
{
384+
if (!empty($this->scopedServices)) {
385+
throw new LogicException('Resetting the container is not allowed when a scope is active.');
386+
}
387+
388+
$this->services = array();
389+
}
390+
378391
/**
379392
* Gets all service ids.
380393
*
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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+
* ResettableContainerInterface defines additional resetting functionality
16+
* for containers, allowing to release shared services when the container is
17+
* not needed anymore.
18+
*
19+
* @author Christophe Coevoet <stof@notk.org>
20+
*/
21+
interface ResettableContainerInterface extends ContainerInterface
22+
{
23+
/**
24+
* Resets shared services from the container.
25+
*
26+
* The container is not intended to be used again after being reset in a normal workflow. This method is
27+
* meant as a way to release references for ref-counting.
28+
* A subsequent call to ContainerInterface::get will recreate a new instance of the shared service.
29+
*/
30+
public function reset();
31+
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php
+43Lines 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 testReset()
324+
{
325+
$c = new Container();
326+
$c->set('bar', new \stdClass());
327+
328+
$c->reset();
329+
330+
$this->assertNull($c->get('bar', ContainerInterface::NULL_ON_INVALID_REFERENCE));
331+
}
332+
333+
/**
334+
* @expectedException \Symfony\Component\DependencyInjection\Exception\LogicException
335+
* @expectedExceptionMessage Resetting the container is not allowed when a scope is active.
336+
* @group legacy
337+
*/
338+
public function testCannotResetInActiveScope()
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->reset();
347+
}
348+
349+
/**
350+
* @group legacy
351+
*/
352+
public function testResetAfterLeavingScope()
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->reset();
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Kernel.php
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
2424
use Symfony\Component\DependencyInjection\Loader\DirectoryLoader;
2525
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
26+
use Symfony\Component\DependencyInjection\ResettableContainerInterface;
2627
use Symfony\Component\HttpFoundation\Request;
2728
use Symfony\Component\HttpFoundation\Response;
2829
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
@@ -180,6 +181,10 @@ public function shutdown()
180181
$bundle->setContainer(null);
181182
}
182183

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.