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

Add listener config to EventManager #83

Open
wants to merge 1 commit into
base: 2.0.x
Choose a base branch
from

Conversation

alanpoulain
Copy link

This PR adds the possibility to have configuration when adding a listener to the event manager.

For now, it contains only a method: when a method is defined, it is used instead of the event name to call the listener.

It will allow to define a custom method when adding a listener for instance in Symfony:

    App\EventListener\FooListener:
        tags:
            - { name: 'doctrine.event_listener', event: preSoftDelete, connection: custom_connection, method: onDelete }

@alanpoulain alanpoulain force-pushed the listener-config branch 2 times, most recently from 04322c4 to bed1895 Compare March 21, 2025 12:43
@derrabus
Copy link
Member

Thank you for your PR. However, I don't really feel we need this change, to be honest. Those listener objects are a piece of glue code solely meant to be consumed by the EventManager. Why would I want to change the method name?

@alanpoulain
Copy link
Author

alanpoulain commented Mar 21, 2025

I'm not sure to follow, those listeners can be declared outside. See for instance https://symfony.com/doc/current/doctrine/events.html#doctrine-lifecycle-listeners.

And if you want to see the related code:
https://github.com/symfony/doctrine-bridge/blob/9431a37baaa77a6e232c37c0564e900793f77c25/DependencyInjection/CompilerPass/RegisterEventListenersAndSubscribersPass.php#L102

Changing the method name can be useful to convey what the method does. Also it follows what can be done with the entity listeners.

@derrabus
Copy link
Member

derrabus commented Mar 21, 2025

Changing the method name can be useful to convey what the method does.

Sure, then have the handling method with the ugly event name call another method with a name that you can pick.

class MyListener
{
    public function someEvent($eventArgs)
    {
        $this->doSomethingVeryUseful($eventArgs);
    }

    public function doSomethingVeryUseful($eventArgs)
    {
        // your handling logic goes here.
    }
}

Also it follows what can be done with the entity listeners.

Okay, but we apparently did not need to change EventManager to implement that feature. Why is it suddenly necessary for the feature that you have in mind?

@alanpoulain
Copy link
Author

alanpoulain commented Mar 21, 2025

Sure it can be done like this but I thought it would be better to have the possibility to explicitly configure it than having a dynamic call by convention.

Okay, but we apparently did not need to change EventManager to implement that feature. Why is it suddenly necessary for the feature that you have in mind?

The entity listeners do not use the EventManager, they use this class:
https://github.com/doctrine/orm/blob/3.3.x/src/Tools/AttachEntityListenersListener.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants