Skip to content

Commit

Permalink
feature #26803 [Messenger] Add debug:messenger CLI command (ro0NL, sr…
Browse files Browse the repository at this point in the history
…oze)

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
-------

7f87309 fix deps
290c7eb Rename the command `DebugCommand`
9847b83 [Messenger] Add debug:messenger CLI command
  • Loading branch information
sroze committed May 9, 2018
2 parents 9ce5941 + 7f87309 commit 6207d70
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 8 deletions.
Expand Up @@ -273,6 +273,7 @@ public function load(array $configs, ContainerBuilder $container)
$this->registerMessengerConfiguration($config['messenger'], $container, $loader, $config['serializer'], $config['validation']);
} else {
$container->removeDefinition('console.command.messenger_consume_messages');
$container->removeDefinition('console.command.messenger_debug');
}

if ($this->isConfigEnabled($container, $config['web_link'])) {
Expand Down
Expand Up @@ -77,6 +77,11 @@
<tag name="console.command" command="messenger:consume-messages" />
</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" />
</service>

<service id="console.command.router_debug" class="Symfony\Bundle\FrameworkBundle\Command\RouterDebugCommand">
<argument type="service" id="router" />
<tag name="console.command" command="debug:router" />
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/composer.json
Expand Up @@ -41,7 +41,7 @@
"symfony/security": "~3.4|~4.0",
"symfony/form": "^4.1",
"symfony/expression-language": "~3.4|~4.0",
"symfony/messenger": "^4.1",
"symfony/messenger": "^4.1-beta2",
"symfony/process": "~3.4|~4.0",
"symfony/security-core": "~3.4|~4.0",
"symfony/security-csrf": "~3.4|~4.0",
Expand Down
81 changes: 81 additions & 0 deletions src/Symfony/Component/Messenger/Command/DebugCommand.php
@@ -0,0 +1,81 @@
<?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;

/**
* A console command to debug Messenger information.
*
* @author Roland Franssen <franssen.roland@gmail.com>
*
* @experimental in 4.1
*/
class DebugCommand extends Command
{
protected static $defaultName = 'debug:messenger';

private $mapping;

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

$this->mapping = $mapping;
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setDescription('Lists messages you can dispatch using the message bus')
->setHelp(<<<'EOF'
The <info>%command.name%</info> command displays all messages that can be
dispatched using the message bus:
<info>php %command.full_name%</info>
EOF
)
;
}

/**
* {@inheritdoc}
*/
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));
}
}

if ($tableRows) {
$io->table(array(), $tableRows);
} else {
$io->text('No messages were found that have valid handlers.');
}
}
}
Expand Up @@ -104,26 +104,29 @@ private function registerHandlers(ContainerBuilder $container)
}

$definitions = array();
$handlersLocatorMapping = array();
foreach ($handlersByMessage as $message => $handlers) {
if (1 === \count($handlers)) {
$handlersByMessage[$message] = current($handlers);
$handlersLocatorMapping['handler.'.$message] = current($handlers);
} else {
$d = new Definition(ChainHandler::class, array($handlers));
$d->setPrivate(true);
$serviceId = hash('sha1', $message);
$definitions[$serviceId] = $d;
$handlersByMessage[$message] = new Reference($serviceId);
$handlersLocatorMapping['handler.'.$message] = new Reference($serviceId);
}
}
$container->addDefinitions($definitions);

$handlersLocatorMapping = array();
foreach ($handlersByMessage as $message => $handler) {
$handlersLocatorMapping['handler.'.$message] = $handler;
}

$handlerResolver = $container->getDefinition('messenger.handler_resolver');
$handlerResolver->replaceArgument(0, ServiceLocatorTagPass::register($container, $handlersLocatorMapping));

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));
}
}

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

0 comments on commit 6207d70

Please sign in to comment.