Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1f86a53

Browse filesBrowse 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 1f86a53
Copy full SHA for 1f86a53

File tree

Expand file treeCollapse file tree

5 files changed

+61
-2
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+61
-2
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Extension/WorkflowExtension.php
+6Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Tests/Extension/WorkflowExtensionTest.php
+10Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/composer.json
+2-2Lines 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/WorkflowTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Workflow/Tests/WorkflowTest.php
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,32 @@ 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+
$eventDispatcher = new EventDispatcher();
600+
$eventDispatcher->addListener('workflow.workflow_name.guard.t1', function (GuardEvent $event) {
601+
$event->setBlocked(true);
602+
});
603+
$workflow = new Workflow($definition, new MethodMarkingStore(), $eventDispatcher, 'workflow_name');
604+
605+
$this->assertEmpty($workflow->getEnabledTransitions($subject));
606+
607+
$subject->setMarking(['d' => 1]);
608+
$transition = $workflow->getEnabledTransition($subject, 't3');
609+
$this->assertInstanceOf(Transition::class, $transition);
610+
$this->assertSame('t3', $transition->getName());
611+
612+
$subject->setMarking(['c' => 1, 'e' => 1]);
613+
$transition = $workflow->getEnabledTransition($subject, 't5');
614+
$this->assertInstanceOf(Transition::class, $transition);
615+
$this->assertSame('t5', $transition->getName());
616+
617+
$transition = $workflow->getEnabledTransition($subject, 'does_not_exist');
618+
$this->assertNull($transition);
619+
}
620+
595621
public function testGetEnabledTransitionsWithSameNameTransition()
596622
{
597623
$definition = $this->createWorkflowWithSameNameTransition();

‎src/Symfony/Component/Workflow/Workflow.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Workflow/Workflow.php
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,23 @@ public function getEnabledTransitions(object $subject)
235235
return $enabledTransitions;
236236
}
237237

238+
public function getEnabledTransition(object $subject, string $name): ?Transition
239+
{
240+
$marking = $this->getMarking($subject);
241+
242+
foreach ($this->definition->getTransitions() as $transition) {
243+
if ($transition->getName() !== $name) {
244+
continue;
245+
}
246+
$transitionBlockerList = $this->buildTransitionBlockerListForTransition($subject, $marking, $transition);
247+
if ($transitionBlockerList->isEmpty()) {
248+
return $transition;
249+
}
250+
}
251+
252+
return null;
253+
}
254+
238255
/**
239256
* {@inheritdoc}
240257
*/

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.