13
13
14
14
use Symfony \Component \Console \Attribute \AsCommand ;
15
15
use Symfony \Component \Console \Command \Command ;
16
+ use Symfony \Component \Console \Completion \CompletionInput ;
17
+ use Symfony \Component \Console \Completion \CompletionSuggestions ;
16
18
use Symfony \Component \Console \Exception \InvalidArgumentException ;
17
19
use Symfony \Component \Console \Input \InputArgument ;
18
20
use Symfony \Component \Console \Input \InputInterface ;
32
34
#[AsCommand(name: 'workflow:dump ' , description: 'Dump a workflow ' )]
33
35
class WorkflowDumpCommand extends Command
34
36
{
37
+ private array $ 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
+ }
51
+
35
52
/**
36
53
* {@inheritdoc}
37
54
*/
@@ -42,7 +59,7 @@ protected function configure()
42
59
new InputArgument ('name ' , InputArgument::REQUIRED , 'A workflow name ' ),
43
60
new InputArgument ('marking ' , InputArgument::IS_ARRAY , 'A marking (a list of places) ' ),
44
61
new InputOption ('label ' , 'l ' , InputOption::VALUE_REQUIRED , 'Label a graph ' ),
45
- 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 ' ),
46
63
])
47
64
->setHelp (<<<'EOF'
48
65
The <info>%command.name%</info> command dumps the graphical representation of a
@@ -61,19 +78,14 @@ protected function configure()
61
78
*/
62
79
protected function execute (InputInterface $ input , OutputInterface $ output ): int
63
80
{
64
- $ container = $ this ->getApplication ()->getKernel ()->getContainer ();
65
- $ serviceId = $ input ->getArgument ('name ' );
66
-
67
- if ($ container ->has ('workflow. ' .$ serviceId )) {
68
- $ workflow = $ container ->get ('workflow. ' .$ serviceId );
69
- $ type = 'workflow ' ;
70
- } elseif ($ container ->has ('state_machine. ' .$ serviceId )) {
71
- $ workflow = $ container ->get ('state_machine. ' .$ serviceId );
72
- $ type = 'state_machine ' ;
73
- } else {
74
- throw new InvalidArgumentException (sprintf ('No service found for "workflow.%1$s" nor "state_machine.%1$s". ' , $ serviceId ));
81
+ $ workflowId = $ input ->getArgument ('name ' );
82
+
83
+ if (!\in_array ($ workflowId , array_keys ($ this ->workflows ), true )) {
84
+ throw new InvalidArgumentException (sprintf ('No service found for "workflow.%1$s" nor "state_machine.%1$s". ' , $ workflowId ));
75
85
}
76
86
87
+ $ type = explode ('. ' , $ workflowId )[0 ];
88
+
77
89
switch ($ input ->getOption ('dump-format ' )) {
78
90
case 'puml ' :
79
91
$ transitionType = 'workflow ' === $ type ? PlantUmlDumper::WORKFLOW_TRANSITION : PlantUmlDumper::STATEMACHINE_TRANSITION ;
@@ -96,15 +108,28 @@ protected function execute(InputInterface $input, OutputInterface $output): int
96
108
$ marking ->mark ($ place );
97
109
}
98
110
111
+ $ workflow = $ this ->workflows [$ workflowId ];
112
+
99
113
$ options = [
100
- 'name ' => $ serviceId ,
114
+ 'name ' => $ workflowId ,
101
115
'nofooter ' => true ,
102
116
'graph ' => [
103
117
'label ' => $ input ->getOption ('label ' ),
104
118
],
105
119
];
106
- $ output ->writeln ($ dumper ->dump ($ workflow-> getDefinition () , $ marking , $ options ));
120
+ $ output ->writeln ($ dumper ->dump ($ workflow , $ marking , $ options ));
107
121
108
122
return 0 ;
109
123
}
124
+
125
+ public function complete (CompletionInput $ input , CompletionSuggestions $ suggestions ): void
126
+ {
127
+ if ($ input ->mustSuggestArgumentValuesFor ('name ' )) {
128
+ $ suggestions ->suggestValues (array_keys ($ this ->workflows ));
129
+ }
130
+
131
+ if ($ input ->mustSuggestOptionValuesFor ('dump-format ' )) {
132
+ $ suggestions ->suggestValues (self ::DUMP_FORMAT_OPTIONS );
133
+ }
134
+ }
110
135
}
0 commit comments