File tree 5 files changed +89
-1
lines changed
5 files changed +89
-1
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ CHANGELOG
7
7
* allowed specifying a directory to recursively load all configuration files it contains
8
8
* deprecated the concept of scopes
9
9
* added ` Definition::setShared() ` and ` Definition::isShared() `
10
+ * added ResettableContainerInterface to be able to reset the container to release memory on shutdown
10
11
11
12
2.7.0
12
13
-----
Original file line number Diff line number Diff line change 60
60
*
61
61
* @api
62
62
*/
63
- class Container implements IntrospectableContainerInterface
63
+ class Container implements IntrospectableContainerInterface, ResettableContainerInterface
64
64
{
65
65
/**
66
66
* @var ParameterBagInterface
@@ -375,6 +375,18 @@ public function initialized($id)
375
375
return isset ($ this ->services [$ id ]) || array_key_exists ($ id , $ this ->services );
376
376
}
377
377
378
+ /**
379
+ * Clears shared services from the container.
380
+ */
381
+ public function reset ()
382
+ {
383
+ if (!empty ($ this ->scopedServices )) {
384
+ throw new RuntimeException ('Resetting the container is not allowed when a scope is active. ' );
385
+ }
386
+
387
+ $ this ->services = array ();
388
+ }
389
+
378
390
/**
379
391
* Gets all service ids.
380
392
*
Original file line number Diff line number Diff line change
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
+ * 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 <[email protected] >
20
+ */
21
+ interface ResettableContainerInterface extends ContainerInterface
22
+ {
23
+ /**
24
+ * Resets shared services from the container.
25
+ */
26
+ public function reset ();
27
+ }
Original file line number Diff line number Diff line change @@ -320,6 +320,49 @@ public function testInitialized()
320
320
$ this ->assertTrue ($ sc ->initialized ('alias ' ), '->initialized() returns true for alias if aliased service is initialized ' );
321
321
}
322
322
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\RuntimeException
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
+
323
366
/**
324
367
* @group legacy
325
368
*/
Original file line number Diff line number Diff line change 13
13
14
14
use Symfony \Bridge \ProxyManager \LazyProxy \Instantiator \RuntimeInstantiator ;
15
15
use Symfony \Bridge \ProxyManager \LazyProxy \PhpDumper \ProxyDumper ;
16
+ use Symfony \Component \DependencyInjection \ResettableContainerInterface ;
16
17
use Symfony \Component \DependencyInjection \ContainerInterface ;
17
18
use Symfony \Component \DependencyInjection \ContainerBuilder ;
18
19
use Symfony \Component \DependencyInjection \Dumper \PhpDumper ;
@@ -180,6 +181,10 @@ public function shutdown()
180
181
$ bundle ->setContainer (null );
181
182
}
182
183
184
+ if ($ this ->container instanceof ResettableContainerInterface) {
185
+ $ this ->container ->reset ();
186
+ }
187
+
183
188
$ this ->container = null ;
184
189
}
185
190
You can’t perform that action at this time.
0 commit comments