Selaa lähdekoodia

Se formateo el comando workflow:apply. Si no se pasa como argumento un nombre de workflow se saca de la entidad

Espinoza Guillermo 6 vuotta sitten
vanhempi
commit
361a0eafc7
1 muutettua tiedostoa jossa 41 lisäystä ja 39 poistoa
  1. 41 39
      Command/WorkflowApplyCommand.php

+ 41 - 39
Command/WorkflowApplyCommand.php

@@ -14,7 +14,7 @@ class WorkflowApplyCommand extends ContainerAwareCommand
     {
         $this
             ->setName('workflow:apply')
-            ->setDescription('Ejecuta una transiciÃn (de un workflow) en una entidad')
+            ->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',
@@ -57,47 +57,49 @@ class WorkflowApplyCommand extends ContainerAwareCommand
     protected function execute(InputInterface $input, OutputInterface $output)
     {
         $entity_manager = $this->getContainer()->get("doctrine.orm.entity_manager");
-
-	$transition = $input->getOption('transition');
-	$workflow = $input->getOption('workflow');
+        
+        $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);
-		}
-	}
-
+        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)){
-		throw new \Exception("Class '$entityClass' not found");
-	}
-
-	$entity = $entity_manager->getRepository($entityClass)->find($entityId);
-	if(!$entity){
-		throw new \Exception("Entity $entityClass with id $entityId not found");
-	}
+        if (!class_exists($entityClass)) {
+            throw new \Exception("Class '$entityClass' not found");
+        }
         
-	$wreg = $this->getContainer()->get("workflow.registry");
-	$wf = $wreg->get($entity, $workflow);
-
-	$transitions = array_map(function($obj){
-		return $obj->getName();
-	}, $wf->getEnabledTransitions($entity));
-
-	if(!in_array($transition, $transitions)){
-		throw new \Exception ("Can't apply transition '$transition', only allowed transitions for this entity are '". implode("','", $transitions). "'");
-	}
-	$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));
-
+        $entity = $entity_manager->getRepository($entityClass)->find($entityId);
+        if (!$entity) {
+            throw new \Exception("Entity $entityClass with id $entityId not found");
+        }
+        
+        if (is_null($workflow)) {
+            $workflow = "{$entity->getWorkflowType()}.{$entity->getWorkflowName()}";
+        }
+        
+        $wreg = $this->getContainer()->get("workflow.registry");
+        $wf = $wreg->get($entity, $workflow);
+        $transitions = array_map(function($obj) {
+            return $obj->getName();
+        }, $wf->getEnabledTransitions($entity));
+        
+        if (!in_array($transition, $transitions)) {
+            throw new \Exception ("Can't apply transition '$transition', only allowed transitions for this entity are '". implode("','", $transitions). "'");
+        }
+        
+        $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));
     }
 }