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
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ CHANGELOG
* Add `KernelBrowser::getSession()`
* Add support for configuring workflow places with glob patterns matching consts/backed enums
* Add support for configuring the `CachingHttpClient`
* Add support for weighted transitions in workflows

7.3
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,11 +562,11 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode): void
->requiresAtLeastOneElement()
->prototype('array')
->children()
->scalarNode('name')
->stringNode('name')
->isRequired()
->cannotBeEmpty()
->end()
->scalarNode('guard')
->stringNode('guard')
->cannotBeEmpty()
->info('An expression to block the transition.')
->example('is_fully_authenticated() and is_granted(\'ROLE_JOURNALIST\') and subject.getTitle() == \'My first article\'')
Expand All @@ -576,23 +576,79 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode): void
->acceptAndWrap(['backed-enum', 'string'])
->beforeNormalization()
->ifArray()
->then(static fn ($from) => array_map(static fn ($v) => $v instanceof \BackedEnum ? $v->value : $v, $from))
->then($workflowNormalizeArcs = static function ($arcs) {
// Fix XML parsing, when only one arc is defined
if (\array_key_exists('value', $arcs) && \array_key_exists('weight', $arcs)) {
return [[
'place' => $arcs['value'],
'weight' => $arcs['weight'],
]];
}

$normalizedArcs = [];
foreach ($arcs as $arc) {
if ($arc instanceof \BackedEnum) {
$arc = $arc->value;
}
if (\is_string($arc)) {
$arc = [
'place' => $arc,
'weight' => 1,
];
} elseif (!\is_array($arc)) {
throw new InvalidConfigurationException('The "from" arcs must be a list of strings or arrays in workflow configuration.');
} elseif (\array_key_exists('value', $arc) && \array_key_exists('weight', $arc)) {
// Fix XML parsing
$arc = [
'place' => $arc['value'],
'weight' => $arc['weight'],
];
}

$normalizedArcs[] = $arc;
}

return $normalizedArcs;
})
->end()
->requiresAtLeastOneElement()
->prototype('scalar')
->cannotBeEmpty()
->prototype('array')
->children()
->stringNode('place')
->isRequired()
->cannotBeEmpty()
->end()
->integerNode('weight')
->isRequired()
->end()
->end()
->end()
->end()
->arrayNode('to')
->performNoDeepMerging()
->acceptAndWrap(['backed-enum', 'string'])
->beforeNormalization()
->ifArray()
->then(static fn ($to) => array_map(static fn ($v) => $v instanceof \BackedEnum ? $v->value : $v, $to))
->then($workflowNormalizeArcs)
->end()
->requiresAtLeastOneElement()
->prototype('scalar')
->cannotBeEmpty()
->prototype('array')
->children()
->stringNode('place')
->isRequired()
->cannotBeEmpty()
->end()
->integerNode('weight')
->isRequired()
->end()
->end()
->end()
->end()
->integerNode('weight')
->defaultValue(1)
->validate()
->ifTrue(static fn ($v) => $v < 1)
->thenInvalid('The weight must be greater than 0.')
->end()
->end()
->arrayNode('metadata')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@
use Symfony\Component\WebLink\HttpHeaderParser;
use Symfony\Component\WebLink\HttpHeaderSerializer;
use Symfony\Component\Workflow;
use Symfony\Component\Workflow\Arc;
use Symfony\Component\Workflow\WorkflowInterface;
use Symfony\Component\Yaml\Command\LintCommand as BaseYamlLintCommand;
use Symfony\Component\Yaml\Yaml;
Expand Down Expand Up @@ -1114,6 +1115,11 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
// Global transition counter per workflow
$transitionCounter = 0;
foreach ($workflow['transitions'] as $transition) {
foreach (['from', 'to'] as $direction) {
foreach ($transition[$direction] as $k => $arc) {
$transition[$direction][$k] = new Definition(Arc::class, [$arc['place'], $arc['weight'] ?? 1]);
}
}
if ('workflow' === $type) {
$transitionId = \sprintf('.%s.transition.%s', $workflowId, $transitionCounter++);
$container->register($transitionId, Workflow\Transition::class)
Expand All @@ -1137,7 +1143,7 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
foreach ($transition['to'] as $to) {
$transitionId = \sprintf('.%s.transition.%s', $workflowId, $transitionCounter++);
$container->register($transitionId, Workflow\Transition::class)
->setArguments([$transition['name'], $from, $to]);
->setArguments([$transition['name'], [$from], [$to]]);
$transitions[] = new Reference($transitionId);
if (isset($transition['guard'])) {
$eventName = \sprintf('workflow.%s.guard.%s', $name, $transition['name']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,15 +518,23 @@

<xsd:complexType name="transition">
<xsd:sequence>
<xsd:element name="from" type="xsd:string" minOccurs="1" maxOccurs="unbounded" />
<xsd:element name="to" type="xsd:string" minOccurs="1" maxOccurs="unbounded" />
<xsd:element name="from" type="arc" minOccurs="1" maxOccurs="unbounded" />
<xsd:element name="to" type="arc" minOccurs="1" maxOccurs="unbounded" />
<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" />
<xsd:attribute name="key" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="arc">
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="weight" type="xsd:integer" />
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>

<xsd:complexType name="place" mixed="true">
<xsd:sequence>
<xsd:element name="metadata" type="metadata" minOccurs="0" maxOccurs="unbounded" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,28 @@
'approved_by_spellchecker',
'published',
],
// We also test different configuration formats here
'transitions' => [
'request_review' => [
'from' => 'draft',
'to' => ['wait_for_journalist', 'wait_for_spellchecker'],
],
'journalist_approval' => [
'from' => 'wait_for_journalist',
'from' => ['wait_for_journalist'],
'to' => 'approved_by_journalist',
],
'spellchecker_approval' => [
'from' => 'wait_for_spellchecker',
'to' => 'approved_by_spellchecker',
],
'publish' => [
'from' => ['approved_by_journalist', 'approved_by_spellchecker'],
'from' => [['place' => 'approved_by_journalist', 'weight' => 1], 'approved_by_spellchecker'],
'to' => 'published',
],
'publish_editor_in_chief' => [
'name' => 'publish',
'from' => 'draft',
'to' => 'published',
'to' => [['place' => 'published', 'weight' => 2]],
],
],
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<framework:place name="wait_for_spellchecker" />
<framework:place name="approved_by_spellchecker" />
<framework:place name="published" />
<!-- We also test different configuration formats here -->
<framework:transition name="request_review">
<framework:from>draft</framework:from>
<framework:to>wait_for_journalist</framework:to>
Expand All @@ -32,13 +33,13 @@
<framework:to>approved_by_spellchecker</framework:to>
</framework:transition>
<framework:transition name="publish">
<framework:from>approved_by_journalist</framework:from>
<framework:from>approved_by_spellchecker</framework:from>
<framework:from weight="1">approved_by_journalist</framework:from>
<framework:from weight="1">approved_by_spellchecker</framework:from>
<framework:to>published</framework:to>
</framework:transition>
<framework:transition name="publish">
<framework:from>draft</framework:from>
<framework:to>published</framework:to>
<framework:to weight="2">published</framework:to>
</framework:transition>
</framework:workflow>
</framework:config>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@ framework:
- wait_for_spellchecker
- approved_by_spellchecker
- published
# We also test different configuration formats here
transitions:
request_review:
from: [draft]
from: draft
to: [wait_for_journalist, wait_for_spellchecker]
journalist_approval:
from: [wait_for_journalist]
to: [approved_by_journalist]
to: approved_by_journalist
spellchecker_approval:
from: [wait_for_spellchecker]
to: [approved_by_spellchecker]
from: wait_for_spellchecker
to: approved_by_spellchecker
publish:
from: [approved_by_journalist, approved_by_spellchecker]
to: [published]
from: [{place: approved_by_journalist, weight: 1}, approved_by_spellchecker]
to: published
publish_editor_in_chief:
name: publish
from: [draft]
to: [published]
from: draft
to: [{place: published, weight: 2}]
Loading
Loading