123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Bridge\Twig\Extension;
- use Symfony\Component\Workflow\Registry;
- use Symfony\Component\Workflow\Transition;
- use Twig\Extension\AbstractExtension;
- use Twig\TwigFunction;
- /**
- * WorkflowExtension.
- *
- * @author Grégoire Pineau <lyrixx@lyrixx.info>
- */
- class WorkflowExtension extends AbstractExtension
- {
- private $workflowRegistry;
- public function __construct(Registry $workflowRegistry)
- {
- $this->workflowRegistry = $workflowRegistry;
- }
- public function getFunctions()
- {
- return array(
- new TwigFunction('workflow_can', array($this, 'canTransition')),
- new TwigFunction('workflow_transitions', array($this, 'getEnabledTransitions')),
- new TwigFunction('workflow_has_marked_place', array($this, 'hasMarkedPlace')),
- new TwigFunction('workflow_marked_places', array($this, 'getMarkedPlaces')),
- );
- }
- /**
- * Returns true if the transition is enabled.
- *
- * @param object $subject A subject
- * @param string $transitionName A transition
- * @param string $name A workflow name
- *
- * @return bool true if the transition is enabled
- */
- public function canTransition($subject, $transitionName, $name = null)
- {
- return $this->workflowRegistry->get($subject, $name)->can($subject, $transitionName);
- }
- /**
- * Returns all enabled transitions.
- *
- * @param object $subject A subject
- * @param string $name A workflow name
- *
- * @return Transition[] All enabled transitions
- */
- public function getEnabledTransitions($subject, $name = null)
- {
- return $this->workflowRegistry->get($subject, $name)->getEnabledTransitions($subject);
- }
- /**
- * Returns true if the place is marked.
- *
- * @param object $subject A subject
- * @param string $placeName A place name
- * @param string $name A workflow name
- *
- * @return bool true if the transition is enabled
- */
- public function hasMarkedPlace($subject, $placeName, $name = null)
- {
- return $this->workflowRegistry->get($subject, $name)->getMarking($subject)->has($placeName);
- }
- /**
- * Returns marked places.
- *
- * @param object $subject A subject
- * @param string $placesNameOnly If true, returns only places name. If false returns the raw representation
- * @param string $name A workflow name
- *
- * @return string[]|int[]
- */
- public function getMarkedPlaces($subject, $placesNameOnly = true, $name = null)
- {
- $places = $this->workflowRegistry->get($subject, $name)->getMarking($subject)->getPlaces();
- if ($placesNameOnly) {
- return array_keys($places);
- }
- return $places;
- }
- public function getName()
- {
- return 'workflow';
- }
- }
|