-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[EventDispatcher] Delay instantiation of a service until the respective listener is triggered in ContainerAwareEventDispatcher #12019
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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ea03fa5
feature #12007 make ContainerAwareEventDispatcher even lazier
znerol 7bc9d54
Add back removeListener() and add a test for the lazy-instantiation
znerol 2232862
Fix code style issues, use ContainerBuilder in lazy-loading test, do …
znerol cb3f1cf
Fix cs again
znerol 4df2bf9
Extract condition-check from loop, fix parenthesis
znerol 1285f36
Replace closure with LazyServiceListener
znerol da10aa9
Fix coding style
znerol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/Symfony/Component/EventDispatcher/LazyServiceListener.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?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\EventDispatcher; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
/** | ||
* A listener forwarding its invocation to a service. | ||
*/ | ||
class LazyServiceListener | ||
{ | ||
/** | ||
* The container from where service is loaded | ||
* @var ContainerInterface | ||
*/ | ||
private $container; | ||
|
||
/** | ||
* The service id. | ||
* @var string | ||
*/ | ||
private $serviceId; | ||
|
||
/** | ||
* The name of a method on the service. | ||
* @var string | ||
*/ | ||
private $method; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param ContainerInterface $container The service container | ||
* @param string $serviceId The service identifier | ||
* @param string $method The method name | ||
*/ | ||
public function __construct(ContainerInterface $container, $serviceId, $method) | ||
{ | ||
$this->container = $container; | ||
$this->serviceId = $serviceId; | ||
$this->method = $method; | ||
} | ||
|
||
/** | ||
* Retrieves the service from the container and forwards the method call. | ||
*/ | ||
public function __invoke(Event $event, $eventName, EventDispatcherInterface $dispatcher) | ||
{ | ||
$service = $this->container->get($this->serviceId); | ||
$service->{$this->method}($event, $eventName, $dispatcher); | ||
} | ||
|
||
/** | ||
* Returns the container. | ||
*/ | ||
public function getContainer() | ||
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. IMO, you should not have these getters once you add the method returning the callable |
||
{ | ||
return $this->container; | ||
} | ||
|
||
/** | ||
* Returns the service id. | ||
*/ | ||
public function getServiceId() | ||
{ | ||
return $this->serviceId; | ||
} | ||
|
||
/** | ||
* Returns the method name. | ||
*/ | ||
public function getMethod() | ||
{ | ||
return $this->method; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This looks like a hack. What about implementing the
IntrospectableCallable
as suggested by @stof instead?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.
The very reason for this patch is to prevent preliminary service instantiation. I'm not comfortable with adding a public
getInnerCallable
which suffers exactly from this side-effect.Then there is already a
WrappedListener
class. I initially was tempted to extract aWrappedListenerInterface
from there but that would have made the code inTraceableEventDispatcher
less obvious. Mainly because in that casegetWrappedListener
would have been used for two very different things. IntroducingIntrospectableCallable
without touching theWrappedListener
also would result in a confusing coexistence of two similar concepts in the same component.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.
@znerol your implementation also triggers the lazy-loading of the service when tracing events. But instead of providing an interface usable by other implementations of a lazy-loading dispatcher, it is tied to the Symfony container