Skip to content

Commit

Permalink
[Messenger] Add a command to setup transports
Browse files Browse the repository at this point in the history
  • Loading branch information
vincenttouzet committed Dec 5, 2018
1 parent c7fe1b6 commit a54ec40
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@
<tag name="console.command" command="messenger:consume-messages" />
</service>

<service id="console.command.messenger_setup_transports" class="Symfony\Component\Messenger\Command\SetupTransportsCommand">
<argument type="service" id="messenger.receiver_locator" />
<argument type="collection" /> <!-- Receiver names -->

<tag name="console.command" command="messenger:setup-transports" />
</service>

<service id="console.command.messenger_debug" class="Symfony\Component\Messenger\Command\DebugCommand">
<argument type="collection" /> <!-- Message to handlers mapping -->
<tag name="console.command" command="debug:messenger" />
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.3.0
-----

* Added a `SetupTransportsCommand` command to setup the transports

4.2.0
-----

Expand Down
51 changes: 51 additions & 0 deletions src/Symfony/Component/Messenger/Command/SetupTransportsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Messenger\Transport\SetupableTransportInterface;

/**
* @author Vincent Touzet <vincent.touzet@gmail.com>
*/
class SetupTransportsCommand extends Command
{
protected static $defaultName = 'messenger:setup-transports';

private $senderLocator;
private $receiverNames;

public function __construct(ContainerInterface $senderLocator, array $receiverNames = array())
{
$this->senderLocator = $senderLocator;
$this->receiverNames = $receiverNames;

parent::__construct();
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);

foreach ($this->receiverNames as $id => $receiverName) {
$receiver = $this->senderLocator->get($id);
if ($receiver instanceof SetupableTransportInterface) {
$receiver->setup();
$io->success("Setup $id");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,15 @@ private function registerReceivers(ContainerBuilder $container, array $busIds)
->replaceArgument(4, $busIds);
}

if ($container->hasDefinition('console.command.messenger_setup_transports')) {
$receiverNames = array();
foreach ($receiverMapping as $name => $reference) {
$receiverNames[(string) $reference] = $name;
}
$container->getDefinition('console.command.messenger_setup_transports')
->replaceArgument(1, array_values($receiverNames));
}

$container->getDefinition('messenger.receiver_locator')->replaceArgument(0, $receiverMapping);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\Messenger\Command\ConsumeMessagesCommand;
use Symfony\Component\Messenger\Command\DebugCommand;
use Symfony\Component\Messenger\Command\SetupTransportsCommand;
use Symfony\Component\Messenger\DataCollector\MessengerDataCollector;
use Symfony\Component\Messenger\DependencyInjection\MessengerPass;
use Symfony\Component\Messenger\Envelope;
Expand Down Expand Up @@ -268,6 +269,22 @@ public function testItRegistersMultipleReceiversAndSetsTheReceiverNamesOnTheComm
$this->assertSame(array('message_bus'), $container->getDefinition('console.command.messenger_consume_messages')->getArgument(4));
}

public function testItSetsTheReceiverNamesOnTheSetupTransportsCommand()
{
$container = $this->getContainerBuilder();
$container->register('console.command.messenger_setup_transports', SetupTransportsCommand::class)->setArguments(array(
new Reference('messenger.receiver_locator'),
null,
));

$container->register(AmqpReceiver::class, AmqpReceiver::class)->addTag('messenger.receiver', array('alias' => 'amqp'));
$container->register(DummyReceiver::class, DummyReceiver::class)->addTag('messenger.receiver', array('alias' => 'dummy'));

(new MessengerPass())->process($container);

$this->assertSame(array('amqp', 'dummy'), $container->getDefinition('console.command.messenger_setup_transports')->getArgument(1));
}

public function testItShouldNotThrowIfGeneratorIsReturnedInsteadOfArray()
{
$container = $this->getContainerBuilder($busId = 'message_bus');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Transport\Serialization\Serializer;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
use Symfony\Component\Messenger\Transport\SetupableTransportInterface;
use Symfony\Component\Messenger\Transport\TransportInterface;

/**
* @author Nicolas Grekas <p@tchwork.com>
*
* @experimental in 4.2
*/
class AmqpTransport implements TransportInterface
class AmqpTransport implements TransportInterface, SetupableTransportInterface
{
private $serializer;
private $connection;
Expand Down Expand Up @@ -58,6 +59,14 @@ public function send(Envelope $envelope): Envelope
return ($this->sender ?? $this->getSender())->send($envelope);
}

/**
* {@inheritdoc}
*/
public function setup(): void
{
$this->connection->setup();
}

private function getReceiver()
{
return $this->receiver = new AmqpReceiver($this->connection, $this->serializer);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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\Transport;

/**
* @author Vincent Touzet <vincent.touzet@gmail.com>
*/
interface SetupableTransportInterface
{
/**
* Setup the transport
*/
public function setup(): void;
}

0 comments on commit a54ec40

Please sign in to comment.