Skip to content

Commit 0647c5a

Browse files
committed
bug #29137 [Workflow][FrameworkBundle] fixed guard event names for transitions (destillat, lyrixx)
This PR was merged into the 3.4 branch. Discussion ---------- [Workflow][FrameworkBundle] fixed guard event names for transitions | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #28018 symfony/symfony#28007 (comment) | License | MIT | Doc PR | There is a bug when many transitions are defined with the same name. I finished destillat's work and rebase against 3.4 as it's a bug fix. There another point of failure, but it could not be fixed on 3.4. I will be a need feature. The issue is related to `Workflow::can($subject, $transitionName)`. Since the transitionName could be not unique, we will need to support passing an instance of Transition. A new PR is incomming Commits ------- 83dc473dd6 [FrameworkBundle] fixed guard event names for transitions fb88bfc79a [FrameworkBundle] fixed guard event names for transitions
2 parents 1bc6dd0 + 60b7fcb commit 0647c5a

File tree

6 files changed

+252
-27
lines changed

6 files changed

+252
-27
lines changed

DependencyInjection/FrameworkExtension.php

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -602,15 +602,44 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
602602
@trigger_error(sprintf('The "type" option of the "framework.workflows.%s" configuration entry must be defined since Symfony 3.3. The default value will be "state_machine" in Symfony 4.0.', $name), E_USER_DEPRECATED);
603603
}
604604
$type = $workflow['type'];
605+
$workflowId = sprintf('%s.%s', $type, $name);
605606

607+
// Create transitions
606608
$transitions = array();
609+
$guardsConfiguration = array();
610+
// Global transition counter per workflow
611+
$transitionCounter = 0;
607612
foreach ($workflow['transitions'] as $transition) {
608613
if ('workflow' === $type) {
609-
$transitions[] = new Definition(Workflow\Transition::class, array($transition['name'], $transition['from'], $transition['to']));
614+
$transitionDefinition = new Definition(Workflow\Transition::class, array($transition['name'], $transition['from'], $transition['to']));
615+
$transitionDefinition->setPublic(false);
616+
$transitionId = sprintf('%s.transition.%s', $workflowId, $transitionCounter++);
617+
$container->setDefinition($transitionId, $transitionDefinition);
618+
$transitions[] = new Reference($transitionId);
619+
if (isset($transition['guard'])) {
620+
$configuration = new Definition(Workflow\EventListener\GuardExpression::class);
621+
$configuration->addArgument(new Reference($transitionId));
622+
$configuration->addArgument($transition['guard']);
623+
$configuration->setPublic(false);
624+
$eventName = sprintf('workflow.%s.guard.%s', $name, $transition['name']);
625+
$guardsConfiguration[$eventName][] = $configuration;
626+
}
610627
} elseif ('state_machine' === $type) {
611628
foreach ($transition['from'] as $from) {
612629
foreach ($transition['to'] as $to) {
613-
$transitions[] = new Definition(Workflow\Transition::class, array($transition['name'], $from, $to));
630+
$transitionDefinition = new Definition(Workflow\Transition::class, array($transition['name'], $from, $to));
631+
$transitionDefinition->setPublic(false);
632+
$transitionId = sprintf('%s.transition.%s', $workflowId, $transitionCounter++);
633+
$container->setDefinition($transitionId, $transitionDefinition);
634+
$transitions[] = new Reference($transitionId);
635+
if (isset($transition['guard'])) {
636+
$configuration = new Definition(Workflow\EventListener\GuardExpression::class);
637+
$configuration->addArgument(new Reference($transitionId));
638+
$configuration->addArgument($transition['guard']);
639+
$configuration->setPublic(false);
640+
$eventName = sprintf('workflow.%s.guard.%s', $name, $transition['name']);
641+
$guardsConfiguration[$eventName][] = $configuration;
642+
}
614643
}
615644
}
616645
}
@@ -641,7 +670,6 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
641670
}
642671

643672
// Create Workflow
644-
$workflowId = sprintf('%s.%s', $type, $name);
645673
$workflowDefinition = new ChildDefinition(sprintf('%s.abstract', $type));
646674
$workflowDefinition->replaceArgument(0, new Reference(sprintf('%s.definition', $workflowId)));
647675
if (isset($markingStoreDefinition)) {
@@ -677,16 +705,7 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
677705
}
678706

679707
// Add Guard Listener
680-
$guard = new Definition(Workflow\EventListener\GuardListener::class);
681-
$guard->setPrivate(true);
682-
$configuration = array();
683-
foreach ($workflow['transitions'] as $config) {
684-
$transitionName = $config['name'];
685-
686-
if (!isset($config['guard'])) {
687-
continue;
688-
}
689-
708+
if ($guardsConfiguration) {
690709
if (!class_exists(ExpressionLanguage::class)) {
691710
throw new LogicException('Cannot guard workflows as the ExpressionLanguage component is not installed. Try running "composer require symfony/expression-language".');
692711
}
@@ -695,20 +714,21 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
695714
throw new LogicException('Cannot guard workflows as the Security component is not installed. Try running "composer require symfony/security".');
696715
}
697716

698-
$eventName = sprintf('workflow.%s.guard.%s', $name, $transitionName);
699-
$guard->addTag('kernel.event_listener', array('event' => $eventName, 'method' => 'onTransition'));
700-
$configuration[$eventName] = $config['guard'];
701-
}
702-
if ($configuration) {
717+
$guard = new Definition(Workflow\EventListener\GuardListener::class);
718+
$guard->setPrivate(true);
719+
703720
$guard->setArguments(array(
704-
$configuration,
721+
$guardsConfiguration,
705722
new Reference('workflow.security.expression_language'),
706723
new Reference('security.token_storage'),
707724
new Reference('security.authorization_checker'),
708725
new Reference('security.authentication.trust_resolver'),
709726
new Reference('security.role_hierarchy'),
710727
new Reference('validator', ContainerInterface::NULL_ON_INVALID_REFERENCE),
711728
));
729+
foreach ($guardsConfiguration as $eventName => $config) {
730+
$guard->addTag('kernel.event_listener', array('event' => $eventName, 'method' => 'onTransition'));
731+
}
712732

713733
$container->setDefinition(sprintf('%s.listener.guard', $workflowId), $guard);
714734
$container->setParameter('workflow.has_guard_listeners', true);

Resources/config/schema/symfony-1.0.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@
300300
<xsd:sequence>
301301
<xsd:element name="from" type="xsd:string" minOccurs="1" maxOccurs="unbounded" />
302302
<xsd:element name="to" type="xsd:string" minOccurs="1" maxOccurs="unbounded" />
303+
<xsd:element name="guard" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
303304
</xsd:sequence>
304305
<xsd:attribute name="name" type="xsd:string" use="required" />
305306
</xsd:complexType>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
use Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest;
4+
5+
$container->loadFromExtension('framework', array(
6+
'workflows' => array(
7+
'article' => array(
8+
'type' => 'workflow',
9+
'marking_store' => array(
10+
'type' => 'multiple_state',
11+
),
12+
'supports' => array(
13+
FrameworkExtensionTest::class,
14+
),
15+
'initial_place' => 'draft',
16+
'places' => array(
17+
'draft',
18+
'wait_for_journalist',
19+
'approved_by_journalist',
20+
'wait_for_spellchecker',
21+
'approved_by_spellchecker',
22+
'published',
23+
),
24+
'transitions' => array(
25+
'request_review' => array(
26+
'from' => 'draft',
27+
'to' => array('wait_for_journalist', 'wait_for_spellchecker'),
28+
),
29+
'journalist_approval' => array(
30+
'from' => 'wait_for_journalist',
31+
'to' => 'approved_by_journalist',
32+
),
33+
'spellchecker_approval' => array(
34+
'from' => 'wait_for_spellchecker',
35+
'to' => 'approved_by_spellchecker',
36+
),
37+
'publish' => array(
38+
'from' => array('approved_by_journalist', 'approved_by_spellchecker'),
39+
'to' => 'published',
40+
'guard' => '!!true',
41+
),
42+
'publish_editor_in_chief' => array(
43+
'name' => 'publish',
44+
'from' => 'draft',
45+
'to' => 'published',
46+
'guard' => '!!false',
47+
),
48+
),
49+
),
50+
),
51+
));
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:framework="http://symfony.com/schema/dic/symfony"
6+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
7+
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
8+
9+
<framework:config>
10+
<framework:workflow name="article" type="workflow" initial-place="draft">
11+
<framework:marking-store type="multiple_state">
12+
<framework:argument>a</framework:argument>
13+
<framework:argument>a</framework:argument>
14+
</framework:marking-store>
15+
<framework:support>Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest</framework:support>
16+
<framework:place>draft</framework:place>
17+
<framework:place>wait_for_journalist</framework:place>
18+
<framework:place>approved_by_journalist</framework:place>
19+
<framework:place>wait_for_spellchecker</framework:place>
20+
<framework:place>approved_by_spellchecker</framework:place>
21+
<framework:place>published</framework:place>
22+
<framework:transition name="request_review">
23+
<framework:from>draft</framework:from>
24+
<framework:to>wait_for_journalist</framework:to>
25+
<framework:to>wait_for_spellchecker</framework:to>
26+
</framework:transition>
27+
<framework:transition name="journalist_approval">
28+
<framework:from>wait_for_journalist</framework:from>
29+
<framework:to>approved_by_journalist</framework:to>
30+
</framework:transition>
31+
<framework:transition name="spellchecker_approval">
32+
<framework:from>wait_for_spellchecker</framework:from>
33+
<framework:to>approved_by_spellchecker</framework:to>
34+
</framework:transition>
35+
<framework:transition name="publish">
36+
<framework:from>approved_by_journalist</framework:from>
37+
<framework:from>approved_by_spellchecker</framework:from>
38+
<framework:to>published</framework:to>
39+
<framework:guard>!!true</framework:guard>
40+
</framework:transition>
41+
<framework:transition name="publish">
42+
<framework:from>draft</framework:from>
43+
<framework:to>published</framework:to>
44+
<framework:guard>!!false</framework:guard>
45+
</framework:transition>
46+
</framework:workflow>
47+
</framework:config>
48+
</container>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
framework:
2+
workflows:
3+
article:
4+
type: workflow
5+
marking_store:
6+
type: multiple_state
7+
supports:
8+
- Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest
9+
initial_place: draft
10+
places:
11+
- draft
12+
- wait_for_journalist
13+
- approved_by_journalist
14+
- wait_for_spellchecker
15+
- approved_by_spellchecker
16+
- published
17+
transitions:
18+
request_review:
19+
from: [draft]
20+
to: [wait_for_journalist, wait_for_spellchecker]
21+
journalist_approval:
22+
from: [wait_for_journalist]
23+
to: [approved_by_journalist]
24+
spellchecker_approval:
25+
from: [wait_for_spellchecker]
26+
to: [approved_by_spellchecker]
27+
publish:
28+
from: [approved_by_journalist, approved_by_spellchecker]
29+
to: [published]
30+
guard: "!!true"
31+
publish_editor_in_chief:
32+
name: publish
33+
from: [draft]
34+
to: [published]
35+
guard: "!!false"

Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,84 @@ public function testWorkflowMultipleTransitionsWithSameName()
302302

303303
$this->assertCount(5, $transitions);
304304

305-
$this->assertSame('request_review', $transitions[0]->getArgument(0));
306-
$this->assertSame('journalist_approval', $transitions[1]->getArgument(0));
307-
$this->assertSame('spellchecker_approval', $transitions[2]->getArgument(0));
308-
$this->assertSame('publish', $transitions[3]->getArgument(0));
309-
$this->assertSame('publish', $transitions[4]->getArgument(0));
310-
311-
$this->assertSame(array('approved_by_journalist', 'approved_by_spellchecker'), $transitions[3]->getArgument(1));
312-
$this->assertSame(array('draft'), $transitions[4]->getArgument(1));
305+
$this->assertSame('workflow.article.transition.0', (string) $transitions[0]);
306+
$this->assertSame(array(
307+
'request_review',
308+
array(
309+
'draft',
310+
),
311+
array(
312+
'wait_for_journalist', 'wait_for_spellchecker',
313+
),
314+
), $container->getDefinition($transitions[0])->getArguments());
315+
316+
$this->assertSame('workflow.article.transition.1', (string) $transitions[1]);
317+
$this->assertSame(array(
318+
'journalist_approval',
319+
array(
320+
'wait_for_journalist',
321+
),
322+
array(
323+
'approved_by_journalist',
324+
),
325+
), $container->getDefinition($transitions[1])->getArguments());
326+
327+
$this->assertSame('workflow.article.transition.2', (string) $transitions[2]);
328+
$this->assertSame(array(
329+
'spellchecker_approval',
330+
array(
331+
'wait_for_spellchecker',
332+
),
333+
array(
334+
'approved_by_spellchecker',
335+
),
336+
), $container->getDefinition($transitions[2])->getArguments());
337+
338+
$this->assertSame('workflow.article.transition.3', (string) $transitions[3]);
339+
$this->assertSame(array(
340+
'publish',
341+
array(
342+
'approved_by_journalist',
343+
'approved_by_spellchecker',
344+
),
345+
array(
346+
'published',
347+
),
348+
), $container->getDefinition($transitions[3])->getArguments());
349+
350+
$this->assertSame('workflow.article.transition.4', (string) $transitions[4]);
351+
$this->assertSame(array(
352+
'publish',
353+
array(
354+
'draft',
355+
),
356+
array(
357+
'published',
358+
),
359+
), $container->getDefinition($transitions[4])->getArguments());
360+
}
361+
362+
public function testGuardExpressions()
363+
{
364+
$container = $this->createContainerFromFile('workflow_with_guard_expression');
365+
366+
$this->assertTrue($container->hasDefinition('workflow.article.listener.guard'), 'Workflow guard listener is registered as a service');
367+
$this->assertTrue($container->hasParameter('workflow.has_guard_listeners'), 'Workflow guard listeners parameter exists');
368+
$this->assertTrue(true === $container->getParameter('workflow.has_guard_listeners'), 'Workflow guard listeners parameter is enabled');
369+
$guardDefinition = $container->getDefinition('workflow.article.listener.guard');
370+
$this->assertSame(array(
371+
array(
372+
'event' => 'workflow.article.guard.publish',
373+
'method' => 'onTransition',
374+
),
375+
), $guardDefinition->getTag('kernel.event_listener'));
376+
$guardsConfiguration = $guardDefinition->getArgument(0);
377+
$this->assertTrue(1 === \count($guardsConfiguration), 'Workflow guard configuration contains one element per transition name');
378+
$transitionGuardExpressions = $guardsConfiguration['workflow.article.guard.publish'];
379+
$this->assertSame('workflow.article.transition.3', (string) $transitionGuardExpressions[0]->getArgument(0));
380+
$this->assertSame('!!true', $transitionGuardExpressions[0]->getArgument(1));
381+
$this->assertSame('workflow.article.transition.4', (string) $transitionGuardExpressions[1]->getArgument(0));
382+
$this->assertSame('!!false', $transitionGuardExpressions[1]->getArgument(1));
313383
}
314384

315385
public function testWorkflowServicesCanBeEnabled()

0 commit comments

Comments
 (0)