10000 [Messenger] Add a command to setup transports · symfony/symfony@1d33d2b · GitHub
[go: up one dir, main page]

Skip to content

Commit 1d33d2b

Browse files
committed
[Messenger] Add a command to setup transports
1 parent 59e6380 commit 1d33d2b

File tree

7 files changed

+119
-1
lines changed

7 files changed

+119
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@
8484
<tag name="console.command" command="messenger:consume-messages" />
8585
</service>
8686

87+
<service id="console.command.messenger_setup_transports" class="Symfony\Component\Messenger\Command\SetupTransportsCommand">
88+
<argument type="service" id="messenger.receiver_locator" />
89+
<argument type="collection" /> <!-- Receiver names -->
90+
91+
<tag name="console.command" command="messenger:setup-transports" />
92+
</service>
93+
8794
<service id="console.command.messenger_debug" class="Symfony\Component\Messenger\Command\DebugCommand">
8895
<argument type="collection" /> <!-- Message to handlers mapping -->
8996
<tag name="console.command" command="debug:messenger" />

src/Symfony/Component/Messenger/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ CHANGELOG
1616
* [BC BREAK] If listening to exceptions while using `AmqpSender` or `AmqpReceiver`, `\AMQPException` is
1717
no longer thrown in favor of `TransportException`.
1818

19+
* Added a `SetupTransportsCommand` command to setup the transports
20+
1921
4.2.0
2022
-----
2123

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Command;
13+
14+
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Input\InputInterface;
16+
use Symfony\Component\Console\Output\OutputInterface;
17+
use Symfony\Component\Console\Style\SymfonyStyle;
18+
use Symfony\Component\DependencyInjection\ContainerInterface;
19+
use Symfony\Component\Messenger\Transport\SetupableTransportInterface;
20+
21+
/**
22+
* @author Vincent Touzet <vincent.touzet@gmail.com>
23+
*/
24+
class SetupTransportsCommand extends Command
25+
{
26+
protected static $defaultName = 'messenger:setup-transports';
27+
28+
private $senderLocator;
29+
private $receiverNames;
30+
31+
public function __construct(ContainerInterface $senderLocator, array $receiverNames = [])
32+
{
33+
$this->senderLocator = $senderLocator;
34+
$this->receiverNames = $receiverNames;
35+
36+
parent::__construct();
37+
}
38+
39+
protected functio 57AE n execute(InputInterface $input, OutputInterface $output)
40+
{
41+
$io = new SymfonyStyle($input, $output);
42+
43+
foreach ($this->receiverNames as $id => $receiverName) {
44+
$receiver = $this->senderLocator->get($id);
45+
if ($receiver instanceof SetupableTransportInterface) {
46+
$receiver->setup();
47+
$io->success("Setup $id");
48+
}
49+
}
50+
}
51+
}

src/Symfony/Component/Messenger/DependencyInjection/MessengerPass.php

Original file line numberDiff line numberDiff line change
@@ -252,6 +252,15 @@ private function registerReceivers(ContainerBuilder $container, array $busIds)
252252
->replaceArgument(4, $busIds);
253253
}
254254

255+
if ($container->hasDefinition('console.command.messenger_setup_transports')) {
256+
$receiverNames = [];
257+
foreach ($receiverMapping as $name => $reference) {
258+
$receiverNames[(string) $reference] = $name;
259+
}
260+
$container->getDefinition('console.command.messenger_setup_transports')
261+
->replaceArgument(1, array_values($receiverNames));
262+
}
263+
255264
$container->getDefinition('messenger.receiver_locator')->replaceArgument(0, $receiverMapping);
256265
}
257266

src/Symfony/Component/Messenger/Tests/DependencyInjection/MessengerPassTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\DependencyInjection\ServiceLocator;
2121
use Symfony\Component\Messenger\Command\ConsumeMessagesCommand;
2222
use Symfony\Component\Messenger\Command\DebugCommand;
23+
use Symfony\Component\Messenger\Command\SetupTransportsCommand;
2324
use Symfony\Component\Messenger\DataCollector\MessengerDataCollector;
2425
use Symfony\Component\Messenger\DependencyInjection\MessengerPass;
2526
use Symfony\Component\Messenger\Envelope;
@@ -265,6 +266,22 @@ public function testItRegistersMultipleReceiversAndSetsTheReceiverNamesOnTheComm
265266
$this->assertSame(['message_bus'], $container->getDefinition('console.command.messenger_consume_messages')->getArgument(4));
266267
}
267268

269+
public function testItSetsTheReceiverNamesOnTheSetupTransportsCommand()
270+
{
271+
$container = $this->getContainerBuilder();
272+
$container->register('console.command.messenger_setup_transports', SetupTransportsCommand::class)->setArguments([
273+
new Reference('messenger.receiver_locator'),
274+
null,
275+
]);
276+
277+
$container->register(AmqpReceiver::class, AmqpReceiver::class)->addTag('messenger.receiver', ['alias' => 'amqp']);
278+
$container->register(DummyReceiver::class, DummyReceiver::class)->addTag('messenger.receiver', ['alias' => 'dummy']);
279+
280+
(new MessengerPass())->process($container);
281+
282+
$this->assertSame(['amqp', 'dummy'], $container->getDefinition('console.command.messenger_setup_transports')->getArgument(1));
283+
}
284+
268285
public function testItShouldNotThrowIfGeneratorIsReturnedInsteadOfArray()
269286
{
270287
$container = $this->getContainerBuilder($busId = 'message_bus');

src/Symfony/Component/Messenger/Transport/AmqpExt/AmqpTransport.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
use Symfony\Component\Messenger\Envelope;
1515
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
1616
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
17+
use Symfony\Component\Messenger\Transport\SetupableTransportInterface;
1718
use Symfony\Component\Messenger\Transport\TransportInterface;
1819

1920
/**
2021
* @author Nicolas Grekas <p@tchwork.com>
2122
*
2223
* @experimental in 4.2
2324
*/
24-
class AmqpTransport implements TransportInterface
25+
class AmqpTransport implements TransportInterface, SetupableTransportInterface
2526 10000
{
2627
private $serializer;
2728
private $connection;
@@ -58,6 +59,14 @@ public function send(Envelope $envelope): Envelope
5859
return ($this->sender ?? $this->getSender())->send($envelope);
5960
}
6061

62+
/**
63+
* {@inheritdoc}
64+
*/
65+
public function setup(): void
66+
{
67+
$this->connection->setup();
68+
}
69+
6170
private function getReceiver()
6271
{
6372
return $this->receiver = new AmqpReceiver($this->connection, $this->serializer);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Transport;
13+
14+
/**
15+
* @author Vincent Touzet <vincent.touzet@gmail.com>
16+
*/
17+
interface SetupableTransportInterface
18+
{
19+
/**
20+
* Setup the transport.
21+
*/
22+
public function setup(): void;
23+
}

0 commit comments

Comments
 (0)
0