From 13f32e1ad9b4cb9a37187e65abfa7758cd3d6000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Sat, 12 Apr 2025 12:44:38 +0200 Subject: [PATCH] [FrameworkBundle] Add support for configuring workflow places with a `BackedEnum` --- .../Bundle/FrameworkBundle/CHANGELOG.md | 1 + .../DependencyInjection/Configuration.php | 10 ++++++++ .../Resources/config/schema/symfony-1.0.xsd | 1 + .../Fixtures/Workflow/Places.php | 10 ++++++++ .../Fixtures/php/workflow_enum.php | 25 +++++++++++++++++++ .../Fixtures/php/workflows.php | 1 + .../Fixtures/xml/workflow_enum.xml | 23 +++++++++++++++++ .../Fixtures/yml/workflow_enum.yml | 13 ++++++++++ .../FrameworkExtensionTestCase.php | 8 ++++++ 9 files changed, 92 insertions(+) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/Workflow/Places.php create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/workflow_enum.php create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/workflow_enum.xml create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/workflow_enum.yml diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index 8e70fb98e42fe..942d042b4d7ae 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -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 --- diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index 6b168a2d4a0fd..7effce14899bd 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -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.'); } @@ -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; } diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd b/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd index c4ee3486dae87..5969a13c3427c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd @@ -460,6 +460,7 @@ + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/Workflow/Places.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/Workflow/Places.php new file mode 100644 index 0000000000000..47a6b5a4c0c73 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/Workflow/Places.php @@ -0,0 +1,10 @@ +loadFromExtension('framework', [ + 'workflows' => [ + 'enum' => [ + 'supports' => [ + FrameworkExtensionTestCase::class, + ], + 'places' => Places::cases(), + 'transitions' => [ + 'one' => [ + 'from' => Places::A->value, + 'to' => Places::B->value, + ], + 'two' => [ + 'from' => Places::B->value, + 'to' => Places::C->value, + ], + ], + ] + ], +]); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/workflows.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/workflows.php index 118a627c7c05b..e3a04694c19a8 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/workflows.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/workflows.php @@ -1,5 +1,6 @@ loadFromExtension('framework', [ diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/workflow_enum.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/workflow_enum.xml new file mode 100644 index 0000000000000..cc784c4183a60 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/workflow_enum.xml @@ -0,0 +1,23 @@ + + + + + + + + Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTestCase + + a + b + + + b + c + + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/workflow_enum.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/workflow_enum.yml new file mode 100644 index 0000000000000..9c880d0378876 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/workflow_enum.yml @@ -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 diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php index d942c122c826a..01305daaf4254 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php @@ -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');