Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #28399 [Messenger] Add a SenderLocator decoupled from Contain…
…erInterface (fabpot) This PR was merged into the 4.2-dev branch. Discussion ---------- [Messenger] Add a SenderLocator decoupled from ContainerInterface | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | yes | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | upcoming For handler locators, we have a generic `HandlerLocator` class that takes a simple mapping instead of a service locator. The same did not exist for sender locators. So, this PR adds this possibility as well. That allows for something like this: ```php new MessageBus([ new SendMessageMiddleware(new SenderLocator([ Message::class => new AmqpTransport($encoderDecoder, $encoderDecoder, $connection), ])), new HandleMessageMiddleware(new HandlerLocator([ Message::class => new MessageHandler(), ])), ]); ``` Commits ------- e658e15 [Messenger] added a SenderLocator decoupled from ContainerInterface
- Loading branch information
Showing
9 changed files
with
215 additions
and
93 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
38 changes: 38 additions & 0 deletions
38
src/Symfony/Component/Messenger/Asynchronous/Routing/AbstractSenderLocator.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,38 @@ | ||
<?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\Asynchronous\Routing; | ||
|
||
/** | ||
* @author Samuel Roze <samuel.roze@gmail.com> | ||
* | ||
* @internal | ||
*/ | ||
abstract class AbstractSenderLocator implements SenderLocatorInterface | ||
{ | ||
public static function getValueFromMessageRouting(array $mapping, $message) | ||
{ | ||
if (isset($mapping[\get_class($message)])) { | ||
return $mapping[\get_class($message)]; | ||
} | ||
if ($parentsMapping = array_intersect_key($mapping, class_parents($message))) { | ||
return current($parentsMapping); | ||
} | ||
if ($interfaceMapping = array_intersect_key($mapping, class_implements($message))) { | ||
return current($interfaceMapping); | ||
} | ||
if (isset($mapping['*'])) { | ||
return $mapping['*']; | ||
} | ||
|
||
return null; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/Symfony/Component/Messenger/Asynchronous/Routing/ContainerSenderLocator.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,40 @@ | ||
<?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\Asynchronous\Routing; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Symfony\Component\Messenger\Transport\SenderInterface; | ||
|
||
/** | ||
* @author Samuel Roze <samuel.roze@gmail.com> | ||
*/ | ||
class ContainerSenderLocator extends AbstractSenderLocator | ||
{ | ||
private $senderServiceLocator; | ||
private $messageToSenderIdMapping; | ||
|
||
public function __construct(ContainerInterface $senderServiceLocator, array $messageToSenderIdMapping) | ||
{ | ||
$this->senderServiceLocator = $senderServiceLocator; | ||
$this->messageToSenderIdMapping = $messageToSenderIdMapping; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getSenderForMessage($message): ?SenderInterface | ||
{ | ||
$senderId = self::getValueFromMessageRouting($this->messageToSenderIdMapping, $message); | ||
|
||
return $senderId ? $this->senderServiceLocator->get($senderId) : null; | ||
} | ||
} |
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
19 changes: 19 additions & 0 deletions
19
src/Symfony/Component/Messenger/Exception/RuntimeException.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,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 Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
class RuntimeException extends \RuntimeException implements ExceptionInterface | ||
{ | ||
} |
91 changes: 91 additions & 0 deletions
91
src/Symfony/Component/Messenger/Tests/Asynchronous/Routing/ContainerSenderLocatorTest.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,91 @@ | ||
<?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\Asynchronous\Routing; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\DependencyInjection\Container; | ||
use Symfony\Component\Messenger\Asynchronous\Routing\ContainerSenderLocator; | ||
use Symfony\Component\Messenger\Tests\Fixtures\ChildDummyMessage; | ||
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; | ||
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessageInterface; | ||
use Symfony\Component\Messenger\Tests\Fixtures\SecondMessage; | ||
use Symfony\Component\Messenger\Transport\SenderInterface; | ||
|
||
class ContainerSenderLocatorTest extends TestCase | ||
{ | ||
public function testItReturnsTheSenderBasedOnTheMessageClass() | ||
{ | ||
$sender = $this->getMockBuilder(SenderInterface::class)->getMock(); | ||
$container = new Container(); | ||
$container->set('my_amqp_sender', $sender); | ||
|
||
$locator = new ContainerSenderLocator($container, array( | ||
DummyMessage::class => 'my_amqp_sender', | ||
)); | ||
|
||
$this->assertSame($sender, $locator->getSenderForMessage(new DummyMessage('Hello'))); | ||
$this->assertNull($locator->getSenderForMessage(new SecondMessage())); | ||
} | ||
|
||
public function testItReturnsTheSenderBasedOnTheMessageParentClass() | ||
{ | ||
$container = new Container(); | ||
|
||
$sender = $this->getMockBuilder(SenderInterface::class)->getMock(); | ||
$container->set('my_amqp_sender', $sender); | ||
|
||
$apiSender = $this->getMockBuilder(SenderInterface::class)->getMock(); | ||
$container->set('my_api_sender', $apiSender); | ||
|
||
$locator = new ContainerSenderLocator($container, array( | ||
DummyMessageInterface::class => 'my_api_sender', | ||
DummyMessage::class => 'my_amqp_sender', | ||
)); | ||
|
||
$this->assertSame($sender, $locator->getSenderForMessage(new ChildDummyMessage('Hello'))); | ||
$this->assertNull($locator->getSenderForMessage(new SecondMessage())); | ||
} | ||
|
||
public function testItReturnsTheSenderBasedOnTheMessageInterface() | ||
{ | ||
$container = new Container(); | ||
|
||
$sender = $this->getMockBuilder(SenderInterface::class)->getMock(); | ||
$container->set('my_amqp_sender', $sender); | ||
|
||
$locator = new ContainerSenderLocator($container, array( | ||
DummyMessageInterface::class => 'my_amqp_sender', | ||
)); | ||
|
||
$this->assertSame($sender, $locator->getSenderForMessage(new DummyMessage('Hello'))); | ||
$this->assertNull($locator->getSenderForMessage(new SecondMessage())); | ||
} | ||
|
||
public function testItSupportsAWildcardInsteadOfTheMessageClass() | ||
{ | ||
$container = new Container(); | ||
|
||
$sender = $this->getMockBuilder(SenderInterface::class)->getMock(); | ||
$container->set('my_amqp_sender', $sender); | ||
|
||
$apiSender = $this->getMockBuilder(SenderInterface::class)->getMock(); | ||
$container->set('my_api_sender', $apiSender); | ||
|
||
$locator = new ContainerSenderLocator($container, array( | ||
DummyMessage::class => 'my_amqp_sender', | ||
'*' => 'my_api_sender', | ||
)); | ||
|
||
$this->assertSame($sender, $locator->getSenderForMessage(new DummyMessage('Hello'))); | ||
$this->assertSame($apiSender, $locator->getSenderForMessage(new SecondMessage())); | ||
} | ||
} |
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