WorkflowExtension.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Twig\Extension;
  11. use Symfony\Component\Workflow\Registry;
  12. use Symfony\Component\Workflow\Transition;
  13. use Twig\Extension\AbstractExtension;
  14. use Twig\TwigFunction;
  15. /**
  16. * WorkflowExtension.
  17. *
  18. * @author Grégoire Pineau <lyrixx@lyrixx.info>
  19. */
  20. class WorkflowExtension extends AbstractExtension
  21. {
  22. private $workflowRegistry;
  23. public function __construct(Registry $workflowRegistry)
  24. {
  25. $this->workflowRegistry = $workflowRegistry;
  26. }
  27. public function getFunctions()
  28. {
  29. return array(
  30. new TwigFunction('workflow_can', array($this, 'canTransition')),
  31. new TwigFunction('workflow_transitions', array($this, 'getEnabledTransitions')),
  32. new TwigFunction('workflow_has_marked_place', array($this, 'hasMarkedPlace')),
  33. new TwigFunction('workflow_marked_places', array($this, 'getMarkedPlaces')),
  34. );
  35. }
  36. /**
  37. * Returns true if the transition is enabled.
  38. *
  39. * @param object $subject A subject
  40. * @param string $transitionName A transition
  41. * @param string $name A workflow name
  42. *
  43. * @return bool true if the transition is enabled
  44. */
  45. public function canTransition($subject, $transitionName, $name = null)
  46. {
  47. return $this->workflowRegistry->get($subject, $name)->can($subject, $transitionName);
  48. }
  49. /**
  50. * Returns all enabled transitions.
  51. *
  52. * @param object $subject A subject
  53. * @param string $name A workflow name
  54. *
  55. * @return Transition[] All enabled transitions
  56. */
  57. public function getEnabledTransitions($subject, $name = null)
  58. {
  59. return $this->workflowRegistry->get($subject, $name)->getEnabledTransitions($subject);
  60. }
  61. /**
  62. * Returns true if the place is marked.
  63. *
  64. * @param object $subject A subject
  65. * @param string $placeName A place name
  66. * @param string $name A workflow name
  67. *
  68. * @return bool true if the transition is enabled
  69. */
  70. public function hasMarkedPlace($subject, $placeName, $name = null)
  71. {
  72. return $this->workflowRegistry->get($subject, $name)->getMarking($subject)->has($placeName);
  73. }
  74. /**
  75. * Returns marked places.
  76. *
  77. * @param object $subject A subject
  78. * @param string $placesNameOnly If true, returns only places name. If false returns the raw representation
  79. * @param string $name A workflow name
  80. *
  81. * @return string[]|int[]
  82. */
  83. public function getMarkedPlaces($subject, $placesNameOnly = true, $name = null)
  84. {
  85. $places = $this->workflowRegistry->get($subject, $name)->getMarking($subject)->getPlaces();
  86. if ($placesNameOnly) {
  87. return array_keys($places);
  88. }
  89. return $places;
  90. }
  91. public function getName()
  92. {
  93. return 'workflow';
  94. }
  95. }