Skip to content

Commit d700747

Browse files
stewartmaliklyrixx
authored andcommitted
[Workflow] Choose which Workflow events should be dispatched
1 parent da60fbe commit d700747

21 files changed

+529
-44
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode)
339339
->fixXmlConfig('support')
340340
->fixXmlConfig('place')
341341
->fixXmlConfig('transition')
342+
->fixXmlConfig('dispatched_event')
342343
->children()
343344
->arrayNode('audit_trail')
344345
->canBeEnabled()
@@ -381,6 +382,22 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode)
381382
->defaultValue([])
382383
->prototype('scalar')->end()
383384
->end()
385+
->arrayNode('dispatched_events')
386+
->beforeNormalization()
387+
->ifString()
388+
->then(function ($v) {
389+
return [$v];
390+
})
391+
->end()
392+
// We have to specify a default here as when this config option
393+
// isn't set the default behaviour of `arrayNode()` is to return an empty
394+
// array which conflicts with our Definition, and we cannot set a default
395+
// of `null` for arrayNode()'s
396+
->defaultValue(['all'])
397+
->prototype('scalar')->end()
398+
->info('Select which Transition events should be dispatched for this Workflow')
399+
->example(['leave', 'completed'])
400+
->end()
384401
->arrayNode('places')
385402
->beforeNormalization()
386403
->always()

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,13 +742,25 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
742742
$places = array_column($workflow['places'], 'name');
743743
$initialMarking = $workflow['initial_marking'] ?? [];
744744

745+
// Record which events should be dispatched
746+
if ($workflow['dispatched_events'] === ['all']) {
747+
$dispatchedEvents = null;
748+
} elseif ($workflow['dispatched_events'] === ['none']) {
749+
$dispatchedEvents = [];
750+
} else {
751+
$dispatchedEvents = array_map(function (string $event) {
752+
return 'workflow.'.$event;
753+
}, $workflow['dispatched_events']);
754+
}
755+
745756
// Create a Definition
746757
$definitionDefinition = new Definition(Workflow\Definition::class);
747758
$definitionDefinition->setPublic(false);
748759
$definitionDefinition->addArgument($places);
749760
$definitionDefinition->addArgument($transitions);
750761
$definitionDefinition->addArgument($initialMarking);
751762
$definitionDefinition->addArgument($metadataStoreDefinition);
763+
$definitionDefinition->addArgument($dispatchedEvents);
752764
$definitionDefinition->addTag('workflow.definition', [
753765
'name' => $name,
754766
'type' => $type,

src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@
288288
<xsd:element name="initial-marking" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
289289
<xsd:element name="marking-store" type="marking_store" minOccurs="0" maxOccurs="1" />
290290
<xsd:element name="support" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
291+
<xsd:element name="dispatched-event" type="dispatched_event" minOccurs="0" maxOccurs="unbounded" />
291292
<xsd:element name="place" type="place" minOccurs="0" maxOccurs="unbounded" />
292293
<xsd:element name="transition" type="transition" minOccurs="0" maxOccurs="unbounded" />
293294
<xsd:element name="metadata" type="metadata" minOccurs="0" maxOccurs="unbounded" />
@@ -345,6 +346,19 @@
345346
</xsd:sequence>
346347
</xsd:complexType>
347348

349+
<xsd:simpleType name="dispatched_event">
350+
<xsd:restriction base="xsd:string">
351+
<xsd:enumeration value="all" />
352+
<xsd:enumeration value="none" />
353+
<xsd:enumeration value="leave" />
354+
<xsd:enumeration value="transition" />
355+
<xsd:enumeration value="enter" />
356+
<xsd:enumeration value="entered" />
357+
<xsd:enumeration value="completed" />
358+
<xsd:enumeration value="announce" />
359+
</xsd:restriction>
360+
</xsd:simpleType>
361+
348362
<xsd:simpleType name="default_middleware">
349363
<xsd:restriction base="xsd:string">
350364
<xsd:enumeration value="true" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest;
4+
5+
$container->loadFromExtension('framework', [
6+
'workflows' => [
7+
'my_workflow' => [
8+
'type' => 'state_machine',
9+
'marking_store' => [
10+
'type' => 'method',
11+
'property' => 'state'
12+
],
13+
'supports' => [
14+
FrameworkExtensionTest::class,
15+
],
16+
'places' => [
17+
'one',
18+
'two',
19+
'three',
20+
],
21+
'transitions' => [
22+
'count_to_two' => [
23+
'from' => [
24+
'one',
25+
],
26+
'to' => [
27+
'two',
28+
],
29+
],
30+
'count_to_three' => [
31+
'from' => [
32+
'two',
33+
],
34+
'to' => [
35+
'three'
36+
]
37+
]
38+
],
39+
],
40+
],
41+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
use Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest;
4+
5+
$container->loadFromExtension('framework', [
6+
'workflows' => [
7+
'my_workflow' => [
8+
'type' => 'state_machine',
9+
'marking_store' => [
10+
'type' => 'method',
11+
'property' => 'state'
12+
],
13+
'supports' => [
14+
FrameworkExtensionTest::class,
15+
],
16+
'dispatched_events' => [],
17+
'places' => [
18+
'one',
19+
'two',
20+
'three',
21+
],
22+
'transitions' => [
23+
'count_to_two' => [
24+
'from' => [
25+
'one',
26+
],
27+
'to' => [
28+
'two',
29+
],
30+
],
31+
'count_to_three' => [
32+
'from' => [
33+
'two',
34+
],
35+
'to' => [
36+
'three'
37+
]
38+
]
39+
],
40+
],
41+
],
42+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
use Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest;
4+
5+
$container->loadFromExtension('framework', [
6+
'workflows' => [
7+
'my_workflow' => [
8+
'type' => 'state_machine',
9+
'marking_store' => [
10+
'type' => 'method',
11+
'property' => 'state'
12+
],
13+
'supports' => [
14+
FrameworkExtensionTest::class,
15+
],
16+
'dispatched_events' => [
17+
'leave',
18+
'completed'
19+
],
20+
'places' => [
21+
'one',
22+
'two',
23+
'three',
24+
],
25+
'transitions' => [
26+
'count_to_two' => [
27+
'from' => [
28+
'one',
29+
],
30+
'to' => [
31+
'two',
32+
],
33+
],
34+
'count_to_three' => [
35+
'from' => [
36+
'two',
37+
],
38+
'to' => [
39+
'three'
40+
]
41+
]
42+
],
43+
],
44+
],
45+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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="my_workflow" type="state_machine">
11+
<framework:initial-marking>one</framework:initial-marking>
12+
<framework:marking-store type="method" property="state" />
13+
<framework:support>Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest</framework:support>
14+
<framework:place name="one" />
15+
<framework:place name="two" />
16+
<framework:place name="three" />
17+
<framework:transition name="count_to_two">
18+
<framework:from>one</framework:from>
19+
<framework:to>two</framework:to>
20+
</framework:transition>
21+
<framework:transition name="count_to_three">
22+
<framework:from>two</framework:from>
23+
<framework:to>three</framework:to>
24+
</framework:transition>
25+
</framework:workflow>
26+
</framework:config>
27+
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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="my_workflow" type="state_machine">
11+
<framework:initial-marking>one</framework:initial-marking>
12+
<framework:marking-store type="method" property="state" />
13+
<framework:support>Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest</framework:support>
14+
<framework:dispatched-event>none</framework:dispatched-event>
15+
<framework:place name="one" />
16+
<framework:place name="two" />
17+
<framework:place name="three" />
18+
<framework:transition name="count_to_two">
19+
<framework:from>one</framework:from>
20+
<framework:to>two</framework:to>
21+
</framework:transition>
22+
<framework:transition name="count_to_three">
23+
<framework:from>two</framework:from>
24+
<framework:to>three</framework:to>
25+
</framework:transition>
26+
</framework:workflow>
27+
</framework:config>
28+
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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="my_workflow" type="state_machine">
11+
<framework:initial-marking>one</framework:initial-marking>
12+
<framework:marking-store type="method" property="state" />
13+
<framework:support>Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest</framework:support>
14+
<framework:dispatched-event>leave</framework:dispatched-event>
15+
<framework:dispatched-event>completed</framework:dispatched-event>
16+
<framework:place name="one" />
17+
<framework:place name="two" />
18+
<framework:place name="three" />
19+
<framework:transition name="count_to_two">
20+
<framework:from>one</framework:from>
21+
<framework:to>two</framework:to>
22+
</framework:transition>
23+
<framework:transition name="count_to_three">
24+
<framework:from>two</framework:from>
25+
<framework:to>three</framework:to>
26+
</framework:transition>
27+
</framework:workflow>
28+
</framework:config>
29+
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
framework:
2+
workflows:
3+
my_workflow:
4+
type: state_machine
5+
initial_marking: one
6+
marking_store:
7+
type: method
8+
property: state
9+
supports:
10+
- Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest
11+
places:
12+
- one
13+
- two
14+
- three
15+
transitions:
16+
count_to_two:
17+
from: one
18+
to: two
19+
count_to_three:
20+
from: two
21+
to: three

0 commit comments

Comments
 (0)