Explorar o código

Workflow event listener para cargar el container en la entidad workflow

gabriel %!s(int64=7) %!d(string=hai) anos
pai
achega
1a6db0c70e
Modificáronse 3 ficheiros con 134 adicións e 86 borrados
  1. 97 85
      Entity/Workflow.php
  2. 30 0
      EventListener/WorkflowEventListener.php
  3. 7 1
      Resources/config/services.yml

+ 97 - 85
Entity/Workflow.php

@@ -13,7 +13,8 @@ use Symfony\Component\Console\Input\ArrayInput;
 use Symfony\Component\Console\Output\BufferedOutput;
 use WorkflowBundle\Validator\Constraints as WorkflowAssert;
 use Symfony\Component\Workflow\Registry;
-// TODO : Remplazar las referencias de "global $kenerl por algo mÃs testeable"
+
+// TODO : Remplazar las referencias de "global $kenerl por algo m�s testeable"
 
 /**
  * Workflow
@@ -33,42 +34,42 @@ class Workflow
      * @ORM\GeneratedValue(strategy="AUTO")
      */
     private $id;
-    
+
     /**
      * @var string
      *
      * @ORM\Column(type="string", length=100, nullable=false)
      */
     protected $name;
-    
+
     /**
      * @var string
      *
      * @ORM\Column(type="string", length=100, nullable=false, options={"default":"state_machine"})
      */
-     protected $type = "state_machine";
-    
+    protected $type = "state_machine";
+
     /**
      * @var string
      *
      * @ORM\Column(type="string", length=100, nullable=false, options={"default":"single_state"})
      */
-     protected $markingType = "single_state";
-    
+    protected $markingType = "single_state";
+
     /**
      * @var string
      *
      * @ORM\Column(type="string", length=100, nullable=false, options={"default":"currentState"})
      */
-     protected $markingName = "currentState";
-    
+    protected $markingName = "currentState";
+
     /**
      * @var string
      *
      * @ORM\Column(type="string", length=350, nullable=true)
      */
     protected $description;
-    
+
     /**
      * @var text
      *
@@ -76,7 +77,7 @@ class Workflow
      * @WorkflowAssert\ContainsYaml
      */
     protected $template;
-    
+
     /**
      * @Gedmo\Timestampable(on="create")
      * @ORM\Column(type="datetime")
@@ -88,7 +89,7 @@ class Workflow
      * @ORM\Column(type="datetime")
      */
     protected $updated;
-    
+
     /**
      * @var int
      *
@@ -101,14 +102,20 @@ class Workflow
      *
      */
     protected $enable = true;
-    
+
     /**
      * @ORM\Column(type="array", nullable=true)
      *
      */
     protected $support = array();
-    
-    
+    /**
+     * var mixed Contiene el contenedor.
+     */
+    private $container;
+
+    /**
+     * Workflow constructor.
+     */
     public function __construct()
     {
         $this->support = array();
@@ -119,9 +126,9 @@ class Workflow
      */
     public function __toString()
     {
-        return (string) $this->name;
+        return (string)$this->name;
     }
-    
+
     /**
      * Get id
      *
@@ -142,7 +149,7 @@ class Workflow
     public function setName($name)
     {
         $_name = strtolower($name);
-        $name = str_replace(" ","_",$_name);
+        $name = str_replace(" ", "_", $_name);
         $this->name = $name;
 
         return $this;
@@ -158,7 +165,7 @@ class Workflow
         return $this->name;
     }
 
-     /**
+    /**
      * Set type
      *
      * @param string $type
@@ -182,7 +189,7 @@ class Workflow
         return $this->type;
     }
 
-     /**
+    /**
      * Set markingType
      *
      * @param string $markingType
@@ -299,7 +306,7 @@ class Workflow
     {
         return $this->updated;
     }
-    
+
     /**
      * Set tenancyId
      *
@@ -339,71 +346,67 @@ class Workflow
     /**
      * Get enable
      *
-     * @return boolean 
+     * @return boolean
      */
     public function getEnable()
     {
         return $this->enable;
     }
 
-    /**  
-     *  @ORM\PostPersist 
+    /**
+     * @ORM\PostPersist
      */
-    public function postPersist(LifecycleEventArgs $event) 
+    public function postPersist(LifecycleEventArgs $event)
     {
         $this->updateWorkflows();
     }
 
-    /**  
-     *  @ORM\PostUpdate 
+    /**
+     * @ORM\PostUpdate
      */
-    public function postUpdate(LifecycleEventArgs $event) 
+    public function postUpdate(LifecycleEventArgs $event)
     {
         $this->updateWorkflows();
     }
-    
-    /**  
-     *  @ORM\PreRemove
+
+    /**
+     * @ORM\PreRemove
      */
-    public function preRemove(LifecycleEventArgs $event) 
+    public function preRemove(LifecycleEventArgs $event)
     {
         $this->updateWorkflows();
     }
 
     // La idea era peticionar el comando y que actualice, pero desde aquí, el comando no obtiene el enable actualizado.
-    public function updateWorkflowsCommand() 
+    public function updateWorkflowsCommand()
     {
-        global $kernel;
-
-        $console = $kernel->getRootDir()."/../bin/console";
+        $console = $this->getContainer()->getParameter('kernel.root_dir') . "/../bin/console";
 
         exec("php {$console} workflow:generate:list");
         exec("php {$console} cache:clear --env=prod");
     }
 
-    public function updateWorkflows() 
+    public function updateWorkflows()
     {
-        global $kernel;
-
-        $em = $kernel->getContainer()->get("doctrine.orm.entity_manager");
-        $workflows = $em->getRepository("WorkflowBundle:Workflow")->findBy(array('enable'=>1));
+        $em = $this->getContainer()->get("doctrine.orm.entity_manager");
+        $workflows = $em->getRepository("WorkflowBundle:Workflow")->findBy(array('enable' => 1));
 
-        $rootDir = $kernel->getRootDir();
+        $rootDir = $this->getContainer()->getParameter('kernel.root_dir');
 
         $workflow_template = array();
         $workflow_template['framework'] = array();
 
         $templates = array();
-        foreach($workflows as $k => $workflow) {
+        foreach ($workflows as $k => $workflow) {
 
             $body = array();
 
             $body['type'] = $workflow->getType();
-           
-            if(is_array($workflow->getMarkingName())) {
-               $markingName = $workflow->getMarkingName();
+
+            if (is_array($workflow->getMarkingName())) {
+                $markingName = $workflow->getMarkingName();
             } else {
-               $markingName = array(0 => $workflow->getMarkingName());
+                $markingName = array(0 => $workflow->getMarkingName());
             }
             $body['marking_store'] = array('type' => $workflow->getMarkingType(), 'arguments' => $markingName);
             $body['supports'] = $workflow->getSupport();
@@ -414,22 +417,22 @@ class Workflow
 
         }
 
-        $workflow_file = $rootDir."/Resources/workflows/workflow_list.yml";
-        
-        $handle = fopen($workflow_file,"w+");
+        $workflow_file = $rootDir . "/Resources/workflows/workflow_list.yml";
+
+        $handle = fopen($workflow_file, "w+");
 
-        if($templates) {
+        if ($templates) {
             $workflow_template['framework'] = array('workflows' => $templates);
-            $yaml = Yaml::dump($workflow_template,100,2);
-            fwrite($handle,$yaml);
+            $yaml = Yaml::dump($workflow_template, 100, 2);
+            fwrite($handle, $yaml);
         } else {
-            fwrite($handle,"");
+            fwrite($handle, "");
         }
 
         fclose($handle);
         chmod($workflow_file, 0777);
 
-        $console = $rootDir."/../bin/console";
+        $console = $rootDir . "/../bin/console";
         exec("php {$console} cache:clear --env=prod");
 
         /*$web_workflows = $rootDir."/../web/workflows_png/";
@@ -442,34 +445,28 @@ class Workflow
         }*/
     }
 
-    public function getDefinition($subject) {
-        global $kernel;
-
-	if($kernel){
-		try {
-		    $registry = $kernel->getContainer()->get("workflow.registry");
-		    $definition = $registry->get($subject, $this->name)->getDefinition();
-		} catch (\Exception $e) {
-		    return null;
-		}
-
-		return $definition;
-	}
+    public function getDefinition($subject)
+    {
+        try {
+            $registry = $this->getContainer()->get("workflow.registry");
+            $definition = $registry->get($subject, $this->name)->getDefinition();
+        } catch (\Exception $e) {
+            return null;
+        }
 
+        return $definition;
     }
 
-    public function getInitialPlace($subject) {
-	global $kernel; 
-	if($kernel){
-		try {
-		    $registry = $kernel->getContainer()->get("workflow.registry");
-		    $definition = $registry->get($subject, $this->name)->getDefinition();
-		} catch (\Exception $e) {
-		    return null;
-		}
-		
-		return $definition->getInitialPlace();
-	}
+    public function getInitialPlace($subject)
+    {
+        try {
+            $registry = $this->getContainer()->get("workflow.registry");
+            $definition = $registry->get($subject, $this->name)->getDefinition();
+        } catch (\Exception $e) {
+            return null;
+        }
+
+        return $definition->getInitialPlace();
     }
 
     /**
@@ -499,14 +496,29 @@ class Workflow
      *
      * Para obtener un workflow desde el registry ya que localiza por object y workflowName
      */
-    public function getSubject() 
-    {  
-        if($this->support) {
-           $className =  "\\".$this->support[0];
-           return new $className();
+    public function getSubject()
+    {
+        if ($this->support) {
+            $className = "\\" . $this->support[0];
+            return new $className();
         }
 
         return null;
     }
 
+    /**
+     * @return mixed
+     */
+    public function getContainer()
+    {
+        return $this->container;
+    }
+
+    /**
+     * @param mixed $container
+     */
+    public function setContainer($container)
+    {
+        $this->container = $container;
+    }
 }

+ 30 - 0
EventListener/WorkflowEventListener.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace WorkflowBundle\EventListener;
+
+use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
+
+
+/**
+ * Setea el container al workflow
+ */
+class WorkflowEventListener
+{
+
+    private $container;
+
+    public function __construct($container)
+    {
+        $this->container = $container;
+    }
+
+    public function postLoad(LifecycleEventArgs $args)
+    {
+        $object = $args->getObject();
+        if ($object instanceof \WorkflowBundle\Entity\Workflow) {
+            $object->setContainer($this->container);
+        }
+        return;
+    }
+
+}

+ 7 - 1
Resources/config/services.yml

@@ -43,4 +43,10 @@ services:
     workflow.workflow_interface.listener:
         class: WorkflowBundle\EventListener\WorkflowInterfaceListener
         tags:
-            - { name: doctrine.event_listener, event: prePersist }       
+            - { name: doctrine.event_listener, event: prePersist }
+
+    global.kernel.entity:
+        class: WorkflowBundle\EventListener\WorkflowEventListener
+        tags:
+            - { name: doctrine.event_listener, event: postLoad }
+        arguments: ['@service_container']