WorkflowDumpCommand.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Command;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Exception\InvalidArgumentException;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\Workflow\Dumper\GraphvizDumper;
  18. use Symfony\Component\Workflow\Dumper\PlantUmlDumper;
  19. use Symfony\Component\Workflow\Dumper\StateMachineGraphvizDumper;
  20. use Symfony\Component\Workflow\Marking;
  21. /**
  22. * @author Grégoire Pineau <lyrixx@lyrixx.info>
  23. *
  24. * @final
  25. */
  26. class WorkflowDumpCommand extends Command
  27. {
  28. protected static $defaultName = 'workflow:dump';
  29. /**
  30. * {@inheritdoc}
  31. */
  32. protected function configure()
  33. {
  34. $this
  35. ->setDefinition([
  36. new InputArgument('name', InputArgument::REQUIRED, 'A workflow name'),
  37. new InputArgument('marking', InputArgument::IS_ARRAY, 'A marking (a list of places)'),
  38. new InputOption('label', 'l', InputOption::VALUE_REQUIRED, 'Label a graph'),
  39. new InputOption('dump-format', null, InputOption::VALUE_REQUIRED, 'The dump format [dot|puml]', 'dot'),
  40. ])
  41. ->setDescription('Dump a workflow')
  42. ->setHelp(<<<'EOF'
  43. The <info>%command.name%</info> command dumps the graphical representation of a
  44. workflow in different formats
  45. <info>DOT</info>: %command.full_name% <workflow name> | dot -Tpng > workflow.png
  46. <info>PUML</info>: %command.full_name% <workflow name> --dump-format=puml | java -jar plantuml.jar -p > workflow.png
  47. EOF
  48. )
  49. ;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. protected function execute(InputInterface $input, OutputInterface $output): int
  55. {
  56. $container = $this->getApplication()->getKernel()->getContainer();
  57. $serviceId = $input->getArgument('name');
  58. if ($container->has('workflow.'.$serviceId)) {
  59. $workflow = $container->get('workflow.'.$serviceId);
  60. $type = 'workflow';
  61. } elseif ($container->has('state_machine.'.$serviceId)) {
  62. $workflow = $container->get('state_machine.'.$serviceId);
  63. $type = 'state_machine';
  64. } else {
  65. throw new InvalidArgumentException(sprintf('No service found for "workflow.%1$s" nor "state_machine.%1$s".', $serviceId));
  66. }
  67. if ('puml' === $input->getOption('dump-format')) {
  68. $transitionType = 'workflow' === $type ? PlantUmlDumper::WORKFLOW_TRANSITION : PlantUmlDumper::STATEMACHINE_TRANSITION;
  69. $dumper = new PlantUmlDumper($transitionType);
  70. } elseif ('workflow' === $type) {
  71. $dumper = new GraphvizDumper();
  72. } else {
  73. $dumper = new StateMachineGraphvizDumper();
  74. }
  75. $marking = new Marking();
  76. foreach ($input->getArgument('marking') as $place) {
  77. $marking->mark($place);
  78. }
  79. $options = [
  80. 'name' => $serviceId,
  81. 'nofooter' => true,
  82. 'graph' => [
  83. 'label' => $input->getOption('label'),
  84. ],
  85. ];
  86. $output->writeln($dumper->dump($workflow->getDefinition(), $marking, $options));
  87. return 0;
  88. }
  89. }