Skip to content

[SoftDeleteable] Use ListenersInvoker instead of EventManager #3005

@raymondschouten

Description

@raymondschouten

Feature Request

This is a follow up for closed issues #1285, #1286 and #2136.

Currently the SoftDeleteable events are dispatched using the Doctrine EventManager, however that is only one of the three ways how you can subscribe to events in Doctrine ORM. You can also subscribe to events via lifecycle callbacks and entity listeners. All 3 "systems" are covered by Doctrine when using the ListenersInvoker.

My use-case right now, is that I have entity listeners (not to be confused with event listeners), which make it possible to listen to events targeted to specific entities. This eliminates the need to dispatch the entire event if nobody is listening, and it eliminates the need to do an instanceof check, to see if it is about the entity I expect. However, since the SoftDeleteable events use the EventManager, my entity listeners will not be executed for these events.

It is quite simple to use the ListenersInvoker, just replace (pseudo-code):

$eventManager = $entityManager->getEventManager();
$eventManager->dispatchEvent(self::PRE_SOFT_DELETE, new LifecycleEventArgs($entity, $entityManager));

with:

$classMetadata = $entityManager->getClassMetadata(get_class($entity));
$listenersInvoker = new ListenersInvoker($entityManager);

$invoke = $listenersInvoker->getSubscribedSystems($classMetadata, self::PRE_SOFT_DELETE);

if ($invoke !== ListenersInvoker::INVOKE_NONE) {
    $listenersInvoker->invoke($classMetadata, self::PRE_SOFT_DELETE, $entity, new LifecycleEventArgs($entity, $entityManager), $invoke);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions