10000 feature #35978 [Messenger] Show message & handler(s) class descriptio… · symfony/symfony@dbe37de · GitHub
[go: up one dir, main page]

Skip to content

Commit dbe37de

Browse files
committed
feature #35978 [Messenger] Show message & handler(s) class description in debug:messenger (ogizanagi)
This PR was merged into the 5.1-dev branch. Discussion ---------- [Messenger] Show message & handler(s) class description in debug:messenger | Q | A | ------------- | --- | Branch? | master<!-- see below --> | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | N/A <!-- prefix each issue number with "Fix #", if any --> | License | MIT | Doc PR | N/A Similar to the `debug:autowiring` command, add the messages & handlers class description to the `debug:messenger` command. Messenger is a central piece in CQRS applications. Exposing the messages & handlers descriptions in this command is a great way to have a global vision of the covered use-cases (especially if there is a newcomer to the project). Commits ------- 079efdf [Messenger] Show message & handler(s) class description in debug:messenger
2 parents b470af1 + 079efdf commit dbe37de

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

src/Symfony/Component/Messenger/Command/DebugCommand.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,20 @@ protected function execute(InputInterface $input, OutputInterface $output)
8080

8181
$tableRows = [];
8282
foreach ($handlersByMessage as $message => $handlers) {
83+
if ($description = self::getClassDescription($message)) {
84+
$tableRows[] = [sprintf('<comment>%s</>', $description)];
85+
}
86+
8387
$tableRows[] = [sprintf('<fg=cyan>%s</fg=cyan>', $message)];
8488
foreach ($handlers as $handler) {
8589
$tableRows[] = [
8690
sprintf(' handled by <info>%s</>', $handler[0]).$this->formatConditions($handler[1]),
8791
];
92+
if ($handlerDescription = self::getClassDescription($handler[0])) {
93+
$tableRows[] = [sprintf(' <comment>%s</>', $handlerDescription)];
94+
}
8895
}
96+
$tableRows[] = [''];
8997
}
9098

9199
if ($tableRows) {
@@ -113,4 +121,20 @@ private function formatConditions(array $options): string
113121

114122
return ' (when '.implode(', ', $optionsMapping).')';
115123
}
124+
125+
private static function getClassDescription(string $class): string
126+
{
127+
try {
128+
$r = new \ReflectionClass($class);
129+
130+
if ($docComment = $r->getDocComment()) {
131+
$docComment = preg_split('#\n\s*\*\s*[\n@]#', substr($docComment, 3, -2), 2)[0];
132+
133+
return trim(preg_replace('#\s*\n\s*\*\s*#', ' ', $docComment));
134+
}
135+
} catch (\ReflectionException $e) {
136+
}
137+
138+
return '';
139+
}
116140
}

src/Symfony/Component/Messenger/Tests/Command/DebugCommandTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Symfony\Component\Messenger\Command\DebugCommand;
1717
use Symfony\Component\Messenger\Tests\Fixtures\DummyCommand;
1818
use Symfony\Component\Messenger\Tests\Fixtures\DummyCommandHandler;
19+
use Symfony\Component\Messenger\Tests\Fixtures\DummyCommandWithDescription;
20+
use Symfony\Component\Messenger\Tests\Fixtures\DummyCommandWithDescriptionHandler;
1921
use Symfony\Component\Messenger\Tests\Fixtures\DummyQuery;
2022
use Symfony\Component\Messenger\Tests\Fixtures\DummyQueryHandler;
2123
use Symfony\Component\Messenger\Tests\Fixtures\MultipleBusesMessage;
@@ -41,6 +43,7 @@ public function testOutput()
4143
$command = new DebugCommand([
4244
'command_bus' => [
4345
DummyCommand::class => [[DummyCommandHandler::class, ['option1' => '1', 'option2' => '2']]],
46+
DummyCommandWithDescription::class => [[DummyCommandWithDescriptionHandler::class, []]],
4447
MultipleBusesMessage::class => [[MultipleBusesMessageHandler::class, []]],
4548
],
4649
'query_bus' => [
@@ -65,8 +68,15 @@ public function testOutput()
6568
-----------------------------------------------------------------------------------------------------------
6669
Symfony\Component\Messenger\Tests\Fixtures\DummyCommand
6770
handled by Symfony\Component\Messenger\Tests\Fixtures\DummyCommandHandler (when option1=1, option2=2)
71+
72+
Used whenever a test needs to show a message with a class description.
73+
Symfony\Component\Messenger\Tests\Fixtures\DummyCommandWithDescription
74+
handled by Symfony\Component\Messenger\Tests\Fixtures\DummyCommandWithDescriptionHandler
75+
Used whenever a test needs to show a message handler with a class description.
76+
6877
Symfony\Component\Messenger\Tests\Fixtures\MultipleBusesMessage
6978
handled by Symfony\Component\Messenger\Tests\Fixtures\MultipleBusesMessageHandler
79+
7080
-----------------------------------------------------------------------------------------------------------
7181
7282
query_bus
@@ -77,8 +87,10 @@ public function testOutput()
7787
---------------------------------------------------------------------------------------
7888
Symfony\Component\Messenger\Tests\Fixtures\DummyQuery
7989
handled by Symfony\Component\Messenger\Tests\Fixtures\DummyQueryHandler
90+
8091
Symfony\Component\Messenger\Tests\Fixtures\MultipleBusesMessage
8192
handled by Symfony\Component\Messenger\Tests\Fixtures\MultipleBusesMessageHandler
93+
8294
---------------------------------------------------------------------------------------
8395
8496
@@ -101,8 +113,10 @@ public function testOutput()
101113
---------------------------------------------------------------------------------------
102114
Symfony\Component\Messenger\Tests\Fixtures\DummyQuery
103115
handled by Symfony\Component\Messenger\Tests\Fixtures\DummyQueryHandler
116+
104117
Symfony\Component\Messenger\Tests\Fixtures\MultipleBusesMessage
105118
handled by Symfony\Component\Messenger\Tests\Fixtures\MultipleBusesMessageHandler
119+
106120
---------------------------------------------------------------------------------------
107121
108122
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Tests\Fixtures;
13+
14+
/**
15+
* Used whenever a test needs to show a message with a class description.
16+
*/
17+
class DummyCommandWithDescription
18+
{
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Tests\Fixtures;
13+
14+
/**
15+
* Used whenever a test needs to show a message handler with a class description.
16+
*/
17+
class DummyCommandWithDescriptionHandler
18+
{
19+
public function __invoke(DummyCommandWithDescription $command)
20+
{
21+
}
22+
}

0 commit comments

Comments
 (0)
0