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

Skip to content

Commit 5955744

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 5955744

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
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_marked_places` function
89

910
3.2.0
1011
-----

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ 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_marked_places', array($this, 'getMarkedPlaces')),
3637
);
3738
}
3839

@@ -46,9 +47,38 @@ public function getEnabledTransitions($object, $name = null)
4647
return $this->workflowRegistry->get($object, $name)->getEnabledTransitions($object);
4748
}
4849

49-
public function hasMarkedPlace($object, $place, $name = null)
50+
/**
51+
* Returns true if the place is marked.
52+
*
53+
* @param object $subject A subject
54+
* @param string $transitionName A transition
55+
* @param string $name A workflow name
56+
*
57+
* @return bool true if the transition is enabled
58+
*/
59+
public function hasMarkedPlace($subject, $place, $name = null)
5060
{
51-
return $this->workflowRegistry->get($object, $name)->getMarking($object)->has($place);
61+
return $this->workflowRegistry->get($subject, $name)->getMarking($subject)->has($place);
62+
}
63+
64+
/**
65+
* Returns all places.
66+
*
67+
* @param object $subject A subject
68+
* @param string $placesNameOnly If true, returns only places name. If false returns the raw representation
69+
* @param string $name A workflow name
70+
*
71+
* @return string[]|int[]
72+
*/
73+
public function getMarkedPlaces($subject, $placesNameOnly = true, $name = null)
74+
{
75+
$places = $this->workflowRegistry->get($subject, $name)->getMarking($subject)->getPlaces();
76+
77+
if ($placesNameOnly) {
78+
return array_keys($places);
79+
}
80+
81+
return $places;
5282
}
5383

5484
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