Skip to content
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 @@ -358,7 +358,7 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode): void
->arrayNode('workflows')
->canBeEnabled()
->beforeNormalization()
->always(function ($v) {
->always(static function ($v) {
if (\is_array($v) && true === $v['enabled']) {
$workflows = $v;
unset($workflows['enabled']);
Expand All @@ -367,7 +367,7 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode): void
$workflows = [];
}

if (1 === \count($workflows) && isset($workflows['workflows']) && !array_is_list($workflows['workflows']) && array_diff(array_keys($workflows['workflows']), ['audit_trail', 'type', 'marking_store', 'supports', 'support_strategy', 'initial_marking', 'places', 'transitions'])) {
if (1 === \count($workflows) && isset($workflows['workflows']) && !array_is_list($workflows['workflows']) && array_diff_key($workflows['workflows'], ['audit_trail' => 1, 'type' => 1, 'marking_store' => 1, 'supports' => 1, 'support_strategy' => 1, 'initial_marking' => 1, 'places' => 1, 'transitions' => 1])) {
$workflows = $workflows['workflows'];
}

Expand Down Expand Up @@ -505,26 +505,26 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode): void
->end()
->arrayNode('transitions')
->beforeNormalization()
->always()
->then(function ($transitions) {
->always(static function ($transitions) {
if (!\is_array($transitions)) {
throw new InvalidConfigurationException('The "transitions" option must be an array in workflow configuration.');
}

// It's an indexed array, we let the validation occur
if (isset($transitions[0]) && \is_array($transitions[0])) {
return $transitions;
}

foreach ($transitions as $name => $transition) {
if (\is_array($transition) && \array_key_exists('name', $transition)) {
continue;
$normalizedTransitions = [];
foreach ($transitions as $key => $transition) {
if (\is_array($transition)) {
if (\is_string($key = $transition['key'] ?? $key)) {
$transition['name'] ??= $key;
}
if (!($transition['name'] ?? false)) {
throw new InvalidConfigurationException('The "name" option is required for each transition in workflow configuration.');
}
unset($transition['key']);
}
$transition['name'] = $name;
$transitions[$name] = $transition;
$normalizedTransitions[$key] = $transition;
}

return $transitions;
return $normalizedTransitions;
})
->end()
->isRequired()
Expand All @@ -541,6 +541,7 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode): void
->example('is_fully_authenticated() and is_granted(\'ROLE_JOURNALIST\') and subject.getTitle() == \'My first article\'')
->end()
->arrayNode('from')
->performNoDeepMerging()
->beforeNormalization()
->ifString()
->then(fn ($v) => [$v])
Expand All @@ -551,6 +552,7 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode): void
->end()
->end()
->arrayNode('to')
->performNoDeepMerging()
->beforeNormalization()
->ifString()
->then(fn ($v) => [$v])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -972,16 +972,16 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
$transitionCounter = 0;
foreach ($workflow['transitions'] as $transition) {
if ('workflow' === $type) {
$transitionDefinition = new Definition(Workflow\Transition::class, [$transition['name'], $transition['from'], $transition['to']]);
$transitionId = \sprintf('.%s.transition.%s', $workflowId, $transitionCounter++);
$container->setDefinition($transitionId, $transitionDefinition);
$container->register($transitionId, Workflow\Transition::class)
->setArguments([$transition['name'], $transition['from'], $transition['to']]);
$transitions[] = new Reference($transitionId);
if (isset($transition['guard'])) {
$configuration = new Definition(Workflow\EventListener\GuardExpression::class);
$configuration->addArgument(new Reference($transitionId));
$configuration->addArgument($transition['guard']);
$eventName = \sprintf('workflow.%s.guard.%s', $name, $transition['name']);
$guardsConfiguration[$eventName][] = $configuration;
$guardsConfiguration[$eventName][] = new Definition(
Workflow\EventListener\GuardExpression::class,
[new Reference($transitionId), $transition['guard']]
);
}
if ($transition['metadata']) {
$transitionsMetadataDefinition->addMethodCall('offsetSet', [
Expand All @@ -992,16 +992,16 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
} elseif ('state_machine' === $type) {
foreach ($transition['from'] as $from) {
foreach ($transition['to'] as $to) {
$transitionDefinition = new Definition(Workflow\Transition::class, [$transition['name'], $from, $to]);
$transitionId = \sprintf('.%s.transition.%s', $workflowId, $transitionCounter++);
$container->setDefinition($transitionId, $transitionDefinition);
$container->register($transitionId, Workflow\Transition::class)
->setArguments([$transition['name'], $from, $to]);
$transitions[] = new Reference($transitionId);
if (isset($transition['guard'])) {
$configuration = new Definition(Workflow\EventListener\GuardExpression::class);
$configuration->addArgument(new Reference($transitionId));
$configuration->addArgument($transition['guard']);
$eventName = \sprintf('workflow.%s.guard.%s', $name, $transition['name']);
$guardsConfiguration[$eventName][] = $configuration;
$guardsConfiguration[$eventName][] = new Definition(
Workflow\EventListener\GuardExpression::class,
[new Reference($transitionId), $transition['guard']]
);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

too many intermediary variables made me wonder about this piece of code for too long

}
if ($transition['metadata']) {
$transitionsMetadataDefinition->addMethodCall('offsetSet', [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,8 @@
<xsd:element name="metadata" type="metadata" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="guard" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
<xsd:attribute name="name" type="xsd:string" />
<xsd:attribute name="key" type="xsd:string" />
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In XML, we have to differentiate the name and the key of transitions, so that deep-merging them works the same way as in other config formats (appending numeric keys and recursing into associative ones).

/cc @jakubtobiasz FYI this is how you'll be able to leverage this fix - use attribute "key" instead of "name" if you use the XML format to define workflows.

</xsd:complexType>

<xsd:complexType name="place" mixed="true">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

return function (Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator $container) {
$container->services()->alias('test_workflow', 'workflow.test_workflow')->public();
$container->extension('framework', [
'http_method_override' => false,
'handle_all_throwables' => true,
'annotations' => [
'enabled' => false,
],
'php_errors' => [
'log' => true,
],
'workflows' => [
'test_workflow' => [
'type' => 'workflow',
'supports' => [
'Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTestCase',
],
'initial_marking' => ['start'],
'places' => [
'start',
'middle',
'end',
'alternative',
],
'transitions' => [
'base_transition' => [
'from' => ['start'],
'to' => ['end'],
],
],
],
],
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

return function (Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator $container) {
$container->extension('framework', [
'http_method_override' => false,
'handle_all_throwables' => true,
'annotations' => [
'enabled' => false,
],
'php_errors' => [
'log' => true,
],
'workflows' => [
'test_workflow' => [
'type' => 'workflow',
'supports' => [
'Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTestCase',
],
'initial_marking' => ['start'],
'places' => [
'start',
'middle',
'end',
'alternative',
],
'transitions' => [
'base_transition' => [
'from' => ['middle'],
'to' => ['alternative'],
],
],
],
],
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" ?>
<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 https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<services>
<service id="test_workflow" alias="workflow.test_workflow" public="true" />
</services>
<framework:config http-method-override="false" handle-all-throwables="true">
<framework:annotations enabled="false" />
<framework:php-errors log="true" />
<framework:workflow name="test_workflow" type="workflow">
<framework:initial-marking>start</framework:initial-marking>
<framework:support>Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTestCase</framework:support>
<framework:place>start</framework:place>
<framework:place>middle</framework:place>
<framework:place>end</framework:place>
<framework:place>alternative</framework:place>
<framework:transition key="base_transition">
<framework:from>start</framework:from>
<framework:to>end</framework:to>
</framework:transition>
</framework:workflow>
</framework:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8" ?>
<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 https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config http-method-override="false" handle-all-throwables="true">
<framework:annotations enabled="false" />
<framework:php-errors log="true" />
<framework:workflow name="test_workflow" type="workflow">
<framework:transition key="base_transition">
<framework:from>middle</framework:from>
<framework:to>alternative</framework:to>
</framework:transition>
</framework:workflow>
</framework:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
services:
test_workflow:
alias: workflow.test_workflow
public: true
framework:
http_method_override: false
handle_all_throwables: true
annotations:
enabled: false
php_errors:
log: true
workflows:
test_workflow:
type: workflow
supports:
- Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTestCase
initial_marking: [start]
places:
- start
- middle
- end
- alternative
transitions:
base_transition:
from: [start]
to: [end]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
framework:
http_method_override: false
handle_all_throwables: true
annotations:
enabled: false
php_errors:
log: true
workflows:
test_workflow:
transitions:
base_transition:
from: [middle]
to: [alternative]
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,27 @@ public function testWorkflowsWithSpecifiedDispatchedEvents()
$this->assertSame([WorkflowEvents::LEAVE, WorkflowEvents::COMPLETED], $eventsToDispatch);
}

public function testWorkflowTransitionsPerformNoDeepMerging()
{
$container = $this->createContainer(['kernel.charset' => 'UTF-8', 'kernel.secret' => 'secret', 'kernel.runtime_environment' => 'test']);
$container->registerExtension(new FrameworkExtension());

$this->loadFromFile($container, 'workflow_base_config');

$this->loadFromFile($container, 'workflow_override_config');

$container->compile();

$transitions = [];

foreach ($container->getDefinition('test_workflow')->getArgument(0)->getArgument(1) as $transitionDefinition) {
$transitions[] = $transitionDefinition->getArguments();
}

$this->assertCount(1, $transitions);
$this->assertSame(['base_transition', ['middle'], ['alternative']], $transitions[0]);
}

public function testEnabledPhpErrorsConfig()
{
$container = $this->createContainerFromFile('php_errors_enabled');
Expand Down
Loading