-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/Symfony/Component/Messenger/Exception/MoreThanOneHandlerForMessageException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Exception; | ||
|
||
/** | ||
* @author Kamil Kokot <kamil@kokot.me> | ||
*/ | ||
class MoreThanOneHandlerForMessageException extends \LogicException implements ExceptionInterface | ||
{ | ||
} |
43 changes: 43 additions & 0 deletions
43
src/Symfony/Component/Messenger/Middleware/AllowSingleHandlerMiddleware.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Middleware; | ||
|
||
use Symfony\Component\Messenger\Exception\MoreThanOneHandlerForMessageException; | ||
use Symfony\Component\Messenger\Handler\ChainHandler; | ||
use Symfony\Component\Messenger\Handler\Locator\HandlerLocatorInterface; | ||
|
||
/** | ||
* @author Kamil Kokot <kamil@kokot.me> | ||
*/ | ||
class AllowSingleHandlerMiddleware implements MiddlewareInterface | ||
{ | ||
private $messageHandlerResolver; | ||
|
||
public function __construct(HandlerLocatorInterface $messageHandlerResolver) | ||
{ | ||
$this->messageHandlerResolver = $messageHandlerResolver; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handle($message, callable $next) | ||
{ | ||
$handler = $this->messageHandlerResolver->resolve($message); | ||
|
||
if ($handler instanceof ChainHandler) { | ||
throw new MoreThanOneHandlerForMessageException(sprintf('More than one handler for message "%s".', \get_class($message))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
} | ||
|
||
return $next($message); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/Symfony/Component/Messenger/Tests/Middleware/AllowSingleHandlerMiddlewareTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Tests\Middleware; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Messenger\Handler\ChainHandler; | ||
use Symfony\Component\Messenger\Handler\Locator\HandlerLocator; | ||
use Symfony\Component\Messenger\Middleware\AllowSingleHandlerMiddleware; | ||
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; | ||
|
||
class AllowSingleHandlerMiddlewareTest extends TestCase | ||
{ | ||
public function testItCallsNextMiddlewareAndReturnsItsResult(): void | ||
{ | ||
$message = new DummyMessage('Hey'); | ||
|
||
$handler = $this->createPartialMock(\stdClass::class, array('__invoke')); | ||
$handler->method('__invoke')->willReturn('Hello'); | ||
|
||
$next = $this->createPartialMock(\stdClass::class, array('__invoke')); | ||
$next->expects($this->once())->method('__invoke')->with($message)->willReturn('Foo'); | ||
|
||
$middleware = new AllowSingleHandlerMiddleware(new HandlerLocator(array( | ||
DummyMessage::class => $handler, | ||
))); | ||
|
||
$this->assertSame('Foo', $middleware->handle($message, $next)); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Messenger\Exception\MoreThanOneHandlerForMessageException | ||
* @expectedExceptionMessage More than one handler for message "Symfony\Component\Messenger\Tests\Fixtures\DummyMessage". | ||
*/ | ||
public function testItThrowsAnExceptionIfHandlerIsChainHandler(): void | ||
{ | ||
$next = $this->createPartialMock(\stdClass::class, array('__invoke')); | ||
|
||
$middleware = new AllowSingleHandlerMiddleware(new HandlerLocator(array( | ||
DummyMessage::class => new ChainHandler(array(new \stdClass(), new \stdClass())), | ||
))); | ||
|
||
$middleware->handle(new DummyMessage('Hey'), $next); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 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?
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.
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? 🤔Uh oh!
There was an error while loading. Please reload this page.
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.
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).