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

Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -1483,7 +1483,7 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
}

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

if ($busId === $config['default_bus']) {
$container->setAlias('message_bus', $busId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
<services>
<defaults public="false" />

<!-- Handlers -->
<service id="messenger.handler_resolver" class="Symfony\Component\Messenger\Handler\Locator\ContainerHandlerLocator">
<argument type="service" id="service_container"/>
</service>

<!-- Asynchronous -->
<service id="messenger.asynchronous.routing.sender_locator" class="Symfony\Component\Messenger\Asynchronous\Routing\SenderLocator">
<argument type="service" id="messenger.sender_locator" />
Expand All @@ -32,7 +27,7 @@
<!-- Middleware -->
<service id="messenger.middleware.allow_no_handler" class="Symfony\Component\Messenger\Middleware\AllowNoHandlerMiddleware" abstract="true" />
<service id="messenger.middleware.call_message_handler" class="Symfony\Component\Messenger\Middleware\HandleMessageMiddleware" abstract="true">
<argument type="service" id="messenger.handler_resolver" />
<argument /> <!-- Bus handler resolver -->
</service>

<service id="messenger.middleware.validation" class="Symfony\Component\Messenger\Middleware\ValidationMiddleware" abstract="true">
Expand Down
51 changes: 35 additions & 16 deletions src/Symfony/Component/Messenger/Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Component\Messenger\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
Expand All @@ -31,9 +33,9 @@ class DebugCommand extends Command

public function __construct(array $mapping)
{
parent::__construct();

$this->mapping = $mapping;

parent::__construct();
}

/**
Expand All @@ -42,13 +44,18 @@ public function __construct(array $mapping)
protected function configure()
{
$this
->setDescription('Lists messages you can dispatch using the message bus')
->addArgument('bus', InputArgument::OPTIONAL, sprintf('The bus id (one of %s)', implode(', ', array_keys($this->mapping))), null)
->setDescription('Lists messages you can dispatch using the message buses')
->setHelp(<<<'EOF'
The <info>%command.name%</info> command displays all messages that can be
dispatched using the message bus:
dispatched using the message buses:
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it make sense to add the bus example in the help?

<info>php %command.full_name%</info>
Or for a specific bus only:
<info>php %command.full_name% command_bus</info>

EOF
)
;
Expand All @@ -61,21 +68,33 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$io->title('Messenger');
$io->text('The following messages can be dispatched:');
$io->newLine();

$tableRows = array();
foreach ($this->mapping as $message => $handlers) {
$tableRows[] = array(sprintf('<fg=cyan>%s</fg=cyan>', $message));
foreach ($handlers as $handler) {
$tableRows[] = array(sprintf(' handled by %s', $handler));

$mapping = $this->mapping;
if ($bus = $input->getArgument('bus')) {
if (!isset($mapping[$bus])) {
throw new RuntimeException(sprintf('Bus "%s" does not exist. Known buses are %s.', $bus, implode(', ', array_keys($this->mapping))));
}
$mapping = array($bus => $mapping[$bus]);
}

if ($tableRows) {
$io->table(array(), $tableRows);
} else {
$io->text('No messages were found that have valid handlers.');
foreach ($mapping as $bus => $handlersByMessage) {
$io->section($bus);

$tableRows = array();
foreach ($handlersByMessage as $message => $handlers) {
$tableRows[] = array(sprintf('<fg=cyan>%s</fg=cyan>', $message));
foreach ($handlers as $handler) {
$tableRows[] = array(sprintf(' handled by <info>%s</>', $handler));
}
}

if ($tableRows) {
$io->text('The following messages can be dispatched:');
$io->newLine();
$io->table(array(), $tableRows);
} else {
$io->warning(sprintf('No handled message found in bus "%s".', $bus));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Messenger\Handler\ChainHandler;
use Symfony\Component\Messenger\Handler\Locator\ContainerHandlerLocator;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use Symfony\Component\Messenger\Handler\MessageSubscriberInterface;
use Symfony\Component\Messenger\TraceableMessageBus;
Expand Down Expand Up @@ -51,11 +52,13 @@ public function __construct(string $handlerTag = 'messenger.message_handler', st
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('messenger.handler_resolver')) {
if (!$container->has('message_bus')) {
return;
}

$busIds = array();
foreach ($container->findTaggedServiceIds($this->busTag) as $busId => $tags) {
$busIds[] = $busId;
if ($container->hasParameter($busMiddlewareParameter = $busId.'.middleware')) {
$this->registerBusMiddleware($container, $busId, $container->getParameter($busMiddlewareParameter));

Expand All @@ -69,16 +72,20 @@ public function process(ContainerBuilder $container)

$this->registerReceivers($container);
$this->registerSenders($container);
$this->registerHandlers($container);
$this->registerHandlers($container, $busIds);
}

private function registerHandlers(ContainerBuilder $container)
private function registerHandlers(ContainerBuilder $container, array $busIds)
{
$definitions = array();
$handlersByMessage = array();
$handlersByBusAndMessage = array();

foreach ($container->findTaggedServiceIds($this->handlerTag, true) as $serviceId => $tags) {
foreach ($tags as $tag) {
if (isset($tag['bus']) && !\in_array($tag['bus'], $busIds, true)) {
throw new RuntimeException(sprintf('Invalid handler service "%s": bus "%s" specified on the tag "%s" does not exist (known ones are: %s).', $serviceId, $tag['bus'], $this->handlerTag, implode(', ', $busIds)));
}

$r = $container->getReflectionClass($container->getDefinition($serviceId)->getClass());

if (isset($tag['handles'])) {
Expand All @@ -88,6 +95,7 @@ private function registerHandlers(ContainerBuilder $container)
}

$priority = $tag['priority'] ?? 0;
$handlerBuses = (array) ($tag['bus'] ?? $busIds);

foreach ($handles as $messageClass => $method) {
if (\is_int($messageClass)) {
Expand Down Expand Up @@ -123,38 +131,57 @@ private function registerHandlers(ContainerBuilder $container)
$definitions[$serviceId = '.messenger.method_on_object_wrapper.'.ContainerBuilder::hash($messageClass.':'.$messagePriority.':'.$serviceId.':'.$method)] = $wrapperDefinition;
}

$handlersByMessage[$messageClass][$messagePriority][] = new Reference($serviceId);
foreach ($handlerBuses as $handlerBus) {
$handlersByBusAndMessage[$handlerBus][$messageClass][$messagePriority][] = $serviceId;
}
}
}
}

foreach ($handlersByMessage as $message => $handlers) {
krsort($handlersByMessage[$message]);
$handlersByMessage[$message] = array_merge(...$handlersByMessage[$message]);
foreach ($handlersByBusAndMessage as $bus => $handlersByMessage) {
foreach ($handlersByMessage as $message => $handlersByPriority) {
krsort($handlersByPriority);
$handlersByBusAndMessage[$bus][$message] = array_unique(array_merge(...$handlersByPriority));
}
}

$handlersLocatorMapping = array();
foreach ($handlersByMessage as $message => $handlers) {
if (1 === \count($handlers)) {
$handlersLocatorMapping['handler.'.$message] = current($handlers);
} else {
$d = new Definition(ChainHandler::class, array($handlers));
$d->setPrivate(true);
$serviceId = hash('sha1', $message);
$definitions[$serviceId] = $d;
$handlersLocatorMapping['handler.'.$message] = new Reference($serviceId);
$handlersLocatorMappingByBus = array();
foreach ($handlersByBusAndMessage as $bus => $handlersByMessage) {
foreach ($handlersByMessage as $message => $handlersIds) {
if (1 === \count($handlersIds)) {
$handlersLocatorMappingByBus[$bus]['handler.'.$message] = new Reference(current($handlersIds));
} else {
$chainHandler = new Definition(ChainHandler::class, array(array_map(function (string $handlerId): Reference {
return new Reference($handlerId);
}, $handlersIds)));
$chainHandler->setPrivate(true);
$serviceId = '.messenger.chain_handler.'.ContainerBuilder::hash($bus.$message);
$definitions[$serviceId] = $chainHandler;
$handlersLocatorMappingByBus[$bus]['handler.'.$message] = new Reference($serviceId);
}
}
}
$container->addDefinitions($definitions);

$handlerResolver = $container->getDefinition('messenger.handler_resolver');
$handlerResolver->replaceArgument(0, ServiceLocatorTagPass::register($container, $handlersLocatorMapping));
foreach ($busIds as $bus) {
$container->register($resolverName = "$bus.messenger.handler_resolver", ContainerHandlerLocator::class)
->setArgument(0, ServiceLocatorTagPass::register($container, $handlersLocatorMappingByBus[$bus] ?? array()))
;
if ($container->has($callMessageHandlerId = "$bus.middleware.call_message_handler")) {
$container->getDefinition($callMessageHandlerId)
->replaceArgument(0, new Reference($resolverName))
;
}
}

if ($container->hasDefinition('console.command.messenger_debug')) {
$container->getDefinition('console.command.messenger_debug')
->replaceArgument(0, array_map(function (array $handlers): array {
return array_map('strval', $handlers);
}, $handlersByMessage));
$debugCommandMapping = $handlersByBusAndMessage;
foreach ($busIds as $bus) {
if (!isset($debugCommandMapping[$bus])) {
Copy link
Contributor

@ro0NL ro0NL May 16, 2018

Choose a reason for hiding this comment

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

see previous suggestion, i think this should be checked for at runtime (as well)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added check at runtime in debug command

$debugCommandMapping[$bus] = array();
}
}
$container->getDefinition('console.command.messenger_debug')->replaceArgument(0, $debugCommandMapping);
}
}

Expand Down
Loading