Skip to content

Commit

Permalink
feature #37428 [Workflow] Added Function (and Twig extension) to retr…
Browse files Browse the repository at this point in the history
…ieve 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
  • Loading branch information
fabpot committed Jul 1, 2020
2 parents d8082fa + 0eebe74 commit 5b6139f
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/Symfony/Bridge/Twig/CHANGELOG.md
@@ -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
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
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
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
@@ -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
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
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

0 comments on commit 5b6139f

Please sign in to comment.