Skip to content

Commit 9f697c0

Browse files
committed
feature #26803 [Messenger] Add debug:messenger CLI command (ro0NL, sroze)
This PR was merged into the 4.1 branch. Discussion ---------- [Messenger] Add debug:messenger CLI command | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #... <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Adds a `debug:messenger` CLI command to expose message classes that can be dispatched. Heavily inspired by `debug:autowiring`. Commits ------- 7f87309c10 fix deps 290c7eb1bc Rename the command `DebugCommand` 9847b83723 [Messenger] Add debug:messenger CLI command
2 parents 4a2a5ee + 3b5c73d commit 9f697c0

File tree

2 files changed

+91
-7
lines changed

2 files changed

+91
-7
lines changed

Command/DebugCommand.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
19+
/**
20+
* A console command to debug Messenger information.
21+
*
22+
* @author Roland Franssen <[email protected]>
23+
*
24+
* @experimental in 4.1
25+
*/
26+
class DebugCommand extends Command
27+
{
28+
protected static $defaultName = 'debug:messenger';
29+
30+
private $mapping;
31+
32+
public function __construct(array $mapping)
33+
{
34+
parent::__construct();
35+
36+
$this->mapping = $mapping;
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
protected function configure()
43+
{
44+
$this
45+
->setDescription('Lists messages you can dispatch using the message bus')
46+
->setHelp(<<<'EOF'
47+
The <info>%command.name%</info> command displays all messages that can be
48+
dispatched using the message bus:
49+
50+
<info>php %command.full_name%</info>
51+
52+
EOF
53+
)
54+
;
55+
}
56+
57+
/**
58+
* {@inheritdoc}
59+
*/
60+
protected function execute(InputInterface $input, OutputInterface $output)
61+
{
62+
$io = new SymfonyStyle($input, $output);
63+
$io->title('Messenger');
64+
$io->text('The following messages can be dispatched:');
65+
$io->newLine();
66+
67+
$tableRows = array();
68+
foreach ($this->mapping as $message => $handlers) {
69+
$tableRows[] = array(sprintf('<fg=cyan>%s</fg=cyan>', $message));
70+
foreach ($handlers as $handler) {
71+
$tableRows[] = array(sprintf(' handled by %s', $handler));
72+
}
73+
}
74+
75+
if ($tableRows) {
76+
$io->table(array(), $tableRows);
77+
} else {
78+
$io->text('No messages were found that have valid handlers.');
79+
}
80+
}
81+
}

DependencyInjection/MessengerPass.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,26 +104,29 @@ private function registerHandlers(ContainerBuilder $container)
104104
}
105105

106106
$definitions = array();
107+
$handlersLocatorMapping = array();
107108
foreach ($handlersByMessage as $message => $handlers) {
108109
if (1 === \count($handlers)) {
109-
$handlersByMessage[$message] = current($handlers);
110+
$handlersLocatorMapping['handler.'.$message] = current($handlers);
110111
} else {
111112
$d = new Definition(ChainHandler::class, array($handlers));
112113
$d->setPrivate(true);
113114
$serviceId = hash('sha1', $message);
114115
$definitions[$serviceId] = $d;
115-
$handlersByMessage[$message] = new Reference($serviceId);
116+
$handlersLocatorMapping['handler.'.$message] = new Reference($serviceId);
116117
}
117118
}
118119
$container->addDefinitions($definitions);
119120

120-
$handlersLocatorMapping = array();
121-
foreach ($handlersByMessage as $message => $handler) {
122-
$handlersLocatorMapping['handler.'.$message] = $handler;
123-
}
124-
125121
$handlerResolver = $container->getDefinition('messenger.handler_resolver');
126122
$handlerResolver->replaceArgument(0, ServiceLocatorTagPass::register($container, $handlersLocatorMapping));
123+
124+
if ($container->hasDefinition('console.command.messenger_debug')) {
125+
$container->getDefinition('console.command.messenger_debug')
126+
->replaceArgument(0, array_map(function (array $handlers): array {
127+
return array_map('strval', $handlers);
128+
}, $handlersByMessage));
129+
}
127130
}
128131

129132
private function guessHandledClasses(\ReflectionClass $handlerClass, string $serviceId): array

0 commit comments

Comments
 (0)