8000 [Workflow] Added 'workflow_places' twig function · symfony/symfony@4a2335a · GitHub
[go: up one dir, main page]

Skip to content

Commit 4a2335a

Browse files
committed
[Workflow] Added 'workflow_places' twig function
When someone uses a custom MarkingStore, the value in the $subject::marking can be really different from the value inside Marking::getPlaces(). This occurs, for example, when the value stored in the subject is a bit mask. So it's always safer to get the places names from the marking, and so with this new function.
1 parent 1b63474 commit 4a2335a

File tree

3 files changed

+64
-6
lines changed

3 files changed

+64
-6
lines changed

src/Symfony/Bridge/Twig/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* added a `workflow_has_marked_place` function
8+
* added a `workflow_places` function
89

910
3.2.0
1011
-----

src/Symfony/Bridge/Twig/Extension/WorkflowExtension.php

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,69 @@ public function getFunctions()
3333
new \Twig_SimpleFunction('workflow_can', array($this, 'canTransition')),
3434
new \Twig_SimpleFunction('workflow_transitions', array($this, 'getEnabledTransitions')),
3535
new \Twig_SimpleFunction('workflow_has_marked_place', array($this, 'hasMarkedPlace')),
36+
new \Twig_SimpleFunction('workflow_places', array($this, 'getMarkedPlaces')),
3637
);
3738
}
3839

39-
public function canTransition($object, $transition, $name = null)
40+
/**
41+
* Returns true if the transition is enabled.
42+
*
43+
* @param object $subject A subject
44+
* @param string $transitionName A transition
45+
* @param string $name A workflow name
46+
*
47+
* @return bool true if the transition is enabled
48+
*/
49+
public function canTransition($subject, $transition, $name = null)
4050
{
41-
return $this->workflowRegistry->get($object, $name)->can($object, $transition);
51+
return $this->workflowRegistry->get($subject, $name)->can($subject, $transition);
4252
}
4353

44-
public function getEnabledTransitions($object, $name = null)
54+
/**
55+
* Returns all enabled transitions.
56+
*
57+
* @param object $subject A subject
58+
* @param string $name A workflow name
59+
*
60+
* @return Transition[] All enabled transitions
61+
*/
62+
public function getEnabledTransitions($subject, $name = null)
4563
{
46-
return $this->workflowRegistry->get($object, $name)->getEnabledTransitions($object);
64+
return $this->workflowRegistry->get($subject, $name)->getEnabledTransitions($subject);
4765
}
4866

49-
public function hasMarkedPlace($object, $place, $name = null)
67+
/**
68+
* Returns true if the place is marked.
69+
*
70+
* @param object $subject A subject
71+
* @param string $transitionName A transition
72+
* @param string $name A workflow name
73+
*
74+
* @return bool true if the transition is enabled
75+
*/
76+
public function hasMarkedPlace($subject, $place, $name = null)
5077
{
51-
return $this->workflowRegistry->get($object, $name)->getMarking($object)->has($place);
78+
return $this->workflowRegistry->get($subject, $name)->getMarking($subject)->has($place);
79+
}
80+
81+
/**
82+
* Returns all places.
83+
*
84+
* @param object $subject A subject
85+
* @param string $placesNameOnly If true, returns only places name. If false returns the raw representation
86+
* @param string $name A workflow name
87+
*
88+
* @return string[]|int[]
89+
*/
90+
public function getMarkedPlaces($subject, $placesNameOnly = true, $name = null)
91+
{
92+
$places = $this->workflowRegistry->get($subject, $name)->getMarking($subject)->getPlaces();
93+
94+
if ($placesNameOnly) {
95+
return array_keys($places);
96+
}
97+
98+
return $places;
5299
}
53100

54101
public function getName()

src/Symfony/Bridge/Twig/Tests/Extension/WorkflowExtensionTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,14 @@ public function testHasMarkedPlace()
7575
$this->assertTrue($this->extension->hasMarkedPlace($subject, 'waiting_for_payment'));
7676
$this->assertFalse($this->extension->hasMarkedPlace($subject, 'processed'));
7777
}
78+
79+
public function testGetMarkedPlaces()
80+
{
81+
$subject = new \stdClass();
82+
$subject->marking = array();
83+
$subject->marking = array('ordered' => 1, 'waiting_for_payment' => 1);
84+
85+
$this->assertSame(array('ordered', 'waiting_for_payment'), $this->extension->getMarkedPlaces($subject));
86+
$this->assertSame($subject->marking, $this->extension->getMarkedPlaces($subject, false));
87+
}
7888
}

0 commit comments

Comments
 (0)
0