1313
1414use Symfony \Component \Console \Attribute \AsCommand ;
1515use Symfony \Component \Console \Command \Command ;
16+ use Symfony \Component \Console \Completion \CompletionInput ;
17+ use Symfony \Component \Console \Completion \CompletionSuggestions ;
1618use Symfony \Component \Console \Exception \InvalidArgumentException ;
1719use Symfony \Component \Console \Input \InputArgument ;
1820use Symfony \Component \Console \Input \InputInterface ;
3234#[AsCommand(name: 'workflow:dump ' , description: 'Dump a workflow ' )]
3335class WorkflowDumpCommand extends Command
3436{
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+
3552 /**
3653 * {@inheritdoc}
3754 */
@@ -42,7 +59,7 @@ protected function configure()
4259 new InputArgument ('name ' , InputArgument::REQUIRED , 'A workflow name ' ),
4360 new InputArgument ('marking ' , InputArgument::IS_ARRAY , 'A marking (a list of places) ' ),
4461 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 ' ),
4663 ])
4764 ->setHelp (<<<'EOF'
4865The <info>%command.name%</info> command dumps the graphical representation of a
@@ -61,19 +78,14 @@ protected function configure()
6178 */
6279 protected function execute (InputInterface $ input , OutputInterface $ output ): int
6380 {
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 ));
7585 }
7686
87+ $ type = explode ('. ' , $ workflowId )[0 ];
88+
7789 switch ($ input ->getOption ('dump-format ' )) {
7890 case 'puml ' :
7991 $ transitionType = 'workflow ' === $ type ? PlantUmlDumper::WORKFLOW_TRANSITION : PlantUmlDumper::STATEMACHINE_TRANSITION ;
@@ -96,15 +108,28 @@ protected function execute(InputInterface $input, OutputInterface $output): int
96108 $ marking ->mark ($ place );
97109 }
98110
111+ $ workflow = $ this ->workflows [$ workflowId ];
112+
99113 $ options = [
100- 'name ' => $ serviceId ,
114+ 'name ' => $ workflowId ,
101115 'nofooter ' => true ,
102116 'graph ' => [
103117 'label ' => $ input ->getOption ('label ' ),
104118 ],
105119 ];
106- $ output ->writeln ($ dumper ->dump ($ workflow-> getDefinition () , $ marking , $ options ));
120+ $ output ->writeln ($ dumper ->dump ($ workflow , $ marking , $ options ));
107121
108122 return 0 ;
109123 }
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+ }
110135}
0 commit comments