-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Implement resettable containers #15185
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
|
||
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException; | ||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; | ||
use Symfony\Component\DependencyInjection\Exception\LogicException; | ||
use Symfony\Component\DependencyInjection\Exception\RuntimeException; | ||
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; | ||
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException; | ||
|
@@ -60,7 +61,7 @@ | |
* | ||
* @api | ||
*/ | ||
class Container implements IntrospectableContainerInterface | ||
class Container implements IntrospectableContainerInterface, ResettableContainerInterface | ||
{ | ||
/** | ||
* @var ParameterBagInterface | ||
|
@@ -375,6 +376,18 @@ public function initialized($id) | |
return isset($this->services[$id]) || array_key_exists($id, $this->services); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function reset() | ||
{ | ||
if (!empty($this->scopedServices)) { | ||
throw new LogicException('Resetting the container is not allowed when a scope is active.'); | ||
} | ||
|
||
$this->services = array(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about aliases and some other properties? I know that this does not help with your goal, but this implementation looks like a hack to me. Or you should call it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Clearing it currently sets the container in the same state than just after instantiating it: if you continue to call However, the name of the method could indeed be changed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do understand that, I think the problem is more about the method name. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, I even thought about it. I will update it (and rename the interface too) |
||
} | ||
|
||
/** | ||
* Gets all service ids. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection; | ||
|
||
/** | ||
* ResettableContainerInterface defines additional resetting functionality | ||
* for containers, allowing to release shared services when the container is | ||
* not needed anymore. | ||
* | ||
* @author Christophe Coevoet <[email protected]> | ||
*/ | ||
interface ResettableContainerInterface extends ContainerInterface | ||
{ | ||
/** | ||
* Resets shared services from the container. | ||
* | ||
* The container is not intended to be used again after being reset in a normal workflow. This method is | ||
* meant as a way to release references for ref-counting. | ||
* A subsequent call to ContainerInterface::get will recreate a new instance of the shared service. | ||
*/ | ||
public function reset(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should definitely document the side effects of calling this method, not only the intended effects (memory) but also the side effects (a shared service will be re-created if get() is called).