From 9847b8372327394f9070479684e2c30ea0f35354 Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Wed, 4 Apr 2018 19:53:00 +0200 Subject: [PATCH 1/3] [Messenger] Add debug:messenger CLI command --- .../FrameworkExtension.php | 1 + .../Resources/config/console.xml | 5 ++ .../Command/MessengerDebugCommand.php | 81 +++++++++++++++++++ .../DependencyInjection/MessengerPass.php | 17 ++-- 4 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 src/Symfony/Component/Messenger/Command/MessengerDebugCommand.php diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index d8f4b61699a9e..dafdc40219f95 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -277,6 +277,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'])) { diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml index cb71bbb8de8f7..40da27e075da3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml @@ -77,6 +77,11 @@ + + + + + diff --git a/src/Symfony/Component/Messenger/Command/MessengerDebugCommand.php b/src/Symfony/Component/Messenger/Command/MessengerDebugCommand.php new file mode 100644 index 0000000000000..544538dcf8791 --- /dev/null +++ b/src/Symfony/Component/Messenger/Command/MessengerDebugCommand.php @@ -0,0 +1,81 @@ + + * + * 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 + * + * @experimental in 4.1 + */ +class MessengerDebugCommand 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 %command.name% command displays all messages that can be +dispatched using the message bus: + + php %command.full_name% + +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('%s', $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.'); + } + } +} diff --git a/src/Symfony/Component/Messenger/DependencyInjection/MessengerPass.php b/src/Symfony/Component/Messenger/DependencyInjection/MessengerPass.php index 1fd76db8fee25..2af6577f485a8 100644 --- a/src/Symfony/Component/Messenger/DependencyInjection/MessengerPass.php +++ b/src/Symfony/Component/Messenger/DependencyInjection/MessengerPass.php @@ -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 From 290c7eb1bcf9cd9a17d66e63ca226eed191917ec Mon Sep 17 00:00:00 2001 From: Samuel ROZE Date: Wed, 9 May 2018 09:31:45 +0100 Subject: [PATCH 2/3] Rename the command `DebugCommand` --- src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml | 2 +- .../Command/{MessengerDebugCommand.php => DebugCommand.php} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/Symfony/Component/Messenger/Command/{MessengerDebugCommand.php => DebugCommand.php} (97%) diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml index 40da27e075da3..21fe28375f6fb 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml @@ -77,7 +77,7 @@ - + diff --git a/src/Symfony/Component/Messenger/Command/MessengerDebugCommand.php b/src/Symfony/Component/Messenger/Command/DebugCommand.php similarity index 97% rename from src/Symfony/Component/Messenger/Command/MessengerDebugCommand.php rename to src/Symfony/Component/Messenger/Command/DebugCommand.php index 544538dcf8791..f41747dc0f649 100644 --- a/src/Symfony/Component/Messenger/Command/MessengerDebugCommand.php +++ b/src/Symfony/Component/Messenger/Command/DebugCommand.php @@ -23,7 +23,7 @@ * * @experimental in 4.1 */ -class MessengerDebugCommand extends Command +class DebugCommand extends Command { protected static $defaultName = 'debug:messenger'; From 7f87309c10653dcfeb9a46312959cba6380ae670 Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Wed, 9 May 2018 13:00:56 +0200 Subject: [PATCH 3/3] fix deps --- src/Symfony/Bundle/FrameworkBundle/composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/composer.json b/src/Symfony/Bundle/FrameworkBundle/composer.json index 8c1c32194fdea..3dfca6a069709 100644 --- a/src/Symfony/Bundle/FrameworkBundle/composer.json +++ b/src/Symfony/Bundle/FrameworkBundle/composer.json @@ -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",