Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
sroze wants to merge3 commits intosymfony:masterfromsroze:uses-custom-class

Conversation

@sroze
Copy link
Contributor

QA
Branch?master
Bug fix?no
New feature?no
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed ticketsø
LicenseMIT
Doc PRø

When defining multiple buses, it becomes impossible to type-hint theMessageBusInterface. 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:

namespaceApp\Bus;useSymfony\Component\Messenger\DecoratedMessageBus;finalclass CommandBusextends DecoratedMessageBus{}

Configure your bus to use it:

framework:messenger:buses:commands:decorator_class:'App\Bus\CommandBus'

You can now typehint yourCommandBus 🥁 💃 ⭐️

kbond reacted with thumbs up emojiBernardstanislas reacted with hooray emoji
@srozesroze added this to the4.1 milestoneApr 25, 2018
Copy link
Member

@chalasrchalasr left a 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>
*/
abstractclass DecoratedMessageBusimplements MessageBusInterface
Copy link
Member

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 byAbstract so,AbstractMessageBusDecorator?

sroze reacted with thumbs up emoji
/**
* A decorated message bus.
*
* Use this abstract class to created your message bus decorator to specialise your
Copy link
Member

Choose a reason for hiding this comment

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

typoto create

* A decorated message bus.
*
* Use this abstract class to created your message bus decorator to specialise your
* bus instances and type-hint them.
Copy link
Member

@chalasrchalasrApr 26, 2018
edited
Loading

Choose a reason for hiding this comment

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

I suggesttype-hint against them, dependents type-hint one of their dependencies against the type they need


$tagAttributes =array('name' =>$name);
if (isset($bus['decorator_class'])) {
$tagAttributes['decorator_class'] =$bus['decorator_class'];
Copy link
Member

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 implementMessageBusInterface at least? Perhaps give a complete hint about what is needed for using this option

}
}

finalclass MyTypeHintedBusextends DecoratedMessageBus
Copy link
Member

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

@ro0NL
Copy link
Contributor

ro0NL commentedApr 26, 2018
edited
Loading

it becomes impossible to type-hint the MessageBusInterface

you'll get the default bus AFAIK...

You can now typehint your CommandBus

What if the same decorated class is used multiple times across buses?

isn't this just like sayingMy\CommandBus: '@messenger.bus.command', using regular DI wiring. Put different; do we really want/need this feature?

ogizanagi and Tobion reacted with thumbs up emoji

@sroze
Copy link
ContributorAuthor

What if the same decorated class is used multiple times across buses?

Well, you know what is going to happen: the auto-wiring won't be happy :)

isn't this just like saying My\CommandBus: '@messenger.bus.command', using regular DI wiring. Put different; do we really want/need this feature?

Ish. It still means you'd have to create theMy\CommandBus class, but it's a good point. Should we go with just the class and no FWB configuration?

@ro0NL
Copy link
Contributor

Or just a trait maybe =/ i would indeed avoid the FWB config.

@sroze
Copy link
ContributorAuthor

sroze commentedApr 27, 2018
edited
Loading

It works well with the following configuration 👍

services:'App\Bus\CommandBus':decorates:"messenger.bus.commands"

@srozesrozeforce-pushed theuses-custom-class branch 2 times, most recently from99669ba to2b601adCompareApril 27, 2018 09:10
@sroze
Copy link
ContributorAuthor

To be completed by a lovely documentation :)

@kbond
Copy link
Member

In development, with the following configuration:

services:'App\Bus\CommandBus':decorates:"messenger.bus.commands"

The decorated bus is an instance ofSymfony\Component\Messenger\MessageBus notSymfony\Component\Messenger\TraceableMessageBus

@sroze
Copy link
ContributorAuthor

@kbond you need to typehint itherMessageBusInterface or your ownApp\Bus\CommandBus notSymfony\Component\Messenger\MessageBus 😉

@kbond
Copy link
Member

kbond commentedApr 27, 2018
edited
Loading

@sroze I type-hinted my own bus which extends fromAbstractMessageBusDecorator. The$decoratedBus property is not and instance ofSymfony\Component\Messenger\TraceableMessageBus

It is the correct bus it just doesn't show any activity in the profiler.

@sroze
Copy link
ContributorAuthor

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? :)

@kbond
Copy link
Member

There is no error.

messenger config:

framework:messenger:default_bus:eventbuses:command:default_middlewares:falsemiddlewares:                    -logging                    -Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddleware                    -call_message_handlerquery:default_middlewares:falsemiddlewares:                    -logging                    -call_message_handlerevent:middleware:                    -tolerate_no_handler

service config:

services:App\Messenger\CommandBus:decorates:messenger.bus.command

my command bus class:

namespaceApp\Messenger;useSymfony\Component\Messenger\AbstractMessageBusDecorator;finalclass CommandBusextends AbstractMessageBusDecorator{}

controller injecting command bus:

publicfunctionmyAction(CommandBus$commandBus){dump($commandBus);// ...}

Screenshot of dumped$commandBus:

symfony profiler

Notice the decorated bus is justMessageBus notTraceableMessageBus.

@kbond
Copy link
Member

Note, if I change my service config to:

services:App\Messenger\CommandBus:['@messenger.bus.command']

symfony profiler 1

TheTraceableMessageBus is the decoratedBus.

@sroze
Copy link
ContributorAuthor

Oh, excellent. Thank you for checking that. That's what will be in the documentation then :)

kbond reacted with thumbs up emoji

@sroze
Copy link
ContributorAuthor

Status: Ready

Copy link
Contributor

@ro0NLro0NL left a 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.

Copy link
Member

@weaverryanweaverryan left a 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.

@sroze
Copy link
ContributorAuthor

@nicolas-grekas
Copy link
Member

nicolas-grekas commentedMay 4, 2018
edited
Loading

I'm very skeptical about this PR and the related § insymfony/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 today, telling him about a solution we might want to push forward in 4.2 (it's too early to elaborate here for now).

ostrolucky reacted with thumbs up emoji

@sroze
Copy link
ContributorAuthor

sroze commentedMay 4, 2018 via email

As you know, I agree with you... What if we change the documentation toshow an example with creating the interface? For 4.1, this is very likelyto 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 § insymfony/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> .
kbond reacted with thumbs up emoji

@nicolas-grekas
Copy link
Member

nicolas-grekas commentedMay 4, 2018
edited
Loading

No need for any dedicated interfaces, using bindings solves the issue:
$commandBus: @messenger.bus.command

kbond reacted with thumbs up emoji

@sroze
Copy link
ContributorAuthor

sroze commentedMay 4, 2018
edited
Loading

Outch. Not sure how I feel about that one. I'll give a try.

@nicolas-grekas
Copy link
Member

nicolas-grekas commentedMay 5, 2018
edited
Loading

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.

@sroze
Copy link
ContributorAuthor

Closing, as I'll update the documentation to recommend the binding, that makes sense actually.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@chalasrchalasrchalasr left review comments

@weaverryanweaverryanweaverryan approved these changes

+1 more reviewer

@ro0NLro0NLro0NL approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

4.1

Development

Successfully merging this pull request may close these issues.

7 participants

@sroze@ro0NL@kbond@nicolas-grekas@weaverryan@chalasr@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp