-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Provide the State Machine abstraction #15315
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
dabc351
Install the symfony/workflow package
jakubtobiasz c3b9c9d
Create an interface for Sylius' state machine abstraction
jakubtobiasz d3c0f33
Add a composite state machine
jakubtobiasz 21a9b60
Add a Symfony Workflow state machine adapter
jakubtobiasz 5621c84
Add a WinzouStateMachine state machine adapter
jakubtobiasz bc6aa9d
Add missing tags on state machine adapters
jakubtobiasz 5db98bb
Creating models for testing the state machine abstraction
jakubtobiasz af39f9a
fixup! Creating models for testing the state machine abstraction
jakubtobiasz 1f964a3
Add functional tests checking whether CompositeStateMachine works cor…
jakubtobiasz 99dc3fa
Add checking whether any state machine adapter is passed to the compo…
jakubtobiasz 2d643dc
Apply ECS fixes
jakubtobiasz 7f1615a
Remove `use` statements for the classes from the global namespace
jakubtobiasz 24a7ab0
Replace the null value for $lastException with an empty exception
jakubtobiasz b6a9c62
Improve handling of exceptions in the composite state machine
jakubtobiasz 67ef56f
Add translating of Symfony Workflow's exceptions to the abstracted one
jakubtobiasz c144914
Add translating of WinzouStateMachine's exceptions to the abstracted one
jakubtobiasz 9e4dff0
Rename the `getEnabledTransition` method to `getEnabledTransitions`
jakubtobiasz ff234f2
Add Twig Functions for the new State Machine abstraction
jakubtobiasz 00c3422
Apply ECS fixes
jakubtobiasz 09b70b9
Add a missing array typing
jakubtobiasz 354dda0
Silence the error telling Symfony\Component\Workflow\Registry::get is…
jakubtobiasz cf2a719
Change expected exception to the correct one
jakubtobiasz 8a3df8a
Enable Symfony Workflow in AdminBundle and ApiBundle
jakubtobiasz 83c09f8
Allow configuring a default state machine adapter and a per graph ada…
jakubtobiasz 258ee8b
Make use of configurable state machine adapters in the composite stat…
jakubtobiasz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
framework: | ||
workflows: ~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ framework: | |
test: ~ | ||
mailer: | ||
dsn: 'null://null' | ||
workflows: ~ | ||
|
||
security: | ||
firewalls: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/Sylius/Bundle/CoreBundle/Resources/config/services/state_machines.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<!-- | ||
|
||
This file is part of the Sylius package. | ||
|
||
(c) Sylius Sp. z o.o. | ||
|
||
For the full copyright and license information, please view the LICENSE | ||
file that was distributed with this source code. | ||
|
||
--> | ||
|
||
<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://symfony.com/schema/dic/services" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<defaults public="true" /> | ||
|
||
<service id="sylius.state_machine.composite" class="Sylius\Bundle\CoreBundle\StateMachine\CompositeStateMachine"> | ||
<argument type="tagged_iterator" tag="sylius.state_machine" index-by="key" /> | ||
<argument>%sylius_core.state_machine.default_adapter%</argument> | ||
<argument>%sylius_core.state_machine.graphs_to_adapters_mapping%</argument> | ||
</service> | ||
<service id="Sylius\Bundle\CoreBundle\StateMachine\StateMachineInterface $compositeStateMachine" alias="sylius.state_machine.composite" /> | ||
|
||
<service id="sylius.state_machine" alias="sylius.state_machine.composite" /> | ||
<service id="Sylius\Bundle\CoreBundle\StateMachine\StateMachineInterface" alias="sylius.state_machine" /> | ||
|
||
<service id="sylius.state_machine.adapter.symfony_workflow" class="Sylius\Bundle\CoreBundle\StateMachine\SymfonyWorkflowAdapter"> | ||
<argument type="service" id="workflow.registry" /> | ||
|
||
<tag name="sylius.state_machine" key="symfony_workflow" /> | ||
</service> | ||
<service id="Sylius\Bundle\CoreBundle\StateMachine\StateMachineInterface $symfonyWorkflow" alias="sylius.state_machine.adapter.symfony_workflow" /> | ||
|
||
<service id="sylius.state_machine.adapter.winzou_state_machine" class="Sylius\Bundle\CoreBundle\StateMachine\WinzouStateMachineAdapter"> | ||
<argument type="service" id="sm.factory" /> | ||
|
||
<tag name="sylius.state_machine" key="winzou_state_machine" /> | ||
</service> | ||
<service id="Sylius\Bundle\CoreBundle\StateMachine\StateMachineInterface $winzouStateMachine" alias="sylius.state_machine.adapter.winzou_state_machine" /> | ||
</services> | ||
</container> |
74 changes: 74 additions & 0 deletions
74
src/Sylius/Bundle/CoreBundle/StateMachine/CompositeStateMachine.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\CoreBundle\StateMachine; | ||
|
||
use Traversable; | ||
use Webmozart\Assert\Assert; | ||
|
||
class CompositeStateMachine implements StateMachineInterface | ||
{ | ||
/** @var array<StateMachineInterface> */ | ||
private array $stateMachineAdapters; | ||
jakubtobiasz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* @param iterable<StateMachineInterface> $stateMachineAdapters | ||
* @param array<string, string> $graphsToAdaptersMapping | ||
*/ | ||
public function __construct( | ||
iterable $stateMachineAdapters, | ||
private string $defaultAdapter, | ||
private array $graphsToAdaptersMapping, | ||
) { | ||
Assert::notEmpty($stateMachineAdapters, 'At least one state machine adapter should be provided.'); | ||
Assert::allIsInstanceOf( | ||
$stateMachineAdapters, | ||
StateMachineInterface::class, | ||
sprintf('All state machine adapters should implement the "%s" interface.', StateMachineInterface::class), | ||
); | ||
$this->stateMachineAdapters = $stateMachineAdapters instanceof Traversable ? iterator_to_array($stateMachineAdapters) : $stateMachineAdapters; | ||
} | ||
|
||
/** | ||
* @throws \Exception | ||
*/ | ||
public function can(object $subject, string $graphName, string $transition): bool | ||
{ | ||
return $this->getStateMachineAdapter($graphName)->can($subject, $graphName, $transition); | ||
} | ||
|
||
/** | ||
* @throws \Exception | ||
*/ | ||
public function apply(object $subject, string $graphName, string $transition, array $context = []): void | ||
{ | ||
$this->getStateMachineAdapter($graphName)->apply($subject, $graphName, $transition, $context); | ||
} | ||
|
||
/** | ||
* @throws \Exception | ||
*/ | ||
public function getEnabledTransitions(object $subject, string $graphName): array | ||
{ | ||
return $this->getStateMachineAdapter($graphName)->getEnabledTransitions($subject, $graphName); | ||
} | ||
|
||
private function getStateMachineAdapter(string $graphName): StateMachineInterface | ||
{ | ||
if (isset($this->graphsToAdaptersMapping[$graphName])) { | ||
return $this->stateMachineAdapters[$this->graphsToAdaptersMapping[$graphName]]; | ||
} | ||
|
||
return $this->stateMachineAdapters[$this->defaultAdapter]; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Sylius/Bundle/CoreBundle/StateMachine/Exception/StateMachineExecutionException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\CoreBundle\StateMachine\Exception; | ||
|
||
class StateMachineExecutionException extends \Exception | ||
{ | ||
} |
38 changes: 38 additions & 0 deletions
38
src/Sylius/Bundle/CoreBundle/StateMachine/StateMachineInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\CoreBundle\StateMachine; | ||
|
||
use Sylius\Bundle\CoreBundle\StateMachine\Exception\StateMachineExecutionException; | ||
|
||
interface StateMachineInterface | ||
{ | ||
/** | ||
* @throws StateMachineExecutionException | ||
*/ | ||
public function can(object $subject, string $graphName, string $transition): bool; | ||
|
||
/** | ||
* @param array<string, mixed> $context | ||
* | ||
* @throws StateMachineExecutionException | ||
*/ | ||
public function apply(object $subject, string $graphName, string $transition, array $context = []): void; | ||
|
||
/** | ||
* @throws StateMachineExecutionException | ||
* | ||
* @return array<TransitionInterface> | ||
*/ | ||
public function getEnabledTransitions(object $subject, string $graphName): array; | ||
} |
64 changes: 64 additions & 0 deletions
64
src/Sylius/Bundle/CoreBundle/StateMachine/SymfonyWorkflowAdapter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\CoreBundle\StateMachine; | ||
|
||
use Sylius\Bundle\CoreBundle\StateMachine\Exception\StateMachineExecutionException; | ||
use Symfony\Component\Workflow\Exception\InvalidArgumentException; | ||
use Symfony\Component\Workflow\Registry; | ||
use Symfony\Component\Workflow\Transition as SymfonyWorkflowTransition; | ||
|
||
final class SymfonyWorkflowAdapter implements StateMachineInterface | ||
{ | ||
public function __construct(private Registry $symfonyWorkflowRegistry) | ||
{ | ||
} | ||
|
||
public function can(object $subject, string $graphName, string $transition): bool | ||
{ | ||
try { | ||
return $this->symfonyWorkflowRegistry->get($subject, $graphName)->can($subject, $transition); | ||
} catch (InvalidArgumentException $exception) { | ||
throw new StateMachineExecutionException($exception->getMessage(), $exception->getCode(), $exception); | ||
} | ||
} | ||
|
||
public function apply(object $subject, string $graphName, string $transition, array $context = []): void | ||
{ | ||
try { | ||
$this->symfonyWorkflowRegistry->get($subject, $graphName)->apply($subject, $transition, $context); | ||
} catch (InvalidArgumentException $exception) { | ||
throw new StateMachineExecutionException($exception->getMessage(), $exception->getCode(), $exception); | ||
} | ||
} | ||
|
||
public function getEnabledTransitions(object $subject, string $graphName): array | ||
{ | ||
try { | ||
$enabledTransitions = $this->symfonyWorkflowRegistry->get($subject, $graphName)->getEnabledTransitions($subject); | ||
} catch (InvalidArgumentException $exception) { | ||
throw new StateMachineExecutionException($exception->getMessage(), $exception->getCode(), $exception); | ||
} | ||
|
||
return array_map( | ||
function (SymfonyWorkflowTransition $transition): TransitionInterface { | ||
return new Transition( | ||
$transition->getName(), | ||
$transition->getFroms(), | ||
$transition->getTos(), | ||
); | ||
}, | ||
$enabledTransitions, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\CoreBundle\StateMachine; | ||
|
||
final class Transition implements TransitionInterface | ||
{ | ||
/** | ||
* @param array<string>|null $froms | ||
* @param array<string>|null $tos | ||
*/ | ||
public function __construct( | ||
private string $name, | ||
private ?array $froms, | ||
private ?array $tos, | ||
) { | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function getFroms(): ?array | ||
{ | ||
return $this->froms; | ||
} | ||
|
||
public function getTos(): ?array | ||
{ | ||
return $this->tos; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Sylius/Bundle/CoreBundle/StateMachine/TransitionInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\CoreBundle\StateMachine; | ||
|
||
interface TransitionInterface | ||
{ | ||
public function getName(): string; | ||
|
||
/** | ||
* @return array<string>|null | ||
*/ | ||
public function getFroms(): ?array; | ||
|
||
/** | ||
* @return array<string>|null | ||
*/ | ||
public function getTos(): ?array; | ||
Comment on lines
+25
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We want to be compatible with Workflow, so despite Winzou allows to return only 1 "to", we return an array to |
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we shouldn't deprecate this config immediately after adding it