ContainsYamlValidator.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace WorkflowBundle\Validator\Constraints;
  3. use Symfony\Component\Validator\Constraint;
  4. use Symfony\Component\Validator\ConstraintValidator;
  5. use Symfony\Component\Yaml\Yaml;
  6. use Symfony\Component\Yaml\Exception\DumpException;
  7. use Symfony\Component\Yaml\Exception\ParseException;
  8. use Symfony\Component\Workflow\Definition;
  9. use Symfony\Component\Workflow\Validator\StateMachineValidator;
  10. use Symfony\Component\Workflow\Validator\WorkflowValidator;
  11. use Symfony\Component\Workflow\Transition;
  12. class ContainsYamlValidator extends ConstraintValidator
  13. {
  14. public function validate($value, Constraint $constraint)
  15. {
  16. $valids_keys = array("supports","initial_place","places","transitions");
  17. $object = $this->context->getObject();
  18. $type = $object->getType();
  19. $markingType = $object->getMarkingType();
  20. $name = $this->context->getObject();;
  21. //Validate Format YAML
  22. try {
  23. $yaml = Yaml::parse($value,100,2);
  24. } catch (DumpException $e) {
  25. $this->context->buildViolation("errors.workflow_template_format_error")->setParameter('%string%', "")->addViolation();
  26. return;
  27. } catch (ParseException $e) {
  28. $this->context->buildViolation("errors.workflow_template_format_error")->setParameter('%string%', "")->addViolation();
  29. return;
  30. }
  31. if(!is_array($yaml)) {
  32. $this->context->buildViolation("errors.workflow_template_format_error")->setParameter('%string%', "")->addViolation();
  33. return;
  34. }
  35. if($yaml) {
  36. $no_valid_key = false;
  37. foreach($yaml as $key => $d) {
  38. if(!in_array($key,$valids_keys)) {
  39. $no_valid_key = true;
  40. $this->context->buildViolation("errors.workflow_template_key_invalid")->setParameter('%key%', $key)->addViolation();
  41. }
  42. }
  43. if($no_valid_key) return;
  44. } else {
  45. $this->context->buildViolation("errors.workflow_template_format_error")->setParameter('%string%', "")->addViolation();
  46. return;
  47. }
  48. //Validate Template SUPPORTS
  49. if(isset($yaml['supports'])) {
  50. if(!is_array($yaml['supports'])) {
  51. $this->context->buildViolation("errors.workflow_template_supports_error")->setParameter('%string%', "")->addViolation();
  52. } elseif(count($yaml['supports']) == 0) {
  53. $this->context->buildViolation("errors.workflow_template_supports_empty")->setParameter('%string%', "")->addViolation();
  54. } else {
  55. foreach($yaml['supports'] as $k => $class) {
  56. if(!class_exists($class)) {
  57. $this->context->buildViolation("errors.workflow_template_supports_class_error")->setParameter('%class%', $class)->addViolation();
  58. }
  59. }
  60. }
  61. } else {
  62. $this->context->buildViolation("errors.workflow_template_supports_undefined")->setParameter('%string%', "")->addViolation();
  63. }
  64. //Validate Template PLACES
  65. $places = array();
  66. if(isset($yaml['places'])) {
  67. $places = $yaml['places'];
  68. } else {
  69. $this->context->buildViolation("errors.workflow_template_places_undefined")->setParameter('%string%', "")->addViolation();
  70. }
  71. $transitions = array();
  72. if(isset($yaml['transitions'])) {
  73. foreach($yaml['transitions'] as $k => $data) {
  74. if(isset($data['from']) && isset($data['to'])) {
  75. $transitions[] = new Transition($k,$data['from'],$data['to']);
  76. } else {
  77. $this->context->buildViolation("errors.workflow_template_transitions_format")->setParameter('%transition%', $k)->addViolation();
  78. }
  79. }
  80. } else {
  81. $this->context->buildViolation("errors.workflow_template_transitions_undefined")->setParameter('%string%', "")->addViolation();
  82. }
  83. //Validate Definition
  84. if($type == "workflow") {
  85. $mt = ($markingType == "single_state");
  86. $validator = new WorkflowValidator($mt);
  87. } else {
  88. $validator = new StateMachineValidator();
  89. }
  90. $initialPlace = null;
  91. if(isset($yaml['initial_place'])) {
  92. $initialPlace = $yaml['initial_place'];
  93. }
  94. try {
  95. $definition = new Definition($places,$transitions,$initialPlace);
  96. $validator->validate($definition, $name);
  97. } catch (\Exception $e) {
  98. $this->context->buildViolation("Exception: ".$e->getMessage())->setParameter('%string%', "")->addViolation();
  99. return;
  100. } catch (\LogicException $e) {
  101. $this->context->buildViolation("Exception: ".$e->getMessage())->setParameter('%string%', "")->addViolation();
  102. return;
  103. }
  104. }
  105. }