Skip to content

[Workflow] Added Function (and Twig extension) to retrieve a specific transition #37428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Symfony/Bridge/Twig/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.2.0
-----

* Added function `workflow_transition` to easily retrieve a specific transition object

5.0.0
-----

Expand All @@ -16,7 +21,7 @@ CHANGELOG

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

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

Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Bridge/Twig/Extension/WorkflowExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function getFunctions(): array
return [
new TwigFunction('workflow_can', [$this, 'canTransition']),
new TwigFunction('workflow_transitions', [$this, 'getEnabledTransitions']),
new TwigFunction('workflow_transition', [$this, 'getEnabledTransition']),
new TwigFunction('workflow_has_marked_place', [$this, 'hasMarkedPlace']),
new TwigFunction('workflow_marked_places', [$this, 'getMarkedPlaces']),
new TwigFunction('workflow_metadata', [$this, 'getMetadata']),
Expand All @@ -64,6 +65,11 @@ public function getEnabledTransitions(object $subject, string $name = null): arr
return $this->workflowRegistry->get($subject, $name)->getEnabledTransitions($subject);
}

public function getEnabledTransition(object $subject, string $transition, string $name = null): ?Transition
{
return $this->workflowRegistry->get($subject, $name)->getEnabledTransition($subject, $transition);
}

/**
* Returns true if the place is marked.
*/
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Bridge/Twig/Tests/Extension/WorkflowExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ public function testGetEnabledTransitions()
$this->assertSame('t1', $transitions[0]->getName());
}

public function testGetEnabledTransition()
{
$subject = new Subject();

$transition = $this->extension->getEnabledTransition($subject, 't1');

$this->assertInstanceOf(Transition::class, $transition);
$this->assertSame('t1', $transition->getName());
}

public function testHasMarkedPlace()
{
$subject = new Subject(['ordered' => 1, 'waiting_for_payment' => 1]);
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bridge/Twig/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"symfony/console": "^4.4|^5.0",
"symfony/expression-language": "^4.4|^5.0",
"symfony/web-link": "^4.4|^5.0",
"symfony/workflow": "^4.4|^5.0",
"symfony/workflow": "^5.2",
"twig/cssinliner-extra": "^2.12",
"twig/inky-extra": "^2.12",
"twig/markdown-extra": "^2.12"
Expand All @@ -53,7 +53,7 @@
"symfony/http-foundation": "<4.4",
"symfony/http-kernel": "<4.4",
"symfony/translation": "<5.0",
"symfony/workflow": "<4.4"
"symfony/workflow": "<5.2"
},
"suggest": {
"symfony/finder": "",
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Workflow/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.2.0
-----

* Added function `getEnabledTransition` to easily retrieve a specific transition object

5.1.0
-----

Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/Workflow/Tests/WorkflowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,21 @@ public function testGetEnabledTransitions()
$this->assertSame('t5', $transitions[0]->getName());
}

public function testGetEnabledTransition()
{
$definition = $this->createComplexWorkflowDefinition();
$subject = new Subject();
$workflow = new Workflow($definition, new MethodMarkingStore());

$subject->setMarking(['d' => 1]);
$transition = $workflow->getEnabledTransition($subject, 't3');
$this->assertInstanceOf(Transition::class, $transition);
$this->assertSame('t3', $transition->getName());

$transition = $workflow->getEnabledTransition($subject, 'does_not_exist');
$this->assertNull($transition);
}

public function testGetEnabledTransitionsWithSameNameTransition()
{
$definition = $this->createWorkflowWithSameNameTransition();
Expand Down
19 changes: 19 additions & 0 deletions src/Symfony/Component/Workflow/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,25 @@ public function getEnabledTransitions(object $subject)
return $enabledTransitions;
}

public function getEnabledTransition(object $subject, string $name): ?Transition
{
$marking = $this->getMarking($subject);

foreach ($this->definition->getTransitions() as $transition) {
if ($transition->getName() !== $name) {
continue;
}
$transitionBlockerList = $this->buildTransitionBlockerListForTransition($subject, $marking, $transition);
if (!$transitionBlockerList->isEmpty()) {
continue;
}

return $transition;
}

return null;
}

/**
* {@inheritdoc}
*/
Expand Down