Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing a bug where a transport could receive a message and dispatch i…
…t to a different bus
- Loading branch information
1 parent
522594a
commit 41069af
Showing
10 changed files
with
269 additions
and
33 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 Symfony 4.2 | ||
* | ||
* @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 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 Symfony 4.2 | ||
* | ||
* @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 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 Symfony 4.2 | ||
* | ||
* @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; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Symfony/Component/Messenger/Tests/Middleware/AddBusNameStampMiddlewareTest.php
This file contains 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.