-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Fixing a bug where messenger:consume could send message to wrong bus #30652
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
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
39 changes: 39 additions & 0 deletions
39
src/Symfony/Component/Messenger/Middleware/AddBusNameStampMiddleware.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,39 @@ | ||
<?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\Envelope; | ||
use Symfony\Component\Messenger\Stamp\BusNameStamp; | ||
|
||
/** | ||
* Adds the BusNameStamp to the bus. | ||
* | ||
* @experimental in 4.3 | ||
* | ||
* @author Ryan Weaver <ryan@symfonycasts.com> | ||
*/ | ||
class AddBusNameStampMiddleware implements MiddlewareInterface | ||
{ | ||
private $busName; | ||
|
||
public function __construct(string $busName) | ||
{ | ||
$this->busName = $busName; | ||
} | ||
|
||
public function handle(Envelope $envelope, StackInterface $stack): Envelope | ||
{ | ||
$envelope = $envelope->with(new BusNameStamp($this->busName)); | ||
|
||
return $stack->next()->handle($envelope, $stack); | ||
} | ||
} |
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,58 @@ | ||
<?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; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Symfony\Component\Messenger\Exception\InvalidArgumentException; | ||
use Symfony\Component\Messenger\Stamp\BusNameStamp; | ||
|
||
/** | ||
* Bus of buses that is routable using a BusNameStamp. | ||
* | ||
* This is useful when passed to Worker: messages received | ||
* from the transport can be sent to the correct bus. | ||
* | ||
* @experimental in 4.3 | ||
* | ||
* @author Ryan Weaver <ryan@symfonycasts.com> | ||
*/ | ||
class RoutableMessageBus implements MessageBusInterface | ||
{ | ||
private $busLocator; | ||
|
||
/** | ||
* @param ContainerInterface $busLocator A locator full of MessageBusInterface objects | ||
*/ | ||
public function __construct(ContainerInterface $busLocator) | ||
{ | ||
$this->busLocator = $busLocator; | ||
} | ||
|
||
public function dispatch($envelope): Envelope | ||
{ | ||
if (!$envelope instanceof Envelope) { | ||
throw new InvalidArgumentException('Messages passed to RoutableMessageBus::dispatch() must be inside an Envelope'); | ||
} | ||
|
||
/** @var BusNameStamp $busNameStamp */ | ||
$busNameStamp = $envelope->last(BusNameStamp::class); | ||
if (null === $busNameStamp) { | ||
throw new InvalidArgumentException('Envelope does not contain a BusNameStamp.'); | ||
} | ||
|
||
if (!$this->busLocator->has($busNameStamp->getBusName())) { | ||
throw new InvalidArgumentException(sprintf('Invalid bus name "%s" on BusNameStamp.', $busNameStamp->getBusName())); | ||
} | ||
|
||
return $this->busLocator->get($busNameStamp->getBusName())->dispatch($envelope); | ||
} | ||
} |
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,34 @@ | ||
<?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\Stamp; | ||
|
||
/** | ||
* Stamp used to identify which bus it was passed to. | ||
* | ||
* @experimental in 4.3 | ||
* | ||
* @author Ryan Weaver <ryan@symfonycasts.com> | ||
*/ | ||
class BusNameStamp implements StampInterface | ||
{ | ||
private $busName; | ||
|
||
public function __construct(string $busName) | ||
{ | ||
$this->busName = $busName; | ||
} | ||
|
||
public function getBusName(): string | ||
{ | ||
return $this->busName; | ||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
src/Symfony/Component/Messenger/Tests/Middleware/AddBusNameStampMiddlewareTest.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,33 @@ | ||
<?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 Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Middleware\AddBusNameStampMiddleware; | ||
use Symfony\Component\Messenger\Stamp\BusNameStamp; | ||
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase; | ||
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; | ||
|
||
class AddBusNameStampMiddlewareTest extends MiddlewareTestCase | ||
{ | ||
public function testItSendsTheMessageToAssignedSender() | ||
{ | ||
$middleware = new AddBusNameStampMiddleware('the_bus_name'); | ||
$envelope = new Envelope(new DummyMessage('the message')); | ||
|
||
$finalEnvelope = $middleware->handle($envelope, $this->getStackMock()); | ||
/** @var BusNameStamp $busNameStamp */ | ||
$busNameStamp = $finalEnvelope->last(BusNameStamp::class); | ||
$this->assertNotNull($busNameStamp); | ||
$this->assertSame('the_bus_name', $busNameStamp->getBusName()); | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.