Skip to content

[FrameworkBundle] Add support for configuring workflow places with a BackedEnum #60204

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 configuring workflow places with a `BackedEnum`

7.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,11 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode): void
->beforeNormalization()
->always()
->then(function ($places) {
// Hack for XML
if (is_a($places, \BackedEnum::class, true)) {
$places = $places::cases();
}

if (!\is_array($places)) {
throw new InvalidConfigurationException('The "places" option must be an array in workflow configuration.');
}
Expand All @@ -495,6 +500,11 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode): void
if (\is_array($place) && \array_key_exists('name', $place)) {
continue;
}
if ($place instanceof \BackedEnum) {
$places[$name] = ['name' => $place->value];
continue;
}

$place['name'] = $name;
$places[$name] = $place;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@
<xsd:attribute name="initial-marking" type="xsd:string" />
<xsd:attribute name="support-strategy" type="xsd:string" />
<xsd:attribute name="enabled" type="xsd:boolean" />
<xsd:attribute name="places" type="xsd:string" />
</xsd:complexType>

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

namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Fixtures\Workflow;

enum Places: string
{
case A = 'a';
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 another PR, I want add to introduce an attribute to configure the Metadata. What would be the name of such attribute? #[WorkflowMetadata] / #[AsWorkflowMetadata]?

case B = 'b';
case C = 'c';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Fixtures\Workflow\Places;
use Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTestCase;

$container->loadFromExtension('framework', [
'workflows' => [
'enum' => [
'supports' => [
FrameworkExtensionTestCase::class,
],
'places' => Places::cases(),
'transitions' => [
'one' => [
'from' => Places::A->value,
'to' => Places::B->value,
Comment on lines +15 to +16
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
'from' => Places::A->value,
'to' => Places::B->value,
'from' => Places::A,
'to' => Places::B,

In a following PR ?

Copy link
Member Author

Choose a reason for hiding this comment

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

May be 🤞🏼

],
'two' => [
'from' => Places::B->value,
'to' => Places::C->value,
],
],
]
],
]);
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Fixtures\Workflow\Places;
use Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTestCase;

$container->loadFromExtension('framework', [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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 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:workflow name="enum" type="state_machine" places="Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Fixtures\Workflow\Places">
<framework:marking-store service="workflow_service"/>
<framework:support>Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTestCase</framework:support>
<framework:transition name="one">
<framework:from>a</framework:from>
<framework:to>b</framework:to>
</framework:transition>
<framework:transition name="two">
<framework:from>b</framework:from>
<framework:to>c</framework:to>
</framework:transition>
</framework:workflow>
</framework:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
framework:
workflows:
enum:
supports:
- Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTestCase
places: !php/enum Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Fixtures\Workflow\Places
transitions:
one:
from: !php/enum Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Fixtures\Workflow\Places::A->value
to: !php/enum Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Fixtures\Workflow\Places::B->value
two:
from: !php/enum Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Fixtures\Workflow\Places::B->value
to: !php/enum Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Fixtures\Workflow\Places::C->value
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,14 @@ public function testWorkflowMultipleTransitionsWithSameName()
], $container->getDefinition($transitions[4])->getArguments());
}

public function testWorkflowEnum()
{
$container = $this->createContainerFromFile('workflow_enum');

$workflowDefinition = $container->getDefinition('state_machine.enum.definition');
$this->assertSame(['a', 'b', 'c'], $workflowDefinition->getArgument(0));
}

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