-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle][Messenger] Add AsRoutedMessage
attribute
#49143
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
Conversation
36394cc
to
e5fcf36
Compare
Thank you. I think, this feature is certainly useful. What bothers me about the implementation is that we need to register the message classes as services. Given that they should be stupid data containers, having them as services feels wrong. The fact that the pass ignores the service definitions of the message classes entirely shows me that we're abusing the DI container as a descovery mechanism. |
src/Symfony/Component/Messenger/DependencyInjection/MessengerPass.php
Outdated
Show resolved
Hide resolved
@derrabus agree. Should I add an exception if the message class is not a defined service? I understand this is not an ideal solution. I guess this type of "abuse" has already been raised and has no "easy" solution for now 😕 ? |
Please don't. 🙈 |
e5fcf36
to
0c28247
Compare
0c28247
to
aaa5c7c
Compare
Rebased 👍 |
@alexandre-daubois, any thoughts on this concern? I believe it still exists in this PR. |
I'm also not a fan of messages as services. If I understand #51718 correctly, it includes a system to read/parse a set of namespaces for attributes during warmup. Could the same thing be applied here? Could we have a generalized system for this? I've had the need for such a thing myself in the past. |
Closing as we certainly don't want messages as services. |
…sage routing (pounard) This PR was merged into the 7.2 branch. Discussion ---------- [Messenger] Introduce `#[AsMessage]` attribute for message routing | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Fix #57506 | License | MIT Basic implementation of #57506. * Adds the `Symfony\Component\Messenger\Attribute\AsMessage` attribute, with the `$transport` parameter which can be `string` or `array`. * Implement runtime routing in `Symfony\Component\Messenger\Transport\Sender\SendersLocator`. Rationale: * Messages are not services, it cannot be computed during container compilation. * Reflection is very fast, it shouldn't be significant for performances, yet I don't have measured it yet. * YAML configuration and `Symfony\Component\Messenger\Stamp\TransportNamesStamp` will always override the attribute values, allowing users to change hardcoded routing on a per-environment basis. * This is the simplest implementation I could think of for discussion. Links and references: * #33912 where the discussion started, 5 years ago. * #49143 closed PR that was doing the same, but at compile time, rejected because the actual doctrine is that messages should never be services. * #41179 is stilled opened, and awaiting for modifications, but it was written for an earlier version of Symfony and is inactive for a year or so, yet messenger code has evolved since, and this PR will never merge as-is, it requires to be fully rewrote, reason why I opened this new one. Commits ------- d652842 feature #57506 [Messenger] introduce AsMessage attribute for message routing
…sage routing (pounard) This PR was merged into the 7.2 branch. Discussion ---------- [Messenger] Introduce `#[AsMessage]` attribute for message routing | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Fix #57506 | License | MIT Basic implementation of #57506. * Adds the `Symfony\Component\Messenger\Attribute\AsMessage` attribute, with the `$transport` parameter which can be `string` or `array`. * Implement runtime routing in `Symfony\Component\Messenger\Transport\Sender\SendersLocator`. Rationale: * Messages are not services, it cannot be computed during container compilation. * Reflection is very fast, it shouldn't be significant for performances, yet I don't have measured it yet. * YAML configuration and `Symfony\Component\Messenger\Stamp\TransportNamesStamp` will always override the attribute values, allowing users to change hardcoded routing on a per-environment basis. * This is the simplest implementation I could think of for discussion. Links and references: * symfony/symfony#33912 where the discussion started, 5 years ago. * symfony/symfony#49143 closed PR that was doing the same, but at compile time, rejected because the actual doctrine is that messages should never be services. * symfony/symfony#41179 is stilled opened, and awaiting for modifications, but it was written for an earlier version of Symfony and is inactive for a year or so, yet messenger code has evolved since, and this PR will never merge as-is, it requires to be fully rewrote, reason why I opened this new one. Commits ------- d65284239f feature #57506 [Messenger] introduce AsMessage attribute for message routing
This PR add a new
#[AsRoutedMessage]
attribute. This allows to configure a transport on a message class directly, without having to write this in config files.It seems that a try has been made in #41179, but unfortunately, there is no news for a few months now 😕. This is an alternate solution using a compile-time solution whereas the existing PR is using a runtime solution.
The attribute can be used as follow:
On a behavior side, routings defined thanks to this attribute are merged with the ones defined in the configuration.
Finally, I'm not a 100% sure about the attribute naming. Opinons on it are very welcomed!