8000 feature #37428 [Workflow] Added Function (and Twig extension) to retr… · symfony/symfony@5b6139f · GitHub
[go: up one dir, main page]

Skip to content

Commit 5b6139f

Browse files
committed
feature #37428 [Workflow] Added Function (and Twig extension) to retrieve a specific transition (Carlos Pereira De Amorim)
This PR was squashed before being merged into the 5.2-dev branch. Discussion ---------- [Workflow] 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. | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | hmmmm, should I create a ticket first ? | License | MIT | Doc PR | symfony/symfony-docs#13907 I needed to get the metadata of a transition, but in order to do that, you need the transition object. So here is a PR to easily get the transition object Commits ------- 0eebe74 [Workflow] Added Function (and Twig extension) to retrieve a specific transition
2 parents d8082fa + 0eebe74 commit 5b6139f

File tree

7 files changed

+64
-4
lines changed

7 files changed

+64
-4
lines changed

src/Symfony/Bridge/Twig/CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.2.0
5+
-----
6+
7+
* Added function `workflow_transition` to easily retrieve a specific transition object
8+
49
5.0.0
510
-----
611

@@ -16,7 +21,7 @@ CHANGELOG
1621

1722
* added a new `TwigErrorRenderer` for `html` format, integrated with the `ErrorHandler` component
1823
* marked all classes extending twig as `@final`
19-
* deprecated to pass `$rootDir` and `$fileLinkFormatter` as 5th and 6th argument respectively to the
24+
* deprecated to pass `$rootDir` and `$fileLinkFormatter` as 5th and 6th argument respectively to the
2025
`DebugCommand::__construct()` method, swap the variables position.
2126
* the `LintCommand` lints all the templates stored in all configured Twig paths if none argument is provided
2227
* deprecated accepting STDIN implicitly when using the `lint:twig` command, use `lint:twig -` (append a dash) instead to make it explicit.
@@ -29,7 +34,7 @@ CHANGELOG
2934

3035
* added the `form_parent()` function that allows to reliably retrieve the parent form in Twig templates
3136
* added the `workflow_transition_blockers()` function
32-
* deprecated the `$requestStack` and `$requestContext` arguments of the
37+
* deprecated the `$requestStack` and `$requestContext` arguments of the
3338
`HttpFoundationExtension`, pass a `Symfony\Component\HttpFoundation\UrlHelper`
3439
instance as the only argument instead
3540

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/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.2.0
5+
-----
6+
7+
* Added function `getEnabledTransition` to easily retrieve a specific transition object
8+
49
5.1.0
510
-----
611

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,21 @@ 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+
606+
$transition = $workflow->getEnabledTransition($subject, 'does_not_exist');
607+
$this->assertNull($transition);
608+
}
609+
595610
public function testGetEnabledTransitionsWithSameNameTransition()
596611
{
597612
$definition = $this->createWorkflowWithSameNameTransition();

src/Symfony/Component/Workflow/Workflow.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,25 @@ 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+
continue;
249+
}
250+
251+
return $transition;
252+
}
253+
254+
return null;
255+
}
256+
238257
/**
239258
* {@inheritdoc}
240259
*/

0 commit comments

Comments
 (0)
0