WorkflowExtension.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 Symfony\Component\Workflow\TransitionBlockerList;
  14. use Twig\Extension\AbstractExtension;
  15. use Twig\TwigFunction;
  16. /**
  17. * WorkflowExtension.
  18. *
  19. * @author Grégoire Pineau <lyrixx@lyrixx.info>
  20. * @author Carlos Pereira De Amorim <carlos@shauri.fr>
  21. */
  22. final class WorkflowExtension extends AbstractExtension
  23. {
  24. private $workflowRegistry;
  25. public function __construct(Registry $workflowRegistry)
  26. {
  27. $this->workflowRegistry = $workflowRegistry;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function getFunctions(): array
  33. {
  34. return [
  35. new TwigFunction('workflow_can', [$this, 'canTransition']),
  36. new TwigFunction('workflow_transitions', [$this, 'getEnabledTransitions']),
  37. new TwigFunction('workflow_transition', [$this, 'getEnabledTransition']),
  38. new TwigFunction('workflow_has_marked_place', [$this, 'hasMarkedPlace']),
  39. new TwigFunction('workflow_marked_places', [$this, 'getMarkedPlaces']),
  40. new TwigFunction('workflow_metadata', [$this, 'getMetadata']),
  41. new TwigFunction('workflow_transition_blockers', [$this, 'buildTransitionBlockerList']),
  42. ];
  43. }
  44. /**
  45. * Returns true if the transition is enabled.
  46. */
  47. public function canTransition(object $subject, string $transitionName, string $name = null): bool
  48. {
  49. return $this->workflowRegistry->get($subject, $name)->can($subject, $transitionName);
  50. }
  51. /**
  52. * Returns all enabled transitions.
  53. *
  54. * @return Transition[] All enabled transitions
  55. */
  56. public function getEnabledTransitions(object $subject, string $name = null): array
  57. {
  58. return $this->workflowRegistry->get($subject, $name)->getEnabledTransitions($subject);
  59. }
  60. public function getEnabledTransition(object $subject, string $transition, string $name = null): ?Transition
  61. {
  62. return $this->workflowRegistry->get($subject, $name)->getEnabledTransition($subject, $transition);
  63. }
  64. /**
  65. * Returns true if the place is marked.
  66. */
  67. public function hasMarkedPlace(object $subject, string $placeName, string $name = null): bool
  68. {
  69. return $this->workflowRegistry->get($subject, $name)->getMarking($subject)->has($placeName);
  70. }
  71. /**
  72. * Returns marked places.
  73. *
  74. * @return string[]|int[]
  75. */
  76. public function getMarkedPlaces(object $subject, bool $placesNameOnly = true, string $name = null): array
  77. {
  78. $places = $this->workflowRegistry->get($subject, $name)->getMarking($subject)->getPlaces();
  79. if ($placesNameOnly) {
  80. return array_keys($places);
  81. }
  82. return $places;
  83. }
  84. /**
  85. * Returns the metadata for a specific subject.
  86. *
  87. * @param string|Transition|null $metadataSubject Use null to get workflow metadata
  88. * Use a string (the place name) to get place metadata
  89. * Use a Transition instance to get transition metadata
  90. */
  91. public function getMetadata(object $subject, string $key, $metadataSubject = null, string $name = null)
  92. {
  93. return $this
  94. ->workflowRegistry
  95. ->get($subject, $name)
  96. ->getMetadataStore()
  97. ->getMetadata($key, $metadataSubject)
  98. ;
  99. }
  100. public function buildTransitionBlockerList(object $subject, string $transitionName, string $name = null): TransitionBlockerList
  101. {
  102. $workflow = $this->workflowRegistry->get($subject, $name);
  103. return $workflow->buildTransitionBlockerList($subject, $transitionName);
  104. }
  105. }