Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

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

Copy link
Author

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).

{
/**
* @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')
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The 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 kernel.event_listener, this doesn't make so much sense to me.

Copy link
Member

Choose a reason for hiding this comment

The 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 ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the tag is prefixed with kernel, I have no guarantee tomorrow this compiler pass won't add any special behavior to my listeners or whatever because it assumes they are kernel events listeners.

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 event_dispatcher, not kernel.event_dispatcher.

Copy link
Member

Choose a reason for hiding this comment

The 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;

Expand All @@ -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();

Expand Down