-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Messenger] Allow to typehint multiple buses #27054
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mainly wording suggestions
* | ||
* @author Samuel Roze <samuel.roze@gmail.com> | ||
*/ | ||
abstract class DecoratedMessageBus implements MessageBusInterface |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If concrete children are decorators then this is a base decorator, isn't it? Also core abstract classes are prefixed by Abstract
so, AbstractMessageBusDecorator
?
/** | ||
* A decorated message bus. | ||
* | ||
* Use this abstract class to created your message bus decorator to specialise your |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo to create
* A decorated message bus. | ||
* | ||
* Use this abstract class to created your message bus decorator to specialise your | ||
* bus instances and type-hint them. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest type-hint against them
, dependents type-hint one of their dependencies against the type they need
@@ -1481,8 +1481,13 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder | |||
throw new LogicException('The Validation middleware is only available when the Validator component is installed and enabled. Try running "composer require symfony/validator".'); | |||
} | |||
|
|||
$tagAttributes = array('name' => $name); | |||
if (isset($bus['decorator_class'])) { | |||
$tagAttributes['decorator_class'] = $bus['decorator_class']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should it fail if the class does not implement MessageBusInterface
at least? Perhaps give a complete hint about what is needed for using this option
@@ -413,3 +425,7 @@ public function handle($message, callable $next) | |||
return $next($message); | |||
} | |||
} | |||
|
|||
final class MyTypeHintedBus extends DecoratedMessageBus |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TypeHinted is misleading here :) a class is a type, method arguments are type-hinted
you'll get the default bus AFAIK...
What if the same decorated class is used multiple times across buses? isn't this just like saying |
Well, you know what is going to happen: the auto-wiring won't be happy :)
Ish. It still means you'd have to create the |
Or just a trait maybe =/ i would indeed avoid the FWB config. |
It works well with the following configuration 👍 services:
'App\Bus\CommandBus':
decorates: "messenger.bus.commands" |
99669ba
to
2b601ad
Compare
To be completed by a lovely documentation :) |
In development, with the following configuration: services:
'App\Bus\CommandBus':
decorates: "messenger.bus.commands" The decorated bus is an instance of |
@kbond you need to typehint ither |
@sroze I type-hinted my own bus which extends from It is the correct bus it just doesn't show any activity in the profiler. |
I'm sorry but I don't understand the problem... "it works well on my machine" 😅 Can you give me the exact error you face and the classes & configuration you have? :) |
There is no error. messenger config: framework:
messenger:
default_bus: event
buses:
command:
default_middlewares: false
middlewares:
- logging
- Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddleware
- call_message_handler
query:
default_middlewares: false
middlewares:
- logging
- call_message_handler
event:
middleware:
- tolerate_no_handler service config: services:
App\Messenger\CommandBus:
decorates: messenger.bus.command my command bus class: namespace App\Messenger;
use Symfony\Component\Messenger\AbstractMessageBusDecorator;
final class CommandBus extends AbstractMessageBusDecorator
{
} controller injecting command bus: public function myAction(CommandBus $commandBus)
{
dump($commandBus);
// ...
} Screenshot of dumped Notice the decorated bus is just |
Oh, excellent. Thank you for checking that. That's what will be in the documentation then :) |
Status: Ready |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure core needs it.. (i'd write this out anyway :)) but 👍 for the intend allthough i'd use a trait to avoid inheritance and own the private decorated bus in my own class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, it seems like a pretty small feature in its final form. So, I think it's ok. @sroze can you open a Docs PR before this gets merged with a quick dump of the final "docs" needed to make this work? I'm not 100% clear which service configuration was finally the correct one from the comments.
I'm very skeptical about this PR and the related § in symfony/symfony-docs#9727. |
As you know, I agree with you... What if we change the documentation to
show an example with creating the interface? For 4.1, this is very likely
to be the only option we can have in IMHO :)
…On Fri, 4 May 2018 at 04:02, Nicolas Grekas ***@***.***> wrote:
I'm very skeptical about this PR and the related § in
symfony/symfony-docs#9727
<symfony/symfony-docs#9727>.
This encourages type-hinting against a concrete implementation and totally
breaks SOLID, isn't it?
I acknowledge there is a DX issue and that this proposal tries to solve
it. Coincidentally, I was talking about the exact same issue with
@weaverryan <https://github.com/weaverryan> today, tell him about a
solution we might want to push forward in 4.2.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#27054 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAxHEXK2IaTUiHVjw76nM1eR29U5okjfks5tu8TJgaJpZM4TkFYV>
.
|
No need for any dedicated interfaces, using bindings solves the issue: |
Outch. Not sure how I feel about that one. I'll give a try. |
As explained, I'm:-1: as this would put users in a dead end. We should tell about using bindings instead, and seek for a better experience in 4.2 that doesn't break SOLID. |
Closing, as I'll update the documentation to recommend the binding, that makes sense actually. |
When defining multiple buses, it becomes impossible to type-hint the
MessageBusInterface
. This PR adds the ability to wrap the bus within a decorator that you will be able to typehint.As a developer, you will create your classes:
Configure your bus to use it:
You can now typehint your
CommandBus
🥁 💃 ⭐️