8000 [Messenger] Allow to typehint multiple buses by sroze · Pull Request #27054 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Messenger] Allow to typehint multiple buses #27054

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Configure the class used to decorate your bus
  • Loading branch information
sroze committed Apr 25, 2018
commit 78c15c19fba3513ac94d727c885daff59fec90d0
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,7 @@ function ($a) {
->addDefaultsIfNotSet()
->children()
->booleanNode('default_middlewares')->defaultTrue()->end()
->scalarNode('class')->end()
->scalarNode('decorator_class')->end()
->arrayNode('middlewares')
->defaultValue(array())
->prototype('scalar')->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1481,13 +1481,14 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
throw new LogicException('The Validation middleware is only available when the Validator component is installed and enabled. Try running "composer require symfony/validator".');
}

$container->setParameter($busId.'.middlewares', $middlewares);
$container->setDefinition($busId, (new Definition($bus['class'] ?? MessageBus::class, array(array())))->addTag('messenger.bus', array('name' => $name)));

if (isset($bus['class'])) {
$container->setAlias($bus['class'], $busId);
$tagAttributes = array('name' => $name);
if (isset($bus['decorator_class'])) {
$tagAttributes['decorator_class'] = $bus['decorator_class'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it fail if the class does not implement MessageBusInterface at least? Perhaps give a complete hint about what is needed for using this option

}

$container->setParameter($busId.'.middlewares', $middlewares);
$container->setDefinition($busId, (new Definition(MessageBus::class, array(array())))->addTag('messenger.bus', $tagAttributes));

if ($name === $config['default_bus']) {
$container->setAlias('message_bus', $busId);
$container->setAlias(MessageBusInterface::class, $busId);
Expand Down
38 changes: 38 additions & 0 deletions src/Symfony/Component/Messenger/DecoratedMessageBus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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;

/**
* A decorated message bus.
*
* Use this abstract class to created your message bus decorator to specialise your
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo to create

* bus instances and type-hint them.
Copy link
Member
@chalasr chalasr Apr 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest type-hint against them, dependents type-hint one of their dependencies against the type they need

*
* @author Samuel Roze <samuel.roze@gmail.com>
*/
abstract class DecoratedMessageBus implements MessageBusInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If concrete children are decorators then this is a base decorator, isn't it? Also core abstract classes are prefixed by Abstract so, AbstractMessageBusDecorator?

{
private $decoratedBus;

public function __construct(MessageBusInterface $decoratedBus)
{
$this->decoratedBus = $decoratedBus;
}

/**
* {@inheritdoc}
*/
public function dispatch($message)
{
return $this->decoratedBus->dispatch($message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public function process(ContainerBuilder $container)
if ($container->hasDefinition('messenger.data_collector')) {
$this->registerBusToCollector($container, $busId, $tags[0]);
}

if (isset($tags[0]['decorator_class'])) {
$container->register($tags[0]['decorator_class'], $tags[0]['decorator_class'])
->setDecoratedService($busId)
->setArguments(array(new Reference($tags[0]['decorator_class'].'.inner')));
}
}

$this->registerReceivers($container);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\Messenger\Adapter\AmqpExt\AmqpSender;
use Symfony\Component\Messenger\ContainerHandlerLocator;
use Symfony\Component\Messenger\DataCollector\MessengerDataCollector;
use Symfony\Component\Messenger\DecoratedMessageBus;
use Symfony\Component\Messenger\DependencyInjection\MessengerPass;
use Symfony\Component\Messenger\Handler\ChainHandler;
use Symfony\Component\Messenger\Handler\MessageSubscriberInterface;
Expand Down Expand Up @@ -260,6 +261,17 @@ public function testRegistersTraceableBusesToCollector()
$this->assertEquals(array(array('registerBus', array('foo', new Reference($debuggedFooBusId))), array('registerBus', array('messenger.bus.bar', new Reference($debuggedBarBusId)))), $container->getDefinition('messenger.data_collector')->getMethodCalls());
}

public function testRegistersBusDecoratedClass()
{
$container = $this->getContainerBuilder();
$container->register($fooBusId = 'messenger.bus.foo', MessageBusInterface::class)->addTag('messenger.bus', array('decorator_class' => MyTypeHintedBus::class));

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

$this->assertTrue($container->hasDefinition(MyTypeHintedBus::class));
$this->assertSame(array($fooBusId, null, 0), $container->getDefinition(MyTypeHintedBus::class)->getDecoratedService());
}

public function testRegistersMiddlewaresFromServices()
{
$container = $this->getContainerBuilder();
Expand Down Expand Up @@ -413,3 +425,7 @@ public function handle($messa 7694 ge, callable $next)
return $next($message);
}
}

final class MyTypeHintedBus extends DecoratedMessageBus
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypeHinted is misleading here :) a class is a type, method arguments are type-hinted

{
}
0