10000 [FrameworkBundle] Add completion for workflow:dump · symfony/symfony@5d3b60a · GitHub
[go: up one dir, main page]

Skip to content

Commit 5d3b60a

Browse files
StaffNowafabpot
authored andcommitted
[FrameworkBundle] Add completion for workflow:dump
1 parent 4d96f84 commit 5d3b60a

File tree

3 files changed

+83
-14
lines changed

3 files changed

+83
-14
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/WorkflowDumpCommand.php

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Command;
1313

1414
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Completion\CompletionInput;
16+
use Symfony\Component\Console\Completion\CompletionSuggestions;
1517
use Symfony\Component\Console\Exception\InvalidArgumentException;
1618
use Symfony\Component\Console\Input\InputArgument;
1719
use Symfony\Component\Console\Input\InputInterface;
@@ -32,6 +34,20 @@ class WorkflowDumpCommand extends Command
3234
{
3335
protected static $defaultName = 'workflow:dump';
3436
protected static $defaultDescription = 'Dump a workflow';
37+
private $workflows = [];
38+
39+
private const DUMP_FORMAT_OPTIONS = [
40+
'puml',
41+
'mermaid',
42+
'dot',
43+
];
44+
45+
public function __construct(array $workflows)
46+
{
47+
parent::__construct();
48+
49+
$this->workflows = $workflows;
50+
}
3551

3652
/**
3753
* {@inheritdoc}
@@ -43,7 +59,7 @@ protected function configure()
4359
new InputArgument('name', InputArgument::REQUIRED, 'A workflow name'),
4460
new InputArgument('marking', InputArgument::IS_ARRAY, 'A marking (a list of places)'),
4561
new InputOption('label', 'l', InputOption::VALUE_REQUIRED, 'Label a graph'),
46-
new InputOption('dump-format', null, InputOption::VALUE_REQUIRED, 'The dump format [dot|puml]', 'dot'),
62+
new InputOption('dump-format', null, InputOption::VALUE_REQUIRED, 'The dump format ['.implode('|', self::DUMP_FORMAT_OPTIONS).']', 'dot'),
4763
])
4864
->setDescription(self::$defaultDescription)
4965
->setHelp(<<<'EOF'
@@ -63,19 +79,14 @@ protected function configure()
6379
*/
6480
protected function execute(InputInterface $input, OutputInterface $output): int
6581
{
66-
$container = $this->getApplication()->getKernel()->getContainer();
67-
$serviceId = $input->getArgument('name');
68-
69-
if ($container->has('workflow.'.$serviceId)) {
70-
$workflow = $container->get('workflow.'.$serviceId);
71-
$type = 'workflow';
72-
} elseif ($container->has('state_machine.'.$serviceId)) {
73-
$workflow = $container->get('state_machine.'.$serviceId);
74-
$type = 'state_machine';
75-
} else {
76-
throw new InvalidArgumentException(sprintf('No service found for "workflow.%1$s" nor "state_machine.%1$s".', $serviceId));
82+
$workflowId = $input->getArgument('name');
83+
84+
if (!\in_array($workflowId, array_keys($this->workflows), true)) {
85+
throw new InvalidArgumentException(sprintf('No service found for "workflow.%1$s" nor "state_machine.%1$s".', $workflowId));
7786
}
7887

88+
$type = explode('.', $workflowId)[0];
89+
7990
switch ($input->getOption('dump-format')) {
8091
case 'puml':
8192
$transitionType = 'workflow' === $type ? PlantUmlDumper::WORKFLOW_TRANSITION : PlantUmlDumper::STATEMACHINE_TRANSITION;
@@ -98,15 +109,28 @@ protected function execute(InputInterface $input, OutputInterface $output): int
98109
$marking->mark($place);
99110
}
100111

112+
$workflow = $this->workflows[$workflowId];
113+
101114
$options = [
102-
'name' => $serviceId,
115+
'name' => $workflowId,
103116
'nofooter' => true,
104117
'graph' => [
105118
'label' => $input->getOption('label'),
106119
],
107120
];
108-
$output->writeln($dumper->dump($workflow->getDefinition(), $marking, $options));
121+
$output->writeln($dumper->dump($workflow, $marking, $options));
109122

110123
return 0;
111124
}
125+
126+
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
127+
{
128+
if ($input->mustSuggestArgumentValuesFor('name')) {
129+
$suggestions->suggestValues(array_keys($this->workflows));
130+
}
131+
132+
if ($input->mustSuggestOptionValuesFor('dump-format')) {
133+
$suggestions->suggestValues(self::DUMP_FORMAT_OPTIONS);
134+
}
135+
}
112136
}

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,8 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
799799

800800
$registryDefinition = $container->getDefinition('workflow.registry');
801801

802+
$workflows = [];
803+
802804
foreach ($config['workflows'] as $name => $workflow) {
803805
$type = $workflow['type'];
804806
$workflowId = sprintf('%s.%s', $type, $name);
@@ -886,6 +888,8 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
886888
$definitionDefinition->addArgument($initialMarking);
887889
$definitionDefinition->addArgument(new Reference(sprintf('%s.metadata_store', $workflowId)));
888890

891+
$workflows[$workflowId] = $definitionDefinition;
892+
889893
// Create MarkingStore
890894
if (isset($workflow['marking_store']['type'])) {
891895
$markingStoreDefinition = new ChildDefinition('workflow.marking_store.method');
@@ -977,6 +981,9 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
977981
$container->setParameter('workflow.has_guard_listeners', true);
978982
}
979983
}
984+
985+
$commandDumpDefinition = $container->getDefinition('console.command.workflow_dump');
986+
$commandDumpDefinition->setArgument(0, $workflows);
980987
}
981988

982989
private function registerDebugConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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\Bundle\FrameworkBundle\Tests\Command;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bundle\FrameworkBundle\Command\WorkflowDumpCommand;
16+
use Symfony\Component\Console\Application;
17+
use Symfony\Component\Console\Tester\CommandCompletionTester;
18+
19+
class WorkflowDumpCommandTest extends TestCase
20+
{
21+
/**
22+
* @dataProvider provideCompletionSuggestions
23+
*/
24+
public function testComplete(array $input, array $expectedSuggestions)
25+
{
26+
$application = new Application();
27+
$application->add(new WorkflowDumpCommand([]));
28+
29+
$tester = new CommandCompletionTester($application->find('workflow:dump'));
30+
$suggestions = $tester->complete($input, 2);
31+
$this->assertSame($expectedSuggestions, $suggestions);
32+
}
33+
34+
public function provideCompletionSuggestions(): iterable
35+
{
36+
yield 'option --dump-format' => [['--dump-format', ''], ['puml', 'mermaid', 'dot']];
37+
}
38+
}

0 commit comments

Comments
 (0)
0