Skip to content

feat(subscriber): auto-register EmbeddedEventResolverInterface event classes as listeners#66

Open
nowshad7 wants to merge 2 commits intoxiidea:masterfrom
nowshad7:feat/auto-register-embedded-event-resolver
Open

feat(subscriber): auto-register EmbeddedEventResolverInterface event classes as listeners#66
nowshad7 wants to merge 2 commits intoxiidea:masterfrom
nowshad7:feat/auto-register-embedded-event-resolver

Conversation

@nowshad7
Copy link
Copy Markdown

@nowshad7 nowshad7 commented Mar 21, 2026

What & Why

EmbeddedEventResolverInterface already lets an event describe its own audit payload, but using it still required extra subscriber wiring just to get the event to the bundle listener. That meant the embedded-resolver flow carried boilerplate even though the runtime resolver path already knew how to handle these events.

This PR removes that manual registration step and lets embedded audit events be routed through Symfony's event system directly.

Before

class MyEvent extends Event implements EmbeddedEventResolverInterface
{
    // ...
}

class MyEventSubscriber extends AuditLogEventSubscriber
{
    public static function getSubscribedEvents(): array
    {
        return [MyEvent::class => []];
    }
}

After

class MyEvent extends Event implements EmbeddedEventResolverInterface
{
    // ...
}

$dispatcher->dispatch(new MyEvent($data), EmbeddedEventResolverInterface::class);

How

This is implemented with a small Symfony-aligned change set:

  1. Resources/config/services.yml
    Registers xiidea.easy_audit.event_listener as a kernel.event_listener for EmbeddedEventResolverInterface::class.

  2. Listener/LogEventsListener.php
    When an embedded event is dispatched via the interface shortcut, the listener converts the event name from EmbeddedEventResolverInterface::class to the concrete event class so audit entries keep a meaningful type/typeId.

  3. DependencyInjection/Compiler/SubscriberPass.php
    Preserves existing kernel.event_listener tags when adding listener registrations, so the static interface-based listener registration is not overwritten.

Why This Approach

  • Removes subscriber boilerplate for self-resolving audit events.
  • Keeps existing subscriber-based and configured-event flows unchanged.

Compatibility

There is no breaking change for existing users.

  • Existing events: configuration still works.
  • Existing subscriber-based registration still works.
  • Embedded events now have an additional zero-boilerplate path when dispatched with EmbeddedEventResolverInterface::class.

Tests & Docs

  • Updated Resources/doc/embed-resolver.md to document the new interface-based dispatch shortcut and keep the subscriber-based alternative documented.
  • Added functional coverage for dispatching an embedded event with EmbeddedEventResolverInterface::class and asserting the concrete class is used in the audit log.
  • Verified with:
./vendor/bin/phpunit Tests/Functional/CommonTest.php

Passed: 7 tests, 39 assertions.

Copy link
Copy Markdown
Member

@ronisaha ronisaha left a comment

Choose a reason for hiding this comment

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

If we change those the test case changes would no longer needed.

$container->setParameter('xiidea.easy_audit.' . $key, $value);
}

$container->registerForAutoconfiguration(EmbeddedEventResolverInterface::class)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Instead of defining in code, it is easy to define in configuration like:

    Xiidea\EasyAuditBundle\Resolver\EmbeddedEventResolverInterface:
        tags: ['easy_audit.embedded_event']


$this->appendDoctrineEventsToList($container, $eventsList);
$this->appendSubscribedEventsToList($container, $eventsList);
$this->appendEmbeddedEventClasses($container, $eventsList);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Instead of doing it manually, as this event is typed, try to solve it using typed event

@nowshad7 nowshad7 requested a review from ronisaha March 23, 2026 17:37
@nowshad7 nowshad7 force-pushed the feat/auto-register-embedded-event-resolver branch from ed48f63 to 4d2b9fa Compare March 23, 2026 18:00
@nowshad7 nowshad7 force-pushed the feat/auto-register-embedded-event-resolver branch from 4d2b9fa to d5d8d13 Compare March 23, 2026 18:38
@nowshad7
Copy link
Copy Markdown
Author

Hello @ronisaha dada,
Thanks for the feedback. All changes implemented and ready for review.

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.

3 participants