[Workflow] Added Function (and Twig extension) to retrieve a specific transition
This commit is contained in:
parent
80d1f44983
commit
0eebe74259
@ -1,6 +1,11 @@
|
||||
CHANGELOG
|
||||
=========
|
||||
|
||||
5.2.0
|
||||
-----
|
||||
|
||||
* Added function `workflow_transition` to easily retrieve a specific transition object
|
||||
|
||||
5.0.0
|
||||
-----
|
||||
|
||||
@ -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.
|
||||
@ -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
|
||||
|
||||
|
@ -39,6 +39,7 @@ final class WorkflowExtension extends AbstractExtension
|
||||
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']),
|
||||
@ -64,6 +65,11 @@ final class WorkflowExtension extends AbstractExtension
|
||||
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.
|
||||
*/
|
||||
|
@ -81,6 +81,16 @@ class WorkflowExtensionTest extends TestCase
|
||||
$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]);
|
||||
|
@ -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"
|
||||
@ -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": "",
|
||||
|
@ -1,6 +1,11 @@
|
||||
CHANGELOG
|
||||
=========
|
||||
|
||||
5.2.0
|
||||
-----
|
||||
|
||||
* Added function `getEnabledTransition` to easily retrieve a specific transition object
|
||||
|
||||
5.1.0
|
||||
-----
|
||||
|
||||
|
@ -592,6 +592,21 @@ class WorkflowTest extends TestCase
|
||||
$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();
|
||||
|
@ -235,6 +235,25 @@ class Workflow implements WorkflowInterface
|
||||
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}
|
||||
*/
|
||||
|
Reference in New Issue
Block a user