Skip to content

Commit cfc53ad

Browse files
committed
[Workflow] Choose which Workflow events should be dispatched (fix previous commit)
1 parent d700747 commit cfc53ad

22 files changed

+193
-289
lines changed

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

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use Symfony\Component\Translation\Translator;
3535
use Symfony\Component\Validator\Validation;
3636
use Symfony\Component\WebLink\HttpHeaderSerializer;
37+
use Symfony\Component\Workflow\WorkflowEvents;
3738

3839
/**
3940
* FrameworkExtension configuration structure.
@@ -339,7 +340,7 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode)
339340
->fixXmlConfig('support')
340341
->fixXmlConfig('place')
341342
->fixXmlConfig('transition')
342-
->fixXmlConfig('dispatched_event')
343+
->fixXmlConfig('event_to_dispatch', 'events_to_dispatch')
343344
->children()
344345
->arrayNode('audit_trail')
345346
->canBeEnabled()
@@ -382,21 +383,32 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode)
382383
->defaultValue([])
383384
->prototype('scalar')->end()
384385
->end()
385-
->arrayNode('dispatched_events')
386-
->beforeNormalization()
387-
->ifString()
388-
->then(function ($v) {
389-
return [$v];
386+
->variableNode('events_to_dispatch')
387+
->defaultValue(null)
388+
->validate()
389+
->ifTrue(function ($v) {
390+
if (null === $v) {
391+
return false;
392+
}
393+
if (!\is_array($v)) {
394+
return true;
395+
}
396+
397+
foreach ($v as $value) {
398+
if (!\is_string($value)) {
399+
return true;
400+
}
401+
if (class_exists(WorkflowEvents::class) && !\in_array($value, WorkflowEvents::ALIASES)) {
402+
return true;
403+
}
404+
}
405+
406+
return false;
390407
})
408+
->thenInvalid('The value must be "null" or an array of workflow events (like ["workflow.enter"]).')
391409
->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()
398410
->info('Select which Transition events should be dispatched for this Workflow')
399-
->example(['leave', 'completed'])
411+
->example(['workflow.enter', 'workflow.transition'])
400412
->end()
401413
->arrayNode('places')
402414
->beforeNormalization()
@@ -526,6 +538,18 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode)
526538
})
527539
->thenInvalid('"supports" or "support_strategy" should be configured.')
528540
->end()
541+
->beforeNormalization()
542+
->always()
543+
->then(function ($values) {
544+
// Special case to deal with XML when the user wants an empty array
545+
if (\array_key_exists('event_to_dispatch', $values) && null === $values['event_to_dispatch']) {
546+
$values['events_to_dispatch'] = [];
547+
unset($values['event_to_dispatch']);
548+
}
549+
550+
return $values;
551+
})
552+
->end()
529553
->end()
530554
->end()
531555
->end()

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -742,25 +742,13 @@ 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-
756745
// Create a Definition
757746
$definitionDefinition = new Definition(Workflow\Definition::class);
758747
$definitionDefinition->setPublic(false);
759748
$definitionDefinition->addArgument($places);
760749
$definitionDefinition->addArgument($transitions);
761750
$definitionDefinition->addArgument($initialMarking);
762751
$definitionDefinition->addArgument($metadataStoreDefinition);
763-
$definitionDefinition->addArgument($dispatchedEvents);
764752
$definitionDefinition->addTag('workflow.definition', [
765753
'name' => $name,
766754
'type' => $type,
@@ -784,6 +772,7 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
784772
$workflowDefinition->replaceArgument(1, $markingStoreDefinition);
785773
}
786774
$workflowDefinition->replaceArgument(3, $name);
775+
$workflowDefinition->replaceArgument(4, $workflow['events_to_dispatch']);
787776

788777
// Store to container
789778
$container->setDefinition($workflowId, $workflowDefinition);

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +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" />
291+
<xsd:element name="event-to-dispatch" type="event_to_dispatch" minOccurs="0" maxOccurs="unbounded" />
292292
<xsd:element name="place" type="place" minOccurs="0" maxOccurs="unbounded" />
293293
<xsd:element name="transition" type="transition" minOccurs="0" maxOccurs="unbounded" />
294294
<xsd:element name="metadata" type="metadata" minOccurs="0" maxOccurs="unbounded" />
@@ -346,16 +346,16 @@
346346
</xsd:sequence>
347347
</xsd:complexType>
348348

349-
<xsd:simpleType name="dispatched_event">
349+
<xsd:simpleType name="event_to_dispatch">
350350
<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" />
351+
<xsd:enumeration value="" />
352+
<xsd:enumeration value="workflow.leave" />
353+
<xsd:enumeration value="workflow.leave" />
354+
<xsd:enumeration value="workflow.transition" />
355+
<xsd:enumeration value="workflow.enter" />
356+
<xsd:enumeration value="workflow.entered" />
357+
<xsd:enumeration value="workflow.completed" />
358+
<xsd:enumeration value="workflow.announce" />
359359
</xsd:restriction>
360360
</xsd:simpleType>
361361

src/Symfony/Bundle/FrameworkBundle/Resources/config/workflow.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
abstract_arg('marking store'),
2626
service('event_dispatcher')->ignoreOnInvalid(),
2727
abstract_arg('workflow name'),
28+
abstract_arg('events to dispatch'),
2829
])
2930
->abstract()
3031
->public()
@@ -34,6 +35,7 @@
3435
abstract_arg('marking store'),
3536
service('event_dispatcher')->ignoreOnInvalid(),
3637
abstract_arg('workflow name'),
38+
abstract_arg('events to dispatch'),
3739
])
3840
->abstract()
3941
->public()

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/workflow_with_no_dispatched_events.php

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
'supports' => [
1414
FrameworkExtensionTest::class,
1515
],
16+
'events_to_dispatch' => [],
1617
'places' => [
1718
'one',
1819
'two',
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
'supports' => [
1414
FrameworkExtensionTest::class,
1515
],
16-
'dispatched_events' => [
17-
'leave',
18-
'completed'
16+
'events_to_dispatch' => [
17+
'workflow.leave',
18+
'workflow.completed',
1919
],
2020
'places' => [
2121
'one',
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<framework:initial-marking>one</framework:initial-marking>
1212
<framework:marking-store type="method" property="state" />
1313
<framework:support>Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest</framework:support>
14+
<framework:event-to-dispatch></framework:event-to-dispatch>
1415
<framework:place name="one" />
1516
<framework:place name="two" />
1617
<framework:place name="three" />

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/workflow_with_specified_dispatched_events.xml

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
<framework:initial-marking>one</framework:initial-marking>
1212
<framework:marking-store type="method" property="state" />
1313
<framework:support>Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest</framework:support>
14-
<framework:dispatched-event>none</framework:dispatched-event>
14+
<framework:event-to-dispatch>workflow.leave</framework:event-to-dispatch>
15+
<framework:event-to-dispatch>workflow.completed</framework:event-to-dispatch>
1516
<framework:place name="one" />
1617
<framework:place name="two" />
1718
<framework:place name="three" />

0 commit comments

Comments
 (0)