1212namespace Symfony \Bundle \FrameworkBundle \Command ;
1313
1414use Symfony \Component \Console \Command \Command ;
15+ use Symfony \Component \Console \Completion \CompletionInput ;
16+ use Symfony \Component \Console \Completion \CompletionSuggestions ;
1517use Symfony \Component \Console \Exception \InvalidArgumentException ;
1618use Symfony \Component \Console \Input \InputArgument ;
1719use 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}
0 commit comments