12
12
namespace Symfony \Bundle \FrameworkBundle \Command ;
13
13
14
14
use Symfony \Component \Console \Command \Command ;
15
+ use Symfony \Component \Console \Completion \CompletionInput ;
16
+ use Symfony \Component \Console \Completion \CompletionSuggestions ;
15
17
use Symfony \Component \Console \Exception \InvalidArgumentException ;
16
18
use Symfony \Component \Console \Input \InputArgument ;
17
19
use Symfony \Component \Console \Input \InputInterface ;
@@ -32,6 +34,20 @@ class WorkflowDumpCommand extends Command
32
34
{
33
35
protected static $ defaultName = 'workflow:dump ' ;
34
36
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
+ }
35
51
36
52
/**
37
53
* {@inheritdoc}
@@ -43,7 +59,7 @@ protected function configure()
43
59
new InputArgument ('name ' , InputArgument::REQUIRED , 'A workflow name ' ),
44
60
new InputArgument ('marking ' , InputArgument::IS_ARRAY , 'A marking (a list of places) ' ),
45
61
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 ' ),
47
63
])
48
64
->setDescription (self ::$ defaultDescription )
49
65
->setHelp (<<<'EOF'
@@ -63,19 +79,14 @@ protected function configure()
63
79
*/
64
80
protected function execute (InputInterface $ input , OutputInterface $ output ): int
65
81
{
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 ));
77
86
}
78
87
88
+ $ type = explode ('. ' , $ workflowId )[0 ];
89
+
79
90
switch ($ input ->getOption ('dump-format ' )) {
80
91
case 'puml ' :
81
92
$ transitionType = 'workflow ' === $ type ? PlantUmlDumper::WORKFLOW_TRANSITION : PlantUmlDumper::STATEMACHINE_TRANSITION ;
@@ -98,15 +109,28 @@ protected function execute(InputInterface $input, OutputInterface $output): int
98
109
$ marking ->mark ($ place );
99
110
}
100
111
112
+ $ workflow = $ this ->workflows [$ workflowId ];
113
+
101
114
$ options = [
102
- 'name ' => $ serviceId ,
115
+ 'name ' => $ workflowId ,
103
116
'nofooter ' => true ,
104
117
'graph ' => [
105
118
'label ' => $ input ->getOption ('label ' ),
106
119
],
107
120
];
108
- $ output ->writeln ($ dumper ->dump ($ workflow-> getDefinition () , $ marking , $ options ));
121
+ $ output ->writeln ($ dumper ->dump ($ workflow , $ marking , $ options ));
109
122
110
123
return 0 ;
111
124
}
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
+ }
112
136
}
0 commit comments