Skip to content

[Messenger] Add middleware allowing just for a single handler #28716

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

Closed
wants to merge 1 commit into from

Conversation

pamil
Copy link
Contributor

@pamil pamil commented Oct 3, 2018

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR not yet

It's a quite common middleware, required for command buses.

PS. How do you manage to have autocompletion for PHPUnit classes when writing tests in Symfony since there is no PHPUnit in dependencies?

@pamil pamil force-pushed the messenger-command-bus-middleware branch from d0a42d2 to 144ae61 Compare October 3, 2018 19:48
{
$handler = $this->messageHandlerResolver->resolve($message);

if ($handler instanceof ChainHandler) {
Copy link
Member

Choose a reason for hiding this comment

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

I feel like this is tight coupling to a specific implementation: any other handler could implement a chained behavior, isn't it?
Why do we want to restrict the possibilities here?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, this is coupled with the FrameworkBundle implementation. Unfortunately, I don't have any idea on how to make it better (I don't think that sharing the YAML configuration is better either). @nicolas-grekas do you have an idea? 🤔

Copy link
Contributor Author

@pamil pamil Oct 6, 2018

Choose a reason for hiding this comment

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

Yeah, I also don't think this is the best solution, but I couldn't think of any other way (apart from introducing such a constraint into FrameworkBundle itself).

We want to restrict it only to one handler to prevent any issues with having two handlers handling the same command (which could happen by accident, especially if using autowiring and autoconfiguration).

$handler = $this->messageHandlerResolver->resolve($message);

if ($handler instanceof ChainHandler) {
throw new MoreThanOneHandlerForMessageException(sprintf('More than one handler for message "%s".', \get_class($message)));
Copy link
Contributor

Choose a reason for hiding this comment

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

The other "problem" (or challenge) with the current implementation is that we can't display which are the implementations. It would massively help the developer, isn't it?

@ogizanagi
Copy link
Contributor

I wonder if running this check at runtime for each message dispatched really makes sense. It's not runtime dependent at all (hence, not really valid as a middleware), so perhaps we should rather just make it a bus option ?

@nicolas-grekas nicolas-grekas added this to the next milestone Oct 14, 2018
@sroze
Copy link
Contributor

sroze commented Oct 21, 2018

I wonder if running this check at runtime for each message dispatched really makes sense. It's not runtime dependent at all (hence, not really valid as a middleware), so perhaps we should rather just make it a bus option ?

Sounds reasonable actually. An option and the bus and the MessengerPass to enforce it when registering messages. I'm 👍-ing it!

@sroze
Copy link
Contributor

sroze commented Oct 31, 2018

Note that now ChainHandler has been removed :) (see #29010)

@Tobion
Copy link
Contributor

Tobion commented Nov 1, 2018

I wonder if running this check at runtime for each message dispatched really makes sense. It's not runtime dependent at all (hence, not really valid as a middleware), so perhaps we should rather just make it a bus option ?

Sounds reasonable actually. An option and the bus and the MessengerPass to enforce it when registering messages. I'm 👍-ing it!

I don't think this is really possible.

  1. You would need to detect that handlers register for overlapping class hierarchies with all the parents and interfaces and wildcard * being taken into account. Does not seem feasible.
  2. It still depends on which messages are dispatched. If I don't dispatch a message at all that could cause several handlers to be executed (maybe it's unused), then there is no problem at all. But you cannot know that at container compilation time.

It would be easy to implement as a new argument in

public function __construct(HandlersLocatorInterface $handlersLocator, bool $allowNoHandlers = false)

@ogizanagi
Copy link
Contributor

@Tobion : Sure, this comment is outdated and was referring to another era where the ChainHandler existed and everything was guessed at compilation-time. Perhaps you're right :)

  1. You would need to detect that handlers register for overlapping class hierarchies with all the parents and interfaces and wildcard * being taken into account. Does not seem feasible.

Well, if you want to add this check, (I guess) you're likely to use simple objects without inheritance nor interface in your bus. This check at compilation-time based on MessengerPass::guessHandledClasses might still be valuable.

  1. It still depends on which messages are dispatched. If I don't dispatch a message at all that could cause several handlers to be executed (maybe it's unused), then there is no problem at all. But you cannot know that at container compilation time.

If it's not used, then it should have been removed? This check isn't for a particular message, but for the whole bus. (I mean you're not asking if there is an issue with a specific message but if there isn't one in the whole bus which may be mis-configured).

@nicolas-grekas
Copy link
Member

Closing in favor of #29167, thanks for the PR.

@stof stof removed this from the next milestone Nov 23, 2018
@pamil pamil deleted the messenger-command-bus-middleware branch May 25, 2020 20:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants