Skip to content

[DoctrineBridge] Abstract Doctrine Subscribers with tags #11160

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

Merged
merged 1 commit into from
Aug 28, 2014

Conversation

merk
Copy link
Contributor

@merk merk commented Jun 18, 2014

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets this one
License MIT
Doc PR N/A

I've hit a problem with some doctrine listeners, built by decorating an abstract definition.

I want the abstract definition to hold the tag, however because the RegisterEventListenersAndSubscribersPass runs before abstract definitions are removed, they get added as method calls to the EventManager definition, which once the abstract service is removed, we end up with a method call that breaks the container.

I don't know if this is the best approach, it might be better not to return abstract services when calling findTaggedServiceIds instead?

@stof
Copy link
Member

stof commented Jun 18, 2014

I would rather throw an exception instead of ignoring it silently, to be consistent with the way Symfony listeners work

@merk
Copy link
Contributor Author

merk commented Jun 18, 2014

So it isn't a supported use case to tag an abstract service?

@stof
Copy link
Member

stof commented Jun 18, 2014

well, not for registering it as a Doctrine listener, as the abstract service cannot be referenced later (there are some tags for which it can make sense to apply them on abstract definitions, for instance monolog.logger, but not tags used to aggregate dependencies)

@merk
Copy link
Contributor Author

merk commented Jun 18, 2014

Are the reasons the same for the Doctrine and Symfony event dispatchers? The Doctrine one doesnt appear to lazily load its listeners (which means it can accept private services?)

I'm happy to update the PR for whatever solution so people can avoid the time it took me to work it out. This exposed a deficiency in the design of the FOSElasticaBundle listeners where we should only register one, not one per type.

Should it just throw an exception that abstract services cannot be tagged as doctrine listeners or subscribers?

@stof
Copy link
Member

stof commented Jun 18, 2014

the lazyness is the reason why symfony listeners must be public. But we are not talking about private services here but about abstract services. Abstract services don't exist at runtime anymore. they are just prototypes for child definitions.

so yes, it should be an exception (it currently also leads to an exception later because the service does not exist, so it would not change the behavior but simply give a much better error message)

@stof stof added DX labels Jun 18, 2014
@merk
Copy link
Contributor Author

merk commented Jun 18, 2014

And what message should be presented to the developer? "An abstract service cannot be tagged as a doctrine listener or subscriber."?

@stof
Copy link
Member

stof commented Jun 18, 2014

This looks good, except that the message should contain the id of the service to make it easier to fix it.

@merk
Copy link
Contributor Author

merk commented Jun 19, 2014

Updated to throw an exception


$this->process($container);

$this->assertEmpty($this->getServiceOrder($container, 'addEventSubscriber'), 'Subscriber Tagged abstract service was not processed');
Copy link
Member

Choose a reason for hiding this comment

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

this will never be reached as you expect an exception during process()

@fabpot
Copy link
Member

fabpot commented Aug 28, 2014

👍

1 similar comment
@stof
Copy link
Member

stof commented Aug 28, 2014

👍

@stof
Copy link
Member

stof commented Aug 28, 2014

Thank you @merk.

@stof stof merged commit cbcf513 into symfony:2.3 Aug 28, 2014
stof added a commit that referenced this pull request Aug 28, 2014
…merk)

This PR was merged into the 2.3 branch.

Discussion
----------

[DoctrineBridge] Abstract Doctrine Subscribers with tags

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | this one
| License       | MIT
| Doc PR        | N/A

I've hit a problem with some doctrine listeners, built by decorating an abstract definition.

I want the abstract definition to hold the tag, however because the RegisterEventListenersAndSubscribersPass runs before abstract definitions are removed, they get added as method calls to the EventManager definition, which once the abstract service is removed, we end up with a method call that breaks the container.

I don't know if this is the best approach, it might be better not to return abstract services when calling `findTaggedServiceIds` instead?

Commits
-------

cbcf513 Disallow abstract definitions from doctrine event listener registration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Doctrine DX DX = Developer eXperience (anything that improves the experience of using Symfony)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants