Skip to content

[Workflow] Add support for weighted transitions #60201

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

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
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 @@ -25,6 +25,7 @@ CHANGELOG
* Set `framework.rate_limiter.limiters.*.lock_factory` to `auto` by default
* Deprecate `RateLimiterFactory` autowiring aliases, use `RateLimiterFactoryInterface` instead
* Allow configuring compound rate limiters
* Add support for weighted transitions in workflows

7.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,27 +546,80 @@ 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\'')
->end()
->arrayNode('from')
->beforeNormalization()->castToArray()->end()
->beforeNormalization()
->always()
->then($workflowNormalizeArcs = static function ($arcs) {
$arcs = (array) $arcs;

// Fix XML parsing, when only one arc is defined
if (array_key_exists('weight', $arcs) && array_key_exists('value', $arcs)) {
$arcs = [$arcs];
}

foreach ($arcs as $k => $arc) {
if (is_int($k) && is_string($arc)) {
$arcs[$k] = [
'place' => $arc,
'weight' => 1,
];
} else if (array_key_exists('weight', $arc) && array_key_exists('value', $arc)) {
// Fix XML parsing
$arcs[$k] = [
'place' => $arc['value'],
'weight' => $arc['weight'],
];
}
}

return $arcs;
})
->end()
->requiresAtLeastOneElement()
->prototype('scalar')
->cannotBeEmpty()
->prototype('array')
->children()
->stringNode('place')
->isRequired()
->cannotBeEmpty()
->end()
->integerNode('weight')
->isRequired()
->end()
->end()
->end()
->end()
->arrayNode('to')
->beforeNormalization()->castToArray()->end()
->beforeNormalization()
->always()
->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 (int $v): bool => $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 @@ -17,11 +17,9 @@
use Doctrine\ORM\Mapping\MappedSuperclass;
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use phpDocumentor\Reflection\DocBlockFactoryInterface;
use phpDocumentor\Reflection\Types\ContextFactory;
use PhpParser\Parser;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPUnit\Framework\TestCase;
use PhpParser\Parser;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Clock\ClockInterface as PsrClockInterface;
use Psr\Container\ContainerInterface as PsrContainerInterface;
Expand All @@ -33,10 +31,10 @@
use Symfony\Bundle\FrameworkBundle\Routing\RouteLoaderInterface;
use Symfony\Bundle\FullStack;
use Symfony\Bundle\MercureBundle\MercureBundle;
use Symfony\Component\Asset\PackageInterface;
use Symfony\Component\AssetMapper\AssetMapper;
use Symfony\Component\AssetMapper\Compiler\AssetCompilerInterface;
use Symfony\Component\AssetMapper\Compressor\CompressorInterface;
use Symfony\Component\Asset\PackageInterface;
use Symfony\Component\BrowserKit\AbstractBrowser;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
Expand All @@ -49,9 +47,9 @@
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\ResourceCheckerInterface;
use Symfony\Component\Config\Resource\DirectoryResource;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Config\ResourceCheckerInterface;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -208,15 +206,16 @@
use Symfony\Component\Uid\Factory\UuidFactory;
use Symfony\Component\Uid\UuidV4;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\ExpressionLanguageProvider;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Symfony\Component\Validator\Constraints\ExpressionLanguageProvider;
use Symfony\Component\Validator\GroupProviderInterface;
use Symfony\Component\Validator\Mapping\Loader\PropertyInfoLoader;
use Symfony\Component\Validator\ObjectInitializerInterface;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Webhook\Controller\WebhookController;
use Symfony\Component\WebLink\HttpHeaderSerializer;
use Symfony\Component\Webhook\Controller\WebhookController;
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 All @@ -229,6 +228,8 @@
use Symfony\Contracts\Service\ResetInterface;
use Symfony\Contracts\Service\ServiceSubscriberInterface;
use Symfony\Contracts\Translation\LocaleAwareInterface;
use phpDocumentor\Reflection\DocBlockFactoryInterface;
use phpDocumentor\Reflection\Types\ContextFactory;

/**
* Process the configuration and prepare the dependency injection container with
Expand Down Expand Up @@ -1074,6 +1075,14 @@ 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, [
'$place' => $arc['place'],
'$weight' => $arc['weight'] ?? 1,
]);
}
}
if ('workflow' === $type) {
$transitionDefinition = new Definition(Workflow\Transition::class, [$transition['name'], $transition['from'], $transition['to']]);
$transitionId = \sprintf('.%s.transition.%s', $workflowId, $transitionCounter++);
Expand All @@ -1095,7 +1104,7 @@ 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]);
$transitionDefinition = new Definition(Workflow\Transition::class, [$transition['name'], [$from], [$to]]);
$transitionId = \sprintf('.%s.transition.%s', $workflowId, $transitionCounter++);
$container->setDefinition($transitionId, $transitionDefinition);
$transitions[] = new Reference($transitionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,14 +502,22 @@

<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" use="required" />
</xsd:complexType>

<xsd:complexType name="arc">
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="weight" type="xsd:integer" use="optional" />
Copy link
Member

Choose a reason for hiding this comment

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

use="optional" is the default and we generally don't put it explicitly in our schemas

</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