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

EventSubscriber: Subscribe with a parent event class #31969

Open
ndench opened this issue Jun 10, 2019 · 8 comments

Comments

@ndench
Copy link

commented Jun 10, 2019

Description
With the changes to the EventDispatcher in 4.3 we now subscribe to events using the FQCN of the event. It would be awesome if you could use the FQCN of a parent class in order to subscribe to all it's children events.

This comes up because we're using events to power our audit trail. Every controller action dispatches an event which is a subclass of AuditEvent using the same event name. We then have a single subscriber that catches each event and writes it to the database. That can then be accesses to see what actions a user has taken.

While this is still supported (we just have to reorder the arguments), I really like the idea of using FQCN's as it removes a lot of boilerplate and is generally much cleaner.

Example

class VehicleEvent {
}

class MotorcycleEvent extends VehicleEvent 
{
}

class SedanEvent extends VehicleEvent 
{
}

class BicycleEvent extends VehicleEvent 
{
}

class PlaneEvent extends VehicleEvent 
{
}

class VehicleSubscriber extends EventSubscriberInterface 
{
    public function getSubscribedEvents(): array
    {
        return [
            VehicleEvent::class => 'handleVehicleEvent',
        ];
    }

    public function handleVehicleEvent(VehicleEvent $event): void
    {
        // Do vehicle related things
    }
}
@Tobion

This comment has been minimized.

Copy link
Member

commented Jun 10, 2019

You could use the Messenger component which supports this.

@linaori

This comment has been minimized.

Copy link
Contributor

commented Jun 11, 2019

You could use the Messenger component which supports this.

The goal of messages/command is not the same as an event. It would make sense to add this functionality to the event dispatcher imo.

@Simperfit

This comment has been minimized.

Copy link
Contributor

commented Jun 12, 2019

I like the idea too ! @ndench do you want to implement this feature ?

@ndench

This comment has been minimized.

Copy link
Author

commented Jun 12, 2019

@Simperfit yeah I could take a run at it 👍

@ndench

This comment has been minimized.

Copy link
Author

commented Jun 18, 2019

I've submitted a PR for this, please review it and let me know if there are any change you'd like me to make 😄

@xabbuh

This comment has been minimized.

Copy link
Member

commented Jun 18, 2019

Do we need to consider to add support for this as a bugfix? PSR-14 declares that listener providers (event subscribers in the EventDispatcher component) must treat parent types identically to child classes:

Listener Providers MUST treat parent types identically to the Event’s own type when determining listener applicability.

see https://www.php-fig.org/psr/psr-14/#listener-provider

@nicolas-grekas

This comment has been minimized.

Copy link
Member

commented Jun 18, 2019

We don't implement ListenerProviderInterface

@nicolas-grekas

This comment has been minimized.

Copy link
Member

commented Jun 18, 2019

(and I'm not sure this sentence in PSR 14 means that also - it's more about LSP to me)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants
You can’t perform that action at this time.