Skip to content

[Workflow] Choose which Workflow events should be dispatched #37815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Symfony\Component\Translation\Translator;
use Symfony\Component\Validator\Validation;
use Symfony\Component\WebLink\HttpHeaderSerializer;
use Symfony\Component\Workflow\WorkflowEvents;

/**
* FrameworkExtension configuration structure.
Expand Down Expand Up @@ -339,6 +340,7 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode)
->fixXmlConfig('support')
->fixXmlConfig('place')
->fixXmlConfig('transition')
->fixXmlConfig('event_to_dispatch', 'events_to_dispatch')
->children()
->arrayNode('audit_trail')
->canBeEnabled()
Expand Down Expand Up @@ -381,6 +383,33 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode)
->defaultValue([])
->prototype('scalar')->end()
->end()
->variableNode('events_to_dispatch')
->defaultValue(null)
->validate()
->ifTrue(function ($v) {
if (null === $v) {
return false;
}
if (!\is_array($v)) {
return true;
}

foreach ($v as $value) {
if (!\is_string($value)) {
return true;
}
if (class_exists(WorkflowEvents::class) && !\in_array($value, WorkflowEvents::ALIASES)) {
return true;
}
}

return false;
})
->thenInvalid('The value must be "null" or an array of workflow events (like ["workflow.enter"]).')
->end()
->info('Select which Transition events should be dispatched for this Workflow')
->example(['workflow.enter', 'workflow.transition'])
->end()
->arrayNode('places')
->beforeNormalization()
->always()
Expand Down Expand Up @@ -509,6 +538,18 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode)
})
->thenInvalid('"supports" or "support_strategy" should be configured.')
->end()
->beforeNormalization()
->always()
->then(function ($values) {
// Special case to deal with XML when the user wants an empty array
if (\array_key_exists('event_to_dispatch', $values) && null === $values['event_to_dispatch']) {
$values['events_to_dispatch'] = [];
unset($values['event_to_dispatch']);
}

return $values;
})
->end()
->end()
->end()
->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,7 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
$workflowDefinition->replaceArgument(1, $markingStoreDefinition);
}
$workflowDefinition->replaceArgument(3, $name);
$workflowDefinition->replaceArgument(4, $workflow['events_to_dispatch']);

// Store to container
$container->setDefinition($workflowId, $workflowDefinition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@
<xsd:element name="initial-marking" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="marking-store" type="marking_store" minOccurs="0" maxOccurs="1" />
<xsd:element name="support" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="event-to-dispatch" type="event_to_dispatch" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="place" type="place" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="transition" type="transition" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="metadata" type="metadata" minOccurs="0" maxOccurs="unbounded" />
Expand Down Expand Up @@ -345,6 +346,19 @@
</xsd:sequence>
</xsd:complexType>

<xsd:simpleType name="event_to_dispatch">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="" />
<xsd:enumeration value="workflow.leave" />
<xsd:enumeration value="workflow.leave" />
<xsd:enumeration value="workflow.transition" />
<xsd:enumeration value="workflow.enter" />
<xsd:enumeration value="workflow.entered" />
<xsd:enumeration value="workflow.completed" />
<xsd:enumeration value="workflow.announce" />
</xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="default_middleware">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="true" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
abstract_arg('marking store'),
service('event_dispatcher')->ignoreOnInvalid(),
abstract_arg('workflow name'),
abstract_arg('events to dispatch'),
])
->abstract()
->public()
Expand All @@ -34,6 +35,7 @@
abstract_arg('marking store'),
service('event_dispatcher')->ignoreOnInvalid(),
abstract_arg('workflow name'),
abstract_arg('events to dispatch'),
])
->abstract()
->public()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

use Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest;

$container->loadFromExtension('framework', [
'workflows' => [
'my_workflow' => [
'type' => 'state_machine',
'marking_store' => [
'type' => 'method',
'property' => 'state'
],
'supports' => [
FrameworkExtensionTest::class,
],
'events_to_dispatch' => [],
'places' => [
'one',
'two',
'three',
],
'transitions' => [
'count_to_two' => [
'from' => [
'one',
],
'to' => [
'two',
],
],
'count_to_three' => [
'from' => [
'two',
],
'to' => [
'three'
]
]
],
],
],
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

use Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest;

$container->loadFromExtension('framework', [
'workflows' => [
'my_workflow' => [
'type' => 'state_machine',
'marking_store' => [
'type' => 'method',
'property' => 'state'
],
'supports' => [
FrameworkExtensionTest::class,
],
'events_to_dispatch' => [
'workflow.leave',
'workflow.completed',
],
'places' => [
'one',
'two',
'three',
],
'transitions' => [
'count_to_two' => [
'from' => [
'one',
],
'to' => [
'two',
],
],
'count_to_three' => [
'from' => [
'two',
],
'to' => [
'three'
]
]
],
],
],
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config>
<framework:workflow name="my_workflow" type="state_machine">
<framework:initial-marking>one</framework:initial-marking>
<framework:marking-store type="method" property="state" />
<framework:support>Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest</framework:support>
<framework:event-to-dispatch></framework:event-to-dispatch>
<framework:place name="one" />
<framework:place name="two" />
<framework:place name="three" />
<framework:transition name="count_to_two">
<framework:from>one</framework:from>
<framework:to>two</framework:to>
</framework:transition>
<framework:transition name="count_to_three">
<framework:from>two</framework:from>
<framework:to>three</framework:to>
</framework:transition>
</framework:workflow>
</framework:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config>
<framework:workflow name="my_workflow" type="state_machine">
<framework:initial-marking>one</framework:initial-marking>
<framework:marking-store type="method" property="state" />
<framework:support>Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest</framework:support>
<framework:event-to-dispatch>workflow.leave</framework:event-to-dispatch>
<framework:event-to-dispatch>workflow.completed</framework:event-to-dispatch>
<framework:place name="one" />
<framework:place name="two" />
<framework:place name="three" />
<framework:transition name="count_to_two">
<framework:from>one</framework:from>
<framework:to>two</framework:to>
</framework:transition>
<framework:transition name="count_to_three">
<framework:from>two</framework:from>
<framework:to>three</framework:to>
</framework:transition>
</framework:workflow>
</framework:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
framework:
workflows:
my_workflow:
type: state_machine
initial_marking: one
events_to_dispatch: []
marking_store:
type: method
property: state
supports:
- Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest
places:
- one
- two
- three
transitions:
count_to_two:
from: one
to: two
count_to_three:
from: two
to: three
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
framework:
workflows:
my_workflow:
type: state_machine
initial_marking: one
events_to_dispatch: ['workflow.leave', 'workflow.completed']
marking_store:
type: method
property: state
supports:
- Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest
places:
- one
- two
- three
transitions:
count_to_two:
from: one
to: two
count_to_three:
from: two
to: three
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass;
use Symfony\Component\Validator\Mapping\Loader\PropertyInfoLoader;
use Symfony\Component\Workflow;
use Symfony\Component\Workflow\WorkflowEvents;

abstract class FrameworkExtensionTest extends TestCase
{
Expand Down Expand Up @@ -216,6 +217,8 @@ public function testWorkflows()

$this->assertTrue($container->hasDefinition('workflow.article'), 'Workflow is registered as a service');
$this->assertSame('workflow.abstract', $container->getDefinition('workflow.article')->getParent());
$this->assertNull($container->getDefinition('workflow.article')->getArgument('index_4'), 'Workflows has eventsToDispatch=null');

$this->assertTrue($container->hasDefinition('workflow.article.definition'), 'Workflow definition is registered as a service');

$workflowDefinition = $container->getDefinition('workflow.article.definition');
Expand Down Expand Up @@ -423,6 +426,24 @@ public function testWorkflowsNamedExplicitlyEnabled()
$this->assertTrue($container->hasDefinition('workflow.workflows.definition'));
}

public function testWorkflowsWithNoDispatchedEvents()
{
$container = $this->createContainerFromFile('workflow_with_no_events_to_dispatch');

$eventsToDispatch = $container->getDefinition('state_machine.my_workflow')->getArgument('index_4');

$this->assertSame([], $eventsToDispatch);
}

public function testWorkflowsWithSpecifiedDispatchedEvents()
{
$container = $this->createContainerFromFile('workflow_with_specified_events_to_dispatch');

$eventsToDispatch = $container->getDefinition('state_machine.my_workflow')->getArgument('index_4');

$this->assertSame([WorkflowEvents::LEAVE, WorkflowEvents::COMPLETED], $eventsToDispatch);
}

public function testEnabledPhpErrorsConfig()
{
$container = $this->createContainerFromFile('php_errors_enabled');
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Workflow/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
* Added context to the event dispatched
* Dispatch an event when the subject enters in the workflow for the very first time
* Added a default context to the previous event
* Added support for specifying which events should be dispatched when calling `workflow->apply()`

5.1.0
-----
Expand Down
Loading