123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- <?php
- namespace WorkflowBundle\Command;
- use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Input\InputOption;
- use Symfony\Component\Console\Output\OutputInterface;
- use WorkflowBundle\Utils\WorkFlowEntityClasses;
- class WorkflowApplyCommand extends ContainerAwareCommand
- {
- protected function configure()
- {
- $this
- ->setName('workflow:apply')
- ->setDescription('Ejecuta una transici�n (de un workflow) en una entidad')
- ->setHelp('Ejecuta un Action (WorflowBundle\Entity\Action) de workflow para una Entidad (WorkflowBundle\Utils\WorkFlowEntityClasses)')
- ->addOption(
- 'transition',
- null,
- InputOption::VALUE_REQUIRED,
- 'Transition name'
- )
- ->addOption(
- 'workflow',
- null,
- InputOption::VALUE_REQUIRED,
- 'Workflow name'
- )
- ->addOption(
- 'entity',
- null,
- InputOption::VALUE_REQUIRED,
- 'Entity Class. Possible values: ' . implode(', ', WorkFlowEntityClasses::getConstants())
- )
- ->addOption(
- 'id',
- null,
- InputOption::VALUE_REQUIRED,
- 'Entity id'
- )
- ->addOption(
- 'no-filters',
- null,
- InputOption::VALUE_NONE,
- 'disable doctrine orm filters'
- )
- ;
- }
- /**
- * @param InputInterface $input
- * @param OutputInterface $output
- */
- protected function execute(InputInterface $input, OutputInterface $output)
- {
- $entity_manager = $this->getContainer()->get("doctrine.orm.entity_manager");
-
- $transition = $input->getOption('transition');
- $workflow = $input->getOption('workflow');
- $entityClass = $input->getOption('entity');
-
- $no_filters = $input->getOption('no-filters');
- if ($no_filters) {
- foreach ($entity_manager->getFilters()->getEnabledFilters() as $filter_name => $filter) {
- $entity_manager->getFilters()->disable($filter_name);
- }
- }
-
- $entityId = $input->getOption('id');
- if (!class_exists($entityClass)) {
- $output->writeln("Class '$entityClass' not found");
-
- return;
- }
-
- $entity = $entity_manager->getRepository($entityClass)->find($entityId);
- if (!$entity) {
- $output->writeln("Entity $entityClass with id $entityId not found");
-
- return;
- }
-
- // Si no se pasa workflow como parametro se busca según el workflow que tenga
- // asignado la $entity
- if (is_null($workflow)) {
- $workflow = "{$entity->getWorkflowType()}.{$entity->getWorkflowName()}";
- }
-
- $wreg = $this->getContainer()->get("workflow.registry");
- try {
- $wf = $wreg->get($entity, $workflow);
- } catch (\Exception $ex) {
- $wf = $this->getContainer()->get($workflow);
- }
- $transitions = array_map(function($obj) {
- return $obj->getName();
- }, $wf->getEnabledTransitions($entity));
-
- if (!in_array($transition, $transitions)) {
- $output->writeln("Can't apply transition '$transition', only allowed transitions for this entity are '". implode("','", $transitions). "'");
-
- return;
- }
-
- $from = array_keys($wf->getMarking($entity)->getPlaces());
- $newState = $wf->apply($entity, $transition);
- $entity_manager->persist($entity);
- $entity_manager->flush($entity);
-
- $to = array_keys($wf->getMarking($entity)->getPlaces());
-
- $output->writeln("Applyed ". $transition . "(".(string)$entity. ") : " . json_encode($from) . " -> " . json_encode($to));
- }
- }
|