WorkflowApplyCommand.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. ->addOption(
  41. 'no-filters',
  42. null,
  43. InputOption::VALUE_NONE,
  44. 'disable doctrine orm filters'
  45. )
  46. ;
  47. }
  48. /**
  49. * @param InputInterface $input
  50. * @param OutputInterface $output
  51. */
  52. protected function execute(InputInterface $input, OutputInterface $output)
  53. {
  54. $entity_manager = $this->getContainer()->get("doctrine.orm.entity_manager");
  55. $transition = $input->getOption('transition');
  56. $workflow = $input->getOption('workflow');
  57. $entityClass = $input->getOption('entity');
  58. $no_filters = $input->getOption('no-filters');
  59. if ($no_filters) {
  60. foreach ($entity_manager->getFilters()->getEnabledFilters() as $filter_name => $filter) {
  61. $entity_manager->getFilters()->disable($filter_name);
  62. }
  63. }
  64. $entityId = $input->getOption('id');
  65. if (!class_exists($entityClass)) {
  66. throw new \Exception("Class '$entityClass' not found");
  67. }
  68. $entity = $entity_manager->getRepository($entityClass)->find($entityId);
  69. if (!$entity) {
  70. throw new \Exception("Entity $entityClass with id $entityId not found");
  71. }
  72. // Si no se pasa workflow como parametro se busca según el workflow que tenga
  73. // asignado la $entity
  74. if (is_null($workflow)) {
  75. $workflow = "{$entity->getWorkflowType()}.{$entity->getWorkflowName()}";
  76. }
  77. $wreg = $this->getContainer()->get("workflow.registry");
  78. try {
  79. $wf = $wreg->get($entity, $workflow);
  80. } catch (\Exception $ex) {
  81. $wf = $this->getContainer()->get($workflow);
  82. }
  83. $transitions = array_map(function($obj) {
  84. return $obj->getName();
  85. }, $wf->getEnabledTransitions($entity));
  86. if (!in_array($transition, $transitions)) {
  87. // throw new \Exception ("Can't apply transition '$transition', only allowed transitions for this entity are '". implode("','", $transitions). "'");
  88. $output->writeln("Can't apply transition '$transition', only allowed transitions for this entity are '". implode("','", $transitions). "'");
  89. return;
  90. }
  91. $from = array_keys($wf->getMarking($entity)->getPlaces());
  92. $newState = $wf->apply($entity, $transition);
  93. $entity_manager->persist($entity);
  94. $entity_manager->flush($entity);
  95. $to = array_keys($wf->getMarking($entity)->getPlaces());
  96. $output->writeln("Applyed ". $transition . "(".(string)$entity. ") : " . json_encode($from) . " -> " . json_encode($to));
  97. }
  98. }