Skip to content

[FrameworkBundle][Messenger] Add support for namespace wildcard in Messenger routing #48531

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

Merged
merged 1 commit into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Add `DomCrawlerAssertionsTrait::assertSelectorCount(int $count, string $selector)`
* Allow to avoid `limit` definition in a RateLimiter configuration when using the `no_limit` policy
* Add `--format` option to the `debug:config` command
* Add support to pass namespace wildcard in `framework.messenger.routing`

6.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2159,7 +2159,11 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder

$messageToSendersMapping = [];
foreach ($config['routing'] as $message => $messageConfiguration) {
if ('*' !== $message && !class_exists($message) && !interface_exists($message, false)) {
if ('*' !== $message && !class_exists($message) && !interface_exists($message, false) && !preg_match('/^(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+\\\\)++\*$/', $message)) {
if (str_contains($message, '*')) {
throw new LogicException(sprintf('Invalid Messenger routing configuration: invalid namespace "%s" wildcard.', $message));
}

throw new LogicException(sprintf('Invalid Messenger routing configuration: class or interface "%s" not found.', $message));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
SecondMessage::class => [
'senders' => ['amqp', 'audit'],
],
'Symfony\*' => 'amqp',
'*' => 'amqp',
],
'transports' => [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

$container->loadFromExtension('framework', [
'http_method_override' => false,
'serializer' => true,
'messenger' => [
'serializer' => [
'default_serializer' => 'messenger.transport.symfony_serializer',
],
'routing' => [
'Symfony\*\DummyMessage' => ['audit'],
],
'transports' => [
'audit' => 'null://',
],
],
]);
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
<framework:routing message-class="*">
<framework:sender service="amqp" />
</framework:routing>
<framework:routing message-class="Symfony\*">
<framework:sender service="amqp" />
</framework:routing>
<framework:transport name="amqp" dsn="amqp://localhost/%2f/messages" />
<framework:transport name="audit" dsn="null://" />
</framework:messenger>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config http-method-override="false">
<framework:serializer enabled="true" />
<framework:messenger>
<framework:serializer default-serializer="messenger.transport.symfony_serializer" />
<framework:routing message-class="Symfony\*\DummyMessage">
<framework:sender service="audit" />
</framework:routing>
<framework:transport name="audit" dsn="null://" />
</framework:messenger>
</framework:config>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ framework:
'Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\DummyMessage': [amqp, messenger.transport.audit]
'Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\SecondMessage':
senders: [amqp, audit]
'Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\*': [amqp]
'Symfony\*': [amqp]
'*': amqp
transports:
amqp: 'amqp://localhost/%2f/messages'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
framework:
http_method_override: false
serializer: true
messenger:
serializer:
default_serializer: messenger.transport.symfony_serializer
routing:
'Symfony\*\DummyMessage': [audit]
transports:
audit: 'null://'
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,13 @@ public function testMessengerMiddlewareFactoryErroneousFormat()
}

public function testMessengerInvalidTransportRouting()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Invalid Messenger routing configuration: invalid namespace "Symfony\*\DummyMessage" wildcard.');
$this->createContainerFromFile('messenger_routing_invalid_wildcard');
}

public function testMessengerInvalidWildcardRouting()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Invalid Messenger routing configuration: the "Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\DummyMessage" class is being routed to a sender called "invalid". This is not a valid transport or service id.');
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
6.3
---

* Add support for namespace wildcards in the HandlersLocator to allow routing multiple messages within the same namespace
* Deprecate `Symfony\Component\Messenger\Transport\InMemoryTransport` and
`Symfony\Component\Messenger\Transport\InMemoryTransportFactory` in favor of
`Symfony\Component\Messenger\Transport\InMemory\InMemoryTransport` and
Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/Messenger/Handler/HandlersLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,22 @@ public static function listTypes(Envelope $envelope): array
return [$class => $class]
+ class_parents($class)
+ class_implements($class)
+ self::listWildcards($class)
+ ['*' => '*'];
}

private static function listWildcards(string $type): array
{
$type .= '\*';
$wildcards = [];
while ($i = strrpos($type, '\\', -3)) {
$type = substr_replace($type, '\*', $i);
$wildcards[$type] = $type;
}

return $wildcards;
}

private function shouldHandle(Envelope $envelope, HandlerDescriptor $handlerDescriptor): bool
{
if (null === $received = $envelope->last(ReceivedStamp::class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,29 @@ public function testItReturnsOnlyHandlersMatchingTransport()
new Envelope(new DummyMessage('Body'), [new ReceivedStamp('transportName')])
)));
}

public function testItReturnsOnlyHandlersMatchingMessageNamespace()
{
$firstHandler = $this->createPartialMock(HandlersLocatorTestCallable::class, ['__invoke']);
$secondHandler = $this->createPartialMock(HandlersLocatorTestCallable::class, ['__invoke']);

$locator = new HandlersLocator([
str_replace('DummyMessage', '*', DummyMessage::class) => [
$first = new HandlerDescriptor($firstHandler, ['alias' => 'one']),
],
str_replace('Fixtures\\DummyMessage', '*', DummyMessage::class) => [
$second = new HandlerDescriptor($secondHandler, ['alias' => 'two']),
],
]);

$first->getName();
$second->getName();

$this->assertEquals([
$first,
$second,
], iterator_to_array($locator->getHandlers(new Envelope(new DummyMessage('Body')))));
}
}

class HandlersLocatorTestCallable
Expand Down