瀏覽代碼

Se agregan mejoras al bundle.

Maximiliano Schvindt 8 年之前
父節點
當前提交
71f5989a98

+ 25 - 4
Entity/Workflow.php

@@ -11,6 +11,8 @@ use Symfony\Component\Yaml\Yaml;
 use Symfony\Bundle\FrameworkBundle\Console\Application;
 use Symfony\Component\Console\Input\ArrayInput;
 use Symfony\Component\Console\Output\BufferedOutput;
+use WorkflowBundle\Validator\Constraints as WorkflowAssert;
+use Symfony\Component\Workflow\Registry;
 
 /**
  * Workflow
@@ -70,6 +72,7 @@ class Workflow
      * @var text
      *
      * @ORM\Column(type="text", nullable=false)
+     * @WorkflowAssert\ContainsYaml
      */
     protected $template;
     
@@ -100,7 +103,8 @@ class Workflow
     protected $tenancyId = 1;
 
     /**
-     * @ORM\Column(type="boolean", nullable=true, columnDefinition="BOOLEAN DEFAULT TRUE")
+     * @ORM\Column(type="boolean", nullable=true, options={"default":true})
+     *
      */
     protected $enable = true;
     
@@ -259,9 +263,9 @@ class Workflow
      */
     public function setTemplate($template)
     {
-        $array = Yaml::parse($template);
-        $text = Yaml::dump($array,100,2);
-        $this->template = $text;
+        //$array = Yaml::parse($template);
+        //$text = Yaml::dump($array,100,2);
+        $this->template = $template;
 
         return $this;
     }
@@ -469,4 +473,21 @@ class Workflow
         }
     }
 
+    public function getInitialPlace($subject) {
+
+        global $kernel;
+
+        try {
+            $registry = $kernel->getContainer()->get("workflow.registry");
+            $definition = $registry->get($subject, $this->name)->getDefinition();
+        } catch (\Exception $e) {
+            return null;
+        }
+        
+        return $definition->getInitialPlace();
+
+        
+
+    }
+
 }

+ 5 - 0
Resources/config/services.yml

@@ -29,3 +29,8 @@ services:
         tags:
             - { name: kernel.event_subscriber }
         arguments: ['@service_container']
+    workflow.twig_extension:
+        class: WorkflowBundle\Twig\WorkflowExtension
+        tags:
+            - { name: twig.extension }
+        arguments: ["@workflow.registry"]

+ 1 - 1
Resources/views/Workflow/show_template.html.twig

@@ -27,5 +27,5 @@
             editor.getSession().setUseWrapMode(true);
         });
     </script>
-    <img src="http://200.50.175.17/ftth/workflows_png/{{object.getName()}}.png" />
+    <img src="http://200.50.175.17/ftth/workflows_png/{{object.getName()}}.png?nocache={{ date().timestamp }}" />
 {% endblock %}

+ 62 - 0
Twig/WorkflowExtension.php

@@ -0,0 +1,62 @@
+<?php
+
+namespace WorkflowBundle\Twig;
+
+use Symfony\Component\Workflow\Registry;
+
+class WorkflowExtension extends \Twig_Extension
+{
+    private $workflowRegistry;
+
+    public function __construct(Registry $workflowRegistry)
+    {
+        $this->workflowRegistry = $workflowRegistry;
+    }
+
+    public function getFunctions()
+    {
+        return array(
+            new \Twig_SimpleFunction('workflow_can', array($this, 'canTransition')),
+            new \Twig_SimpleFunction('workflow_transitions', array($this, 'getEnabledTransitions')),
+            new \Twig_SimpleFunction('workflow_correct_state', array($this, 'isCorrectState'))
+        );
+    }
+
+    public function isCorrectState($object, $name = null) 
+    {
+        try {
+            $places = $this->workflowRegistry->get($object, $name)->getDefinition()->getPlaces();
+        } catch (\Exception $e) {
+            $places = array();
+        }
+
+        $state = $object->getCurrentState();
+        
+        if(is_null($state)) return false;
+
+        if(isset($places[$state])) return true;
+
+        return false;
+    }
+
+    public function canTransition($object, $transition, $name = null)
+    {
+        return $this->workflowRegistry->get($object, $name)->can($object, $transition);
+    }
+
+    public function getEnabledTransitions($object, $name = null)
+    {
+        try {
+            $return = $this->workflowRegistry->get($object, $name)->getEnabledTransitions($object);
+        } catch (\Exception $e) {
+            $return = array();
+        }
+        
+        return $return;
+    }
+
+    public function getName()
+    {
+        return 'workflow';
+    }
+}

+ 1 - 1
Validator/Constraints/ContainsYaml.php

@@ -8,5 +8,5 @@ use Symfony\Component\Validator\Constraint;
  */
 class ContainsYaml extends Constraint
 {
-    public $message = 'workflow_error_yaml_format';
+    public $message = '%message%';
 }

+ 98 - 11
Validator/Constraints/ContainsYamlValidator.php

@@ -4,21 +4,108 @@ namespace WorkflowBundle\Validator\Constraints;
 use Symfony\Component\Validator\Constraint;
 use Symfony\Component\Validator\ConstraintValidator;
 use Symfony\Component\Yaml\Yaml;
+use Symfony\Component\Yaml\Exception\DumpException;
+use Symfony\Component\Yaml\Exception\ParseException;
+use Symfony\Component\Workflow\Definition;
+use Symfony\Component\Workflow\Validator\StateMachineValidator;
+use Symfony\Component\Workflow\Validator\WorkflowValidator;
+use Symfony\Component\Workflow\Transition;
 
 class ContainsYamlValidator extends ConstraintValidator
 {
     public function validate($value, Constraint $constraint)
     {
-        $yaml = Yaml::dump($value,100,2);
-
-        
-        
-        
-        
-        // if (!preg_match('/^[a-zA-Z0-9]+$/', $value, $matches)) {
-        //     $this->context->buildViolation($constraint->message)
-        //         ->setParameter('%string%', $value)
-        //         ->addViolation();
-        // }
+        $valids_keys = array("supports","initial_place","places","transitions");
+
+        $object = $this->context->getObject();
+        $type = $object->getType();
+        $markingType = $object->getMarkingType();
+        $name = $this->context->getObject();;
+
+        //Validate Format YAML
+        try {
+            $yaml = Yaml::parse($value,100,2);
+        } catch (DumpException $e) {
+            $this->context->buildViolation("errors.workflow_template_format_error")->setParameter('%string%', "")->addViolation();
+            return;
+        } catch (ParseException $e) {
+            $this->context->buildViolation("errors.workflow_template_format_error")->setParameter('%string%', "")->addViolation();
+            return;
+        }
+
+        if($yaml) {
+            $no_valid_key = false;
+            foreach($yaml as $key => $d) {
+                if(!in_array($key,$valids_keys)) {
+                    $no_valid_key = true;
+                    $this->context->buildViolation("errors.workflow_template_key_invalid")->setParameter('%key%', $key)->addViolation();
+                }
+            }
+            if($no_valid_key) return;
+        } else {
+            $this->context->buildViolation("errors.workflow_template_format_error")->setParameter('%string%', "")->addViolation();
+            return;
+        }
+
+        //Validate Template SUPPORTS
+        if(isset($yaml['supports'])) {
+            if(!is_array($yaml['supports'])) {
+                $this->context->buildViolation("errors.workflow_template_supports_error")->setParameter('%string%', "")->addViolation();
+            } elseif(count($yaml['supports']) == 0) {
+                $this->context->buildViolation("errors.workflow_template_supports_empty")->setParameter('%string%', "")->addViolation();
+            } else {
+                foreach($yaml['supports'] as $k => $class) {
+                    if(!class_exists($class)) {
+                        $this->context->buildViolation("errors.workflow_template_supports_class_error")->setParameter('%class%', $class)->addViolation();
+                    }
+                }
+            }
+        } else {
+            $this->context->buildViolation("errors.workflow_template_supports_undefined")->setParameter('%string%', "")->addViolation();
+        }
+
+        //Validate Template PLACES
+        $places = array();
+        if(isset($yaml['places'])) {
+            $places = $yaml['places'];
+        } else {
+            $this->context->buildViolation("errors.workflow_template_places_undefined")->setParameter('%string%', "")->addViolation();
+        }
+
+        $transitions = array();
+        if(isset($yaml['transitions'])) {
+            foreach($yaml['transitions'] as $k => $data) {
+
+                $transitions[] = new Transition($k,$data['from'],$data['to']);
+            }
+        } else {
+            $this->context->buildViolation("errors.workflow_template_transitions_undefined")->setParameter('%string%', "")->addViolation();
+        }
+
+        //Validate Definition
+        if($type == "workflow") {
+            $mt = ($markingType == "single_state");
+            $validator = new WorkflowValidator($mt);
+        } else {
+            $validator = new StateMachineValidator();
+        }
+
+        $initialPlace = null;
+        if(isset($yaml['initial_place'])) {
+            $initialPlace = $yaml['initial_place'];
+        }
+
+        try {
+            $definition = new Definition($places,$transitions,$initialPlace);
+            $validator->validate($definition, $name);
+        }  catch (\Exception $e) {
+            $this->context->buildViolation("Exception: ".$e->getMessage())->setParameter('%string%', "")->addViolation();
+            return;
+        }  catch (\LogicException $e) {
+            $this->context->buildViolation("Exception: ".$e->getMessage())->setParameter('%string%', "")->addViolation();
+            return;
+        }
+
+
     }
 }