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

Autoconfiguration for event subscribers #674

Closed
backbone87 opened this issue Jun 27, 2017 · 18 comments · Fixed by #1119
Closed

Autoconfiguration for event subscribers #674

backbone87 opened this issue Jun 27, 2017 · 18 comments · Fixed by #1119
Assignees
Milestone

Comments

@backbone87
Copy link

Can we have the EventSubscriber autoconfigured?
And it would be also nice if entity listeners can be autoconfigured, maybe by creating a marker interface?

@Ocramius
Copy link
Member

Ocramius commented Jun 28, 2017 via email

@backbone87
Copy link
Author

Erm, i dont think we are on the same page, i talk about marker interface for http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#entity-listeners that are different from event listeners.

@stof
Copy link
Member

stof commented Jun 28, 2017

we cannot autoconfigure entity listeners, as the tag requires additional info (the name of the entity on which it applies for instance)

@backbone87
Copy link
Author

But that link is defined in the entity metadata? With the EntityListener annotation?

@codedmonkey
Copy link

@stof Would it be ok to add auto-tagging for event subscribers since those don't require any additional arguments? It wouldn't be ideal to only have some of the tags autoconfigured, but it still makes sense for the simple use cases, especially since all we need to add is:

        $container->registerForAutoconfiguration(EventSubscriber::class)
            ->addTag('doctrine.event_subscriber');

Would love to know if such a pull request would be accepted.

@alcaeus
Copy link
Member

alcaeus commented Dec 5, 2017

Would it be ok to add auto-tagging for event subscribers since those don't require any additional arguments?

Not @stof, but no. The EventSubscriber interface is from Doctrine\Common and is valid for ORM, ODM and possibly a bunch of other object mappers. Adding the autoconfiguration you posted would also register event subscribers for other mappers in ORM which is not desirable.

The only option you can do this is by introducing a ORM-specific interface.

@codedmonkey
Copy link

Thanks for the response. In that case I would be in favor of creating some interfaces inside this bundle that extend the original interfaces to "mark a class optimized for Symfony features". Auto-tagging not working for such an integral part of the Symfony framework feels like a miss to me.

@alcaeus
Copy link
Member

alcaeus commented Dec 7, 2017

TBH, I dislike the notion of marker interfaces. You can achieve the very same thing by putting a small prototype service:

        <prototype namespace="AppBundle\Doctrine\ORM\EventSubscriber\" resource="../../Doctrine/ORM/EventSubscriber/*">
            <tag name="doctrine.event_subscriber"/>
        </prototype>

However, since this is the bundle and Symfony folks have taken over, it's up to them to decide this. Opinions @weaverryan @fabpot?

@kimhemsoe kimhemsoe self-assigned this Mar 29, 2018
@kimhemsoe
Copy link
Member

May reconsider in the future if a good solution comes or someone thinks otherwise.

@TomasVotruba
Copy link

I was like WTF is autoconfigure broken? It's designed to not bother users with cases like:

services:
    # why this is not autoconfigured? :(
    Pehapkari\Doctrine\EventSubscriber\SetUploadDestinationOnPostLoadEventSubscriber:
        tags:
            - { name: doctrine.event_subscriber }

It's not, it's just missing for un-related reason. This subscriber needs only tag, nothing else.

@TomasVotruba
Copy link

In case you have the same problem, put this in in your Kernel:

protected function configureContainer(ContainerBuilder $containerBuilder, LoaderInterface $loader): void
    {
        $containerBuilder->registerForAutoconfiguration(\Doctrine\Common\EventSubscriber::class)
            ->addTag('doctrine.event_subscriber');

@alcaeus
Copy link
Member

alcaeus commented May 25, 2019

It’s no WTF, I reiterate what I have said before: not every instance of an event subscriber is meant to be an ORM subscriber. This would cause a lot of issues for people running ORM and another mapper (e.g. MongoDB ODM) side-by-side.

@weaverryan
Copy link
Contributor

It is a bit of an inconsistency, since almost everything else uses autoconfigure. I think we should consider (welcome someone to create the PR) a marker interface for the ORM specifically. We didn’t rush into tho for these reasons, but my opinion is that we should go for it.

And I think a marker interface for event subscribers or entity listeners and autoconfiguration are both valid ideas. If we’re going to do one, we might as well do both, but probably in separate PRs.

@alcaeus
Copy link
Member

alcaeus commented May 26, 2019

They can go into the same PR, I wouldn’t have a problem with that.

@latenzio
Copy link

I prefer to add an example to the documentation how to implement auto configuration for this case:

security.yml

services:
    _instanceof:
        App\Doctrine\EventSubscriber:
            tags: [ doctrine.event_subscriber ]

App\Doctrine\EventSubscriber.php:

<?php

namespace App\Doctrine;

use Doctrine\Common\EventSubscriber as BaseEventSubscriber;

/**
 * Interface EventSubscriber to use for auto configuration
 *
 * @author Enrico Thies <[email protected]>
 */
interface EventSubscriber extends BaseEventSubscriber
{

}

Instead of implementing Doctrine\Common\EventSubscriber use App\Doctrine\EventSubscriber and everything works fine.

@lyrixx
Copy link
Contributor

lyrixx commented Dec 20, 2019

A collegue just hit "this issue".
If I do the PR, would you consider merging it?

@alcaeus
Copy link
Member

alcaeus commented Dec 20, 2019

Yes, absolutely! As Ryan suggested, we should add marker interfaces for both entity listeners and event subscribers 👍

@alcaeus
Copy link
Member

alcaeus commented Jan 9, 2020

Thanks to @lyrixx, this will be released with 2.1.0! 🎉

@alcaeus alcaeus added this to the 2.1.0 milestone Jan 9, 2020
@alcaeus alcaeus changed the title Autoconfiguration Autoconfiguration for event subscribers May 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants