Skip to content

Commit 4001b15

Browse files
committed
Added Function (and Twig extension) to retrieve a specific transition
You can now easily retrieve a specific transition. Useful when you want the metadata of a specific transition.
1 parent 80d1f44 commit 4001b15

File tree

6 files changed

+102
-10
lines changed

6 files changed

+102
-10
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function getFunctions(): array
3939
return [
4040
new TwigFunction('workflow_can', [$this, 'canTransition']),
4141
new TwigFunction('workflow_transitions', [$this, 'getEnabledTransitions']),
42+
new TwigFunction('workflow_transition', [$this, 'getEnabledTransition']),
4243
new TwigFunction('workflow_has_marked_place', [$this, 'hasMarkedPlace']),
4344
new TwigFunction('workflow_marked_places', [$this, 'getMarkedPlaces']),
4445
new TwigFunction('workflow_metadata', [$this, 'getMetadata']),
@@ -64,6 +65,11 @@ public function getEnabledTransitions(object $subject, string $name = null): arr
6465
return $this->workflowRegistry->get($subject, $name)->getEnabledTransitions($subject);
6566
}
6667

68+
public function getEnabledTransition(object $subject, string $transition, string $name = null): ?Transition
69+
{
70+
return $this->workflowRegistry->get($subject, $name)->getEnabledTransition($subject, $transition);
71+
}
72+
6773
/**
6874
* Returns true if the place is marked.
6975
*/

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ public function testGetEnabledTransitions()
8181
$this->assertSame('t1', $transitions[0]->getName());
8282
}
8383

84+
public function testGetEnabledTransition()
85+
{
86+
$subject = new Subject();
87+
88+
$transition = $this->extension->getEnabledTransition($subject, 't1');
89+
90+
$this->assertInstanceOf(Transition::class, $transition);
91+
$this->assertSame('t1', $transition->getName());
92+
}
93+
8494
public function testHasMarkedPlace()
8595
{
8696
$subject = new Subject(['ordered' => 1, 'waiting_for_payment' => 1]);

src/Symfony/Bridge/Twig/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"symfony/console": "^4.4|^5.0",
4343
"symfony/expression-language": "^4.4|^5.0",
4444
"symfony/web-link": "^4.4|^5.0",
45-
"symfony/workflow": "^4.4|^5.0",
45+
"symfony/workflow": "^5.2",
4646
"twig/cssinliner-extra": "^2.12",
4747
"twig/inky-extra": "^2.12",
4848
"twig/markdown-extra": "^2.12"
@@ -53,7 +53,7 @@
5353
"symfony/http-foundation": "<4.4",
5454
"symfony/http-kernel": "<4.4",
5555
"symfony/translation": "<5.0",
56-
"symfony/workflow": "<4.4"
56+
"symfony/workflow": "<5.2"
5757
},
5858
"suggest": {
5959
"symfony/finder": "",

src/Symfony/Component/Workflow/Tests/WorkflowBuilderTrait.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ private function createComplexWorkflowDefinition()
1919
$transitions[] = $transitionWithMetadataDumpStyle;
2020
$transitions[] = new Transition('t4', 'd', 'f');
2121
$transitions[] = new Transition('t5', 'e', 'g');
22+
$transitions[] = new Transition('t3', 'f', 'e');
2223
$transitions[] = new Transition('t6', 'f', 'g');
2324

2425
$transitionsMetadata = new \SplObjectStorage();
@@ -32,14 +33,14 @@ private function createComplexWorkflowDefinition()
3233
return new Definition($places, $transitions, null, $inMemoryMetadataStore);
3334

3435
// The graph looks like:
35-
// +---+ +----+ +---+ +----+ +----+ +----+ +----+ +----+ +---+
36-
// | a | --> | t1 | --> | c | --> | t2 | --> | d | --> | t4 | --> | f | --> | t6 | --> | g |
37-
// +---+ +----+ +---+ +----+ +----+ +----+ +----+ +----+ +---+
38-
// | ^ | ^
39-
// | | | |
40-
// v | v |
41-
// +----+ | +----+ +----+ +----+ |
42-
// | b | ----------------+ | t3 | --> | e | --> | t5 | -----------------+
36+
// +---+ +----+ +---+ +----+ +----+ +----+ +----+ +----+ +---+
37+
// | a | --> | t1 | --> | c | --> | t2 | --> | d | --> | t4 | -------------> | f | --> | t6 | --> | g |
38+
// +---+ +----+ +---+ +----+ +----+ +----+ +----+ +----+ +---+
39+
// | ^ | +----+ | ^
40+
// | | | + ----- | t3 | -------+ |
41+
// v | v v +----+ |
42+
// +----+ | +----+ +----+ +----+ |
43+
// | b | ----------------+ | t3 | --> | e | --> | t5 | ----------------------------+
4344
// +----+ +----+ +----+ +----+
4445
}
4546

src/Symfony/Component/Workflow/Tests/WorkflowTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,54 @@ public function testGetEnabledTransitions()
592592
$this->assertSame('t5', $transitions[0]->getName());
593593
}
594594

595+
public function testGetEnabledTransition()
596+
{
597+
$definition = $this->createComplexWorkflowDefinition();
598+
$subject = new Subject();
599+
$workflow = new Workflow($definition, new MethodMarkingStore());
600+
601+
$subject->setMarking(['d' => 1]);
602+
$transition = $workflow->getEnabledTransition($subject, 't3');
603+
$this->assertInstanceOf(Transition::class, $transition);
604+
$this->assertSame('t3', $transition->getName());
605+
$transition = $workflow->getEnabledTransition($subject, 't3', ['d']);
606+
$this->assertInstanceOf(Transition::class, $transition);
607+
$this->assertSame('t3', $transition->getName());
608+
$this->assertSame(['d'], $transition->getFroms());
609+
$transition = $workflow->getEnabledTransition($subject, 't3', ['d'], ['e']);
610+
$this->assertInstanceOf(Transition::class, $transition);
611+
$this->assertSame('t3', $transition->getName());
612+
$this->assertSame(['d'], $transition->getFroms());
613+
$this->assertSame(['e'], $transition->getTos());
614+
$transition = $workflow->getEnabledTransition($subject, 't3', null, ['e']);
615+
$this->assertInstanceOf(Transition::class, $transition);
616+
$this->assertSame('t3', $transition->getName());
617+
$this->assertSame(['e'], $transition->getTos());
618+
$transition = $workflow->getEnabledTransition($subject, 't3', ['a']);
619+
$this->assertNull($transition);
620+
$transition = $workflow->getEnabledTransition($subject, 't3', null, ['b']);
621+
$this->assertNull($transition);
622+
623+
$subject->setMarking(['f' => 1]);
624+
$transition = $workflow->getEnabledTransition($subject, 't3');
625+
$this->assertInstanceOf(Transition::class, $transition);
626+
$this->assertSame('t3', $transition->getName());
627+
$transition = $workflow->getEnabledTransition($subject, 't3', ['d']);
628+
$this->assertNull($transition);
629+
$transition = $workflow->getEnabledTransition($subject, 't3', ['f']);
630+
$this->assertInstanceOf(Transition::class, $transition);
631+
$this->assertSame('t3', $transition->getName());
632+
$this->assertSame(['f'], $transition->getFroms());
633+
634+
$subject->setMarking(['c' => 1, 'e' => 1]);
635+
$transition = $workflow->getEnabledTransition($subject, 't5');
636+
$this->assertInstanceOf(Transition::class, $transition);
637+
$this->assertSame('t5', $transition->getName());
638+
639+
$transition = $workflow->getEnabledTransition($subject, 'does_not_exist');
640+
$this->assertNull($transition);
641+
}
642+
595643
public function testGetEnabledTransitionsWithSameNameTransition()
596644
{
597645
$definition = $this->createWorkflowWithSameNameTransition();

src/Symfony/Component/Workflow/Workflow.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,33 @@ public function getEnabledTransitions(object $subject)
235235
return $enabledTransitions;
236236
}
237237

238+
public function getEnabledTransition(object $subject, string $name, array $froms = null, array $tos = null): ?Transition
239+
{
240+
$marking = $this->getMarking($subject);
241+
242+
$result = array_filter(
243+
$this->definition->getTransitions(),
244+
function ($transition) use ($subject, $marking, $name, $froms, $tos) {
245+
if ($transition->getName() !== $name) {
246+
return false;
247+
}
248+
$transitionBlockerList = $this->buildTransitionBlockerListForTransition($subject, $marking, $transition);
249+
if (!$transitionBlockerList->isEmpty()) {
250+
return false;
251+
}
252+
if (\is_array($froms) && empty(array_intersect($transition->getFroms(), $froms))) {
253+
return false;
254+
}
255+
if (\is_array($tos) && empty(array_intersect($transition->getTos(), $tos))) {
256+
return false;
257+
}
258+
259+
return true;
260+
});
261+
262+
return array_pop($result);
263+
}
264+
238265
/**
239266
* {@inheritdoc}
240267
*/

0 commit comments

Comments
 (0)