Luciano Andrade 7 роки тому
батько
коміт
adff227fb4
1 змінених файлів з 72 додано та 0 видалено
  1. 72 0
      Command/WorkflowApplyCommand.php

+ 72 - 0
Command/WorkflowApplyCommand.php

@@ -0,0 +1,72 @@
+<?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'
+            )
+        ;
+    }
+
+    /**
+     * @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');
+        $entityId = $input->getOption('id');
+
+	$entity = $entity_manager->getRepository($entityClass)->find($entityId);
+	if(!$entity){
+		throw \Exception("Entity $entityClass with id $entityId not found");
+	}
+        
+	$wreg = $this->getContainer()->get("workflow.registry");
+	$wf = $wreg->get($entity, $workflow);
+
+	$newState = $wf->apply($entity, $transition);
+	$entity_manager->persist($entity);
+	$entity_manager->flush($entity);
+
+    }
+}