Skip to content

Commit d6c046a

Browse files
committed
Remove the extra layer of complexity with the adapter and adapter factory interface
1 parent bb31661 commit d6c046a

File tree

8 files changed

+59
-105
lines changed

8 files changed

+59
-105
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
use Symfony\Component\Lock\Store\StoreFactory;
6262
use Symfony\Component\Lock\StoreInterface;
6363
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
64-
use Symfony\Component\Messenger\Adapter\Factory\AdapterInterface as MessengerAdapterInterface;
6564
use Symfony\Component\Messenger\Transport\ReceiverInterface;
6665
use Symfony\Component\Messenger\Transport\SenderInterface;
6766
use Symfony\Component\PropertyAccess\PropertyAccessor;
@@ -1473,19 +1472,20 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
14731472
}
14741473

14751474
foreach ($config['adapters'] as $name => $adapter) {
1476-
$container->setDefinition($adapterId = 'messenger.'.$name.'_adapter', (new Definition(MessengerAdapterInterface::class, array(
1475+
$container->setDefinition('messenger.'.$name.'_sender', (new Definition(SenderInterface::class))->setFactory(array(
1476+
new Reference('messenger.adapter_factory'),
1477+
'createSender',
1478+
))->setArguments(array(
14771479
$adapter['dsn'],
14781480
$adapter['options'],
1479-
)))->setFactory(array(new Reference('messenger.adapter_factory'), 'create'))->setPublic(true));
1480-
1481-
$container->setDefinition('messenger.'.$name.'_sender', (new Definition(SenderInterface::class))->setFactory(array(
1482-
new Reference($adapterId),
1483-
'sender',
14841481
))->addTag('messenger.sender'));
14851482

14861483
$container->setDefinition('messenger.'.$name.'_receiver', (new Definition(ReceiverInterface::class))->setFactory(array(
1487-
new Reference($adapterId),
1488-
'receiver',
1484+
new Reference('messenger.adapter_factory'),
1485+
'createReceiver',
1486+
))->setArguments(array(
1487+
$adapter['dsn'],
1488+
$adapter['options'],
14891489
))->addTag('messenger.receiver'));
14901490
}
14911491
}

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

+16-8
Original file line numberDiff line numberDiff line change
@@ -547,20 +547,28 @@ public function testMessengerValidationDisabled()
547547
public function testMessengerAdapter()
548548
{
549549
$container = $this->createContainerFromFile('messenger_adapter');
550-
$this->assertTrue($container->hasDefinition('messenger.default_adapter'));
551550
$this->assertTrue($container->hasDefinition('messenger.default_sender'));
552551
$this->assertTrue($container->getDefinition('messenger.default_sender')->hasTag('messenger.sender'));
553552
$this->assertTrue($container->hasDefinition('messenger.default_receiver'));
554553
$this->assertTrue($container->getDefinition('messenger.default_receiver')->hasTag('messenger.receiver'));
555554

556-
$this->assertTrue($container->hasDefinition('messenger.customised_adapter'));
557-
$factory = $container->getDefinition('messenger.customised_adapter')->getFactory();
558-
$arguments = $container->getDefinition('messenger.customised_adapter')->getArguments();
555+
$this->assertTrue($container->hasDefinition('messenger.customised_sender'));
556+
$senderFactory = $container->getDefinition('messenger.customised_sender')->getFactory();
557+
$senderArguments = $container->getDefinition('messenger.customised_sender')->getArguments();
559558

560-
$this->assertEquals(array(new Reference('messenger.adapter_factory'), 'create'), $factory);
561-
$this->assertEquals(2, count($arguments));
562-
$this->assertEquals('amqp://localhost/%2f/messages?exchange_name=exchange_name', $arguments[0]);
563-
$this->assertEquals(array('queue_name' => 'Queue'), $arguments[1]);
559+
$this->assertEquals(array(new Reference('messenger.adapter_factory'), 'createSender'), $senderFactory);
560+
$this->assertEquals(2, count($senderArguments));
561+
$this->assertEquals('amqp://localhost/%2f/messages?exchange_name=exchange_name', $senderArguments[0]);
562+
$this->assertEquals(array('queue_name' => 'Queue'), $senderArguments[1]);
563+
564+
$this->assertTrue($container->hasDefinition('messenger.customised_receiver'));
565+
$receiverFactory = $container->getDefinition('messenger.customised_receiver')->getFactory();
566+
$receiverArguments = $container->getDefinition('messenger.customised_receiver')->getArguments();
567+
568+
$this->assertEquals(array(new Reference('messenger.adapter_factory'), 'createReceiver'), $receiverFactory);
569+
$this->assertEquals(2, count($receiverArguments));
570+
$this->assertEquals('amqp://localhost/%2f/messages?exchange_name=exchange_name', $receiverArguments[0]);
571+
$this->assertEquals(array('queue_name' => 'Queue'), $receiverArguments[1]);
564572
}
565573

566574
public function testTranslator()

src/Symfony/Component/Messenger/Adapter/Factory/AdapterFactoryInterface.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,19 @@
1111

1212
namespace Symfony\Component\Messenger\Adapter\Factory;
1313

14+
use Symfony\Component\Messenger\Transport\ReceiverInterface;
15+
use Symfony\Component\Messenger\Transport\SenderInterface;
16+
1417
/**
1518
* Creates a Messenger adapter.
1619
*
1720
* @author Samuel Roze <samuel.roze@gmail.com>
1821
*/
1922
interface AdapterFactoryInterface
2023
{
21-
public function create(string $dsn, array $options): AdapterInterface;
24+
public function createReceiver(string $dsn, array $options): ReceiverInterface;
25+
26+
public function createSender(string $dsn, array $options): SenderInterface;
2227

2328
public function supports(string $dsn, array $options): bool;
2429
}

src/Symfony/Component/Messenger/Adapter/Factory/AdapterInterface.php

-25
This file was deleted.

src/Symfony/Component/Messenger/Adapter/Factory/ChainAdapterFactory.php

+17-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Symfony\Component\Messenger\Adapter\Factory;
1313

14+
use Symfony\Component\Messenger\Transport\ReceiverInterface;
15+
use Symfony\Component\Messenger\Transport\SenderInterface;
16+
1417
/**
1518
* @author Samuel Roze <samuel.roze@gmail.com>
1619
*/
@@ -26,15 +29,26 @@ public function __construct(iterable $factories)
2629
$this->factories = $factories;
2730
}
2831

29-
public function create(string $dsn, array $options): AdapterInterface
32+
public function createReceiver(string $dsn, array $options): ReceiverInterface
33+
{
34+
foreach ($this->factories as $factory) {
35+
if ($factory->supports($dsn, $options)) {
36+
return $factory->createReceiver($dsn, $options);
37+
}
38+
}
39+
40+
throw new \InvalidArgumentException(sprintf('No adapter supports the given DSN "%s".', $dsn));
41+
}
42+
43+
public function createSender(string $dsn, array $options): SenderInterface
3044
{
3145
foreach ($this->factories as $factory) {
3246
if ($factory->supports($dsn, $options)) {
33-
return $factory->create($dsn, $options);
47+
return $factory->createSender($dsn, $options);
3448
}
3549
}
3650

37-
throw new \InvalidArgumentException(sprintf('The given DSN "%s" is invalid.', $dsn));
51+
throw new \InvalidArgumentException(sprintf('No adapter supports the given DSN "%s".', $dsn));
3852
}
3953

4054
public function supports(string $dsn, array $options): bool

src/Symfony/Component/Messenger/Adapter/PhpAmqp/AmqpAdapter.php

-50
This file was deleted.

src/Symfony/Component/Messenger/Adapter/PhpAmqp/AmqpAdapterFactory.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111

1212
namespace Symfony\Component\Messenger\Adapter\PhpAmqp;
1313

14-
use Symfony\Component\Messenger\Adapter\Factory\AdapterInterface;
1514
use Symfony\Component\Messenger\Adapter\Factory\AdapterFactoryInterface;
15+
use Symfony\Component\Messenger\Transport\ReceiverInterface;
16+
use Symfony\Component\Messenger\Transport\SenderInterface;
1617
use Symfony\Component\Messenger\Transport\Serialization\DecoderInterface;
1718
use Symfony\Component\Messenger\Transport\Serialization\EncoderInterface;
1819

@@ -32,13 +33,14 @@ public function __construct(EncoderInterface $encoder, DecoderInterface $decoder
3233
$this->debug = $debug;
3334
}
3435

35-
public function create(string $dsn, array $options): AdapterInterface
36+
public function createReceiver(string $dsn, array $options): ReceiverInterface
3637
{
37-
return new AmqpAdapter(
38-
new AmqpReceiver($this->decoder, $this->connection),
39-
new AmqpSender($this->encoder, $this->connection),
40-
Connection::fromDsn($dsn, $options, $this->debug)
41-
);
38+
return new AmqpReceiver($this->decoder, Connection::fromDsn($dsn, $options, $this->debug));
39+
}
40+
41+
public function createSender(string $dsn, array $options): SenderInterface
42+
{
43+
return new AmqpSender($this->encoder, Connection::fromDsn($dsn, $options, $this->debug));
4244
}
4345

4446
public function supports(string $dsn, array $options): bool

src/Symfony/Component/Messenger/Transport/Enhancers/MaximumCountReceiver.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ public function __construct(ReceiverInterface $decoratedReceiver, int $maximumNu
2727
$this->maximumNumberOfMessages = $maximumNumberOfMessages;
2828
}
2929

30-
public function receive(callable $handler) : void
30+
public function receive(callable $handler): void
3131
{
3232
$receivedMessages = 0;
3333

34-
$this->decoratedReceiver->receive(function($message) use ($handler, &$receivedMessages) {
34+
$this->decoratedReceiver->receive(function ($message) use ($handler, &$receivedMessages) {
3535
$handler($message);
3636

3737
if (++$receivedMessages >= $this->maximumNumberOfMessages) {

0 commit comments

Comments
 (0)