WorkflowApplyCommand.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace WorkflowBundle\Command;
  3. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  4. use Symfony\Component\Console\Input\InputInterface;
  5. use Symfony\Component\Console\Input\InputOption;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. use WorkflowBundle\Utils\WorkFlowEntityClasses;
  8. class WorkflowApplyCommand extends ContainerAwareCommand
  9. {
  10. protected function configure()
  11. {
  12. $this
  13. ->setName('workflow:apply')
  14. ->setDescription('Ejecuta una transiciÃn (de un workflow) en una entidad')
  15. ->setHelp('Ejecuta un Action (WorflowBundle\Entity\Action) de workflow para una Entidad (WorkflowBundle\Utils\WorkFlowEntityClasses)')
  16. ->addOption(
  17. 'transition',
  18. null,
  19. InputOption::VALUE_REQUIRED,
  20. 'Transition name'
  21. )
  22. ->addOption(
  23. 'workflow',
  24. null,
  25. InputOption::VALUE_REQUIRED,
  26. 'Workflow name'
  27. )
  28. ->addOption(
  29. 'entity',
  30. null,
  31. InputOption::VALUE_REQUIRED,
  32. 'Entity Class. Possible values: ' . implode(', ', WorkFlowEntityClasses::getConstants())
  33. )
  34. ->addOption(
  35. 'id',
  36. null,
  37. InputOption::VALUE_REQUIRED,
  38. 'Entity id'
  39. )
  40. ;
  41. }
  42. /**
  43. * @param InputInterface $input
  44. * @param OutputInterface $output
  45. */
  46. protected function execute(InputInterface $input, OutputInterface $output)
  47. {
  48. $entity_manager = $this->getContainer()->get("doctrine.orm.entity_manager");
  49. $transition = $input->getOption('transition');
  50. $workflow = $input->getOption('workflow');
  51. $entityClass = $input->getOption('entity');
  52. $entityId = $input->getOption('id');
  53. if(!class_exists($entityClass)){
  54. throw new \Exception("Class '$entityClass' not found");
  55. }
  56. $entity = $entity_manager->getRepository($entityClass)->find($entityId);
  57. if(!$entity){
  58. throw new \Exception("Entity $entityClass with id $entityId not found");
  59. }
  60. $wreg = $this->getContainer()->get("workflow.registry");
  61. $wf = $wreg->get($entity, $workflow);
  62. $transitions = array_map(function($obj){
  63. return $obj->getName();
  64. }, $wf->getEnabledTransitions($entity));
  65. if(!in_array($transition, $transitions)){
  66. throw new \Exception ("Can't apply transition '$transition', only allowed transitions for this entity are '". implode("','", $transitions). "'");
  67. }
  68. $from = array_keys($wf->getMarking($entity)->getPlaces());
  69. $newState = $wf->apply($entity, $transition);
  70. $entity_manager->persist($entity);
  71. $entity_manager->flush($entity);
  72. $to = array_keys($wf->getMarking($entity)->getPlaces());
  73. $output->writeln("Applyed ". $transition . "(".(string)$entity. ") : " . json_encode($from) . " -> " . json_encode($to));
  74. }
  75. }