Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a54ec40

Browse files
committed
[Messenger] Add a command to setup transports
1 parent c7fe1b6 commit a54ec40

File tree

7 files changed

+122
-1
lines changed

7 files changed

+122
-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
@@ -79,6 +79,13 @@
7979
<tag name="console.command" command="messenger:consume-messages" />
8080
</service>
8181

82+
<service id="console.command.messenger_setup_transports" class="Symfony\Component\Messenger\Command\SetupTransportsCommand">
83+
<argument type="service" id="messenger.receiver_locator" />
84+
<argument type="collection" /> <!-- Receiver names -->
85+
86+
<tag name="console.command" command="messenger:setup-transports" />
87+
</service>
88+
8289
<service id="console.command.messenger_debug" class="Symfony\Component\Messenger\Command\DebugCommand">
8390
<argument type="collection" /> <!-- Message to handlers mapping -->
8491
<tag name="console.command" command="debug:messenger" />

src/Symfony/Component/Messenger/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.3.0
5+
-----
6+
7+
* Added a `SetupTransportsCommand` command to setup the transports
8+
49
4.2.0
510
-----
611

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 <[email protected]>
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 <[email protected]>
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 = array())
32+
{
33+
$this->senderLocator = $senderLocator;
34+
$this->receiverNames = $receiverNames;
35+
36+
parent::__construct();
37+
}
38+
39+
protected function 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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,15 @@ private function registerReceivers(ContainerBuilder $container, array $busIds)
257257
->replaceArgument(4, $busIds);
258258
}
259259

260+
if ($container->hasDefinition('console.command.messenger_setup_transports')) {
261+
$receiverNames = array();
262+
foreach ($receiverMapping as $name => $reference) {
263+
$receiverNames[(string) $reference] = $name;
264+
}
265+
$container->getDefinition('console.command.messenger_setup_transports')
266+
->replaceArgument(1, array_values($receiverNames));
267+
}
268+
260269
$container->getDefinition('messenger.receiver_locator')->replaceArgument(0, $receiverMapping);
261270
}
262271

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;
@@ -268,6 +269,22 @@ public function testItRegistersMultipleReceiversAndSetsTheReceiverNamesOnTheComm
268269
$this->assertSame(array('message_bus'), $container->getDefinition('console.command.messenger_consume_messages')->getArgument(4));
269270
}
270271

272+
public function testItSetsTheReceiverNamesOnTheSetupTransportsCommand()
273+
{
274+
$container = $this->getContainerBuilder();
275+
$container->register('console.command.messenger_setup_transports', SetupTransportsCommand::class)->setArguments(array(
276+
new Reference('messenger.receiver_locator'),
277+
null,
278+
));
279+
280+
$container->register(AmqpReceiver::class, AmqpReceiver::class)->addTag('messenger.receiver', array('alias' => 'amqp'));
281+
$container->register(DummyReceiver::class, DummyReceiver::class)->addTag('messenger.receiver', array('alias' => 'dummy'));
282+
283+
(new MessengerPass())->process($container);
284+
285+
$this->assertSame(array('amqp', 'dummy'), $container->getDefinition('console.command.messenger_setup_transports')->getArgument(1));
286+
}
287+
271288
public function testItShouldNotThrowIfGeneratorIsReturnedInsteadOfArray()
272289
{
273290
$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\Serializer;
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 <[email protected]>
2122
*
2223
* @experimental in 4.2
2324
*/
24-
class AmqpTransport implements TransportInterface
25+
class AmqpTransport implements TransportInterface, SetupableTransportInterface
2526
{
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 <[email protected]>
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 <[email protected]>
16+
*/
17+
interface SetupableTransportInterface
18+
{
19+
/**
20+
* Setup the transport
21+
*/
22+
public function setup(): void;
23+
}

0 commit comments

Comments
 (0)