Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[RFC] EventSubscriber attribute #47015

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

Closed
94noni opened this issue Jul 21, 2022 · 2 comments
Closed

[RFC] EventSubscriber attribute #47015

94noni opened this issue Jul 21, 2022 · 2 comments
Labels
RFC RFC = Request For Comments (proposals about features that you want to be discussed)

Comments

@94noni
Copy link
Contributor

94noni commented Jul 21, 2022

Description

RFC - Feature Request - EventSubscriber attribute

Opening this issue to gather feedbacks

Adding a new attribute AsEventSubscriber (mimic of AsCommand) for configuring EventSubscriber classes/methodes

Example

Before:

final class ProductSyncSubscriber implements EventSubscriberInterface
{
    public function __construct(private readonly Manager $manager) {}

    public static function getSubscribedEvents(): array
    {
        return [
            ProductUpdatedEvent::class => 'onProductUpdated',
        ];
    }

    public function onProductUpdated(ProductUpdatedEvent $productUpdatedEvent): void
    {
        $this->manager->sync($productUpdatedEvent->getProductId());
    }
}

After:

final class ProductSyncSubscriber
{
    public function __construct(private readonly Manager $manager) {}
    
    #[AsEventSubscriber(event: ProductUpdatedEvent, priority: 11)]
    public function onProductUpdated(ProductUpdatedEvent $productUpdatedEvent): void
    {
        $this->manager->sync($productUpdatedEvent->getProductId());
    }
}

  • Then either implement new empty interface or extends empty class to autoconfigure
  • Or register like controller as service
@carsonbot carsonbot added the RFC RFC = Request For Comments (proposals about features that you want to be discussed) label Jul 21, 2022
@stof
Copy link
Member

stof commented Jul 21, 2022

Well, your use case is defining an event listener, not an event subscriber (by the definitions of the component), and we already have AsEventListener doing exactly what you areasking

@stof stof closed this as completed Jul 21, 2022
@94noni
Copy link
Contributor Author

94noni commented Jul 21, 2022

@stof great, I did not find any doc that is why I've created this,

https://github.com/symfony/symfony-docs/search?q=AsEventListener
https://github.com/symfony/symfony-docs/search?q=AsCommand

may I ask you a link to PR to check that? thank you :)

edit: found 4f13189 will see if I can provide doc for that

javiereguiluz added a commit to symfony/symfony-docs that referenced this issue Aug 9, 2022
…e (94noni)

This PR was submitted for the 6.0 branch but it was merged into the 5.4 branch instead.

Discussion
----------

[EventDispatcher] Document the #AsEventListener attribute

Document the AsEventListener attribute

Ref symfony/symfony#47015 (did not found any doc thus opened an issue at first)
For the code example, I've taken inspiration on the related PR:

- symfony/symfony@2ab3caf#diff-57b112e725705bbbf01380b0d11c9a8fe16ea69d85d1d3e618eceeb8b58094ce
- symfony/symfony@2ab3caf#diff-73a6a14d3288b358a21c0f47ecc1ec78c05e933b66e9a647e5013b2cab002b89

Commits
-------

ff5032d Update event_dispatcher.rst
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFC RFC = Request For Comments (proposals about features that you want to be discussed)
Projects
None yet
Development

No branches or pull requests

3 participants