-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
[FrameworkBundle] make RegisterKernelListenersPass reusable #6643
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 |
---|---|---|
|
@@ -14,17 +14,47 @@ | |
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
|
||
/** | ||
* Compiler pass to register tagged services for an event dispatcher. | ||
*/ | ||
class RegisterKernelListenersPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $dispatcherService; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $listenerTag; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $subscriberTag; | ||
|
||
/** | ||
* @param string $dispatcherService Service name of the event dispatcher in processed container | ||
* @param string $listenerTag Tag name used for listener | ||
* @param string $listenerTag Tag name used for subscribers | ||
*/ | ||
public function __construct($dispatcherService = 'event_dispatcher', $listenerTag = 'kernel.event_listener', $subscriberTag = 'kernel.event_subscriber') | ||
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'd put $dispatcherService as third argument as it'd less likely be changed than the others. 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. If you don't change the dispatcher service, registering a second compiler pass is totally useless. So it is likely to change 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. Why useless? Then you'd have to tag services listening to your own events with 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. why wouldn't it make sense ? You are using the event dispatcher used by the kernel, so why duplicating the way to register listeners in 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. Because the tag is prefixed with If you tell me that it's not the case, it is meant to be generic and I can safely rely on it, I'll do so, but then I'm wondering why it has been prefixed like this? The "kernel" dispatcher service is called 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 don't see any reason to add special behaviors on the registration of listeners (except improving the error reporting when someone tries to register a private service, which is currently silently skipped) |
||
{ | ||
$this->dispatcherService = $dispatcherService; | ||
$this->listenerTag = $listenerTag; | ||
$this->subscriberTag = $subscriberTag; | ||
} | ||
|
||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->hasDefinition('event_dispatcher')) { | ||
if (!$container->hasDefinition($this->dispatcherService)) { | ||
return; | ||
} | ||
|
||
$definition = $container->getDefinition('event_dispatcher'); | ||
$definition = $container->getDefinition($this->dispatcherService); | ||
|
||
foreach ($container->findTaggedServiceIds('kernel.event_listener') as $id => $events) { | ||
foreach ($container->findTaggedServiceIds($this->listenerTag) as $id => $events) { | ||
foreach ($events as $event) { | ||
$priority = isset($event['priority']) ? $event['priority'] : 0; | ||
|
||
|
@@ -43,7 +73,7 @@ public function process(ContainerBuilder $container) | |
} | ||
} | ||
|
||
foreach ($container->findTaggedServiceIds('kernel.event_subscriber') as $id => $attributes) { | ||
foreach ($container->findTaggedServiceIds($this->subscriberTag) as $id => $attributes) { | ||
// We must assume that the class value has been correctly filled, even if the service is created by a factory | ||
$class = $container->getDefinition($id)->getClass(); | ||
|
||
|
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.
then the class name isn't appropriate anymore
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.
See the last message in PR. It can be moved to two locations (HttpKernel or EventDispatcher).