Skip to content
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 25 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
dabc351
Install the symfony/workflow package
jakubtobiasz Sep 16, 2023
c3b9c9d
Create an interface for Sylius' state machine abstraction
jakubtobiasz Sep 16, 2023
d3c0f33
Add a composite state machine
jakubtobiasz Sep 16, 2023
21a9b60
Add a Symfony Workflow state machine adapter
jakubtobiasz Sep 16, 2023
5621c84
Add a WinzouStateMachine state machine adapter
jakubtobiasz Sep 16, 2023
bc6aa9d
Add missing tags on state machine adapters
jakubtobiasz Sep 16, 2023
5db98bb
Creating models for testing the state machine abstraction
jakubtobiasz Sep 16, 2023
af39f9a
fixup! Creating models for testing the state machine abstraction
jakubtobiasz Sep 16, 2023
1f964a3
Add functional tests checking whether CompositeStateMachine works cor…
jakubtobiasz Sep 16, 2023
99dc3fa
Add checking whether any state machine adapter is passed to the compo…
jakubtobiasz Sep 16, 2023
2d643dc
Apply ECS fixes
jakubtobiasz Sep 16, 2023
7f1615a
Remove `use` statements for the classes from the global namespace
jakubtobiasz Sep 16, 2023
24a7ab0
Replace the null value for $lastException with an empty exception
jakubtobiasz Sep 16, 2023
b6a9c62
Improve handling of exceptions in the composite state machine
jakubtobiasz Sep 16, 2023
67ef56f
Add translating of Symfony Workflow's exceptions to the abstracted one
jakubtobiasz Sep 16, 2023
c144914
Add translating of WinzouStateMachine's exceptions to the abstracted one
jakubtobiasz Sep 16, 2023
9e4dff0
Rename the `getEnabledTransition` method to `getEnabledTransitions`
jakubtobiasz Sep 16, 2023
ff234f2
Add Twig Functions for the new State Machine abstraction
jakubtobiasz Sep 16, 2023
00c3422
Apply ECS fixes
jakubtobiasz Sep 16, 2023
09b70b9
Add a missing array typing
jakubtobiasz Sep 16, 2023
354dda0
Silence the error telling Symfony\Component\Workflow\Registry::get is…
jakubtobiasz Sep 16, 2023
cf2a719
Change expected exception to the correct one
jakubtobiasz Sep 16, 2023
8a3df8a
Enable Symfony Workflow in AdminBundle and ApiBundle
jakubtobiasz Sep 16, 2023
83c09f8
Allow configuring a default state machine adapter and a per graph ada…
jakubtobiasz Sep 17, 2023
258ee8b
Make use of configurable state machine adapters in the composite stat…
jakubtobiasz Sep 17, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
"symfony/twig-bundle": "^5.4.21 || ^6.0",
"symfony/validator": "^5.4.21 || ^6.0",
"symfony/webpack-encore-bundle": "^1.15",
"symfony/workflow": "^5.4.21 || ^6.0",
"symfony/yaml": "^5.4.21 || ^6.0",
"twig/intl-extra": "^2.12 || ^3.4",
"twig/twig": "^2.12 || ^3.3",
Expand Down
2 changes: 2 additions & 0 deletions config/packages/workflow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
framework:
workflows: ~
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<import resource="services/promotion.xml" />
<import resource="services/providers.xml" />
<import resource="services/shipping.xml" />
<import resource="services/state_machines.xml" />
<import resource="services/state_resolvers.xml" />
<import resource="services/taxation.xml" />
<import resource="services/templating.xml" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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" />
</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" priority="128" />
</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" priority="64" />
</service>
<service id="Sylius\Bundle\CoreBundle\StateMachine\StateMachineInterface $winzouStateMachine" alias="sylius.state_machine.adapter.winzou_state_machine" />
</services>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?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 Exception;
jakubtobiasz marked this conversation as resolved.
Show resolved Hide resolved
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

public function __construct(iterable $stateMachineAdapters)
{
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
{
$lastException = null;

foreach ($this->stateMachineAdapters as $stateMachineAdapter) {
try {
return $stateMachineAdapter->can($subject, $graphName, $transition);
} catch (Exception $exception) {
$lastException = $exception;
}
}

throw $lastException;

Check failure on line 51 in src/Sylius/Bundle/CoreBundle/StateMachine/CompositeStateMachine.php

View workflow job for this annotation

GitHub Actions / Static checks / PHP 8.0, Symfony ^5.4, API Platform ^2.7.10

InvalidThrow

src/Sylius/Bundle/CoreBundle/StateMachine/CompositeStateMachine.php:51:9: InvalidThrow: Cannot throw null as it does not extend Exception or implement Throwable (see https://psalm.dev/133)

Check failure on line 51 in src/Sylius/Bundle/CoreBundle/StateMachine/CompositeStateMachine.php

View workflow job for this annotation

GitHub Actions / Static checks / PHP 8.2, Symfony ^6.0, API Platform ^2.7.10

InvalidThrow

src/Sylius/Bundle/CoreBundle/StateMachine/CompositeStateMachine.php:51:9: InvalidThrow: Cannot throw null as it does not extend Exception or implement Throwable (see https://psalm.dev/133)
}

/**
* @throws Exception
*/
public function apply(object $subject, string $graphName, string $transition, array $context = []): void
{
$lastException = null;

foreach ($this->stateMachineAdapters as $stateMachineAdapter) {
try {
$stateMachineAdapter->apply($subject, $graphName, $transition, $context);

return;
} catch (Exception $exception) {
$lastException = $exception;
}
}

throw $lastException;

Check failure on line 71 in src/Sylius/Bundle/CoreBundle/StateMachine/CompositeStateMachine.php

View workflow job for this annotation

GitHub Actions / Static checks / PHP 8.0, Symfony ^5.4, API Platform ^2.7.10

InvalidThrow

src/Sylius/Bundle/CoreBundle/StateMachine/CompositeStateMachine.php:71:9: InvalidThrow: Cannot throw null as it does not extend Exception or implement Throwable (see https://psalm.dev/133)

Check failure on line 71 in src/Sylius/Bundle/CoreBundle/StateMachine/CompositeStateMachine.php

View workflow job for this annotation

GitHub Actions / Static checks / PHP 8.2, Symfony ^6.0, API Platform ^2.7.10

InvalidThrow

src/Sylius/Bundle/CoreBundle/StateMachine/CompositeStateMachine.php:71:9: InvalidThrow: Cannot throw null as it does not extend Exception or implement Throwable (see https://psalm.dev/133)
}

/**
* @throws Exception
*/
public function getEnabledTransition(object $subject, string $graphName): array
{
$lastException = null;

foreach ($this->stateMachineAdapters as $stateMachineAdapter) {
try {
return $stateMachineAdapter->getEnabledTransition($subject, $graphName);
} catch (Exception $exception) {
$lastException = $exception;
}
}

throw $lastException;

Check failure on line 89 in src/Sylius/Bundle/CoreBundle/StateMachine/CompositeStateMachine.php

View workflow job for this annotation

GitHub Actions / Static checks / PHP 8.0, Symfony ^5.4, API Platform ^2.7.10

InvalidThrow

src/Sylius/Bundle/CoreBundle/StateMachine/CompositeStateMachine.php:89:9: InvalidThrow: Cannot throw null as it does not extend Exception or implement Throwable (see https://psalm.dev/133)

Check failure on line 89 in src/Sylius/Bundle/CoreBundle/StateMachine/CompositeStateMachine.php

View workflow job for this annotation

GitHub Actions / Static checks / PHP 8.2, Symfony ^6.0, API Platform ^2.7.10

InvalidThrow

src/Sylius/Bundle/CoreBundle/StateMachine/CompositeStateMachine.php:89:9: InvalidThrow: Cannot throw null as it does not extend Exception or implement Throwable (see https://psalm.dev/133)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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 StateMachineInterface
{
public function can(object $subject, string $graphName, string $transition): bool;

public function apply(object $subject, string $graphName, string $transition, array $context = []): void;

/**
* @return array<TransitionInterface>
*/
public function getEnabledTransition(object $subject, string $graphName): array;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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 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
{
return $this->symfonyWorkflowRegistry->get($subject, $graphName)->can($subject, $transition);

Check failure on line 27 in src/Sylius/Bundle/CoreBundle/StateMachine/SymfonyWorkflowAdapter.php

View workflow job for this annotation

GitHub Actions / Static checks / PHP 8.2, Symfony ^6.0, API Platform ^2.7.10

InternalMethod

src/Sylius/Bundle/CoreBundle/StateMachine/SymfonyWorkflowAdapter.php:27:48: InternalMethod: The method Symfony\Component\Workflow\Registry::get is internal to Symfony and Symfony but called from Sylius\Bundle\CoreBundle\StateMachine\SymfonyWorkflowAdapter::can (see https://psalm.dev/175)
}

public function apply(object $subject, string $graphName, string $transition, array $context = []): void
{
$this->symfonyWorkflowRegistry->get($subject, $graphName)->apply($subject, $transition, $context);

Check failure on line 32 in src/Sylius/Bundle/CoreBundle/StateMachine/SymfonyWorkflowAdapter.php

View workflow job for this annotation

GitHub Actions / Static checks / PHP 8.2, Symfony ^6.0, API Platform ^2.7.10

InternalMethod

src/Sylius/Bundle/CoreBundle/StateMachine/SymfonyWorkflowAdapter.php:32:41: InternalMethod: The method Symfony\Component\Workflow\Registry::get is internal to Symfony and Symfony but called from Sylius\Bundle\CoreBundle\StateMachine\SymfonyWorkflowAdapter::apply (see https://psalm.dev/175)
}

public function getEnabledTransition(object $subject, string $graphName): array
{
$enabledTransitions = $this->symfonyWorkflowRegistry->get($subject, $graphName)->getEnabledTransitions($subject);

Check failure on line 37 in src/Sylius/Bundle/CoreBundle/StateMachine/SymfonyWorkflowAdapter.php

View workflow job for this annotation

GitHub Actions / Static checks / PHP 8.2, Symfony ^6.0, API Platform ^2.7.10

InternalMethod

src/Sylius/Bundle/CoreBundle/StateMachine/SymfonyWorkflowAdapter.php:37:63: InternalMethod: The method Symfony\Component\Workflow\Registry::get is internal to Symfony and Symfony but called from Sylius\Bundle\CoreBundle\StateMachine\SymfonyWorkflowAdapter::getEnabledTransition (see https://psalm.dev/175)

return array_map(
function (SymfonyWorkflowTransition $transition): TransitionInterface {
return new Transition(
$transition->getName(),
$transition->getFroms(),
$transition->getTos(),
);
},
$enabledTransitions,
);
}
}
39 changes: 39 additions & 0 deletions src/Sylius/Bundle/CoreBundle/StateMachine/Transition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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
{
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;
}
}
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 tos.

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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 SM\Factory\FactoryInterface;

final class WinzouStateMachineAdapter implements StateMachineInterface
{
public function __construct(private FactoryInterface $winzouStateMachineFactory)
{
}

public function can(object $subject, string $graphName, string $transition): bool
{
return $this->getStateMachine($subject, $graphName)->can($transition);
}

public function apply(object $subject, string $graphName, string $transition, array $context = []): void
{
$this->getStateMachine($subject, $graphName)->apply($transition);
}

public function getEnabledTransition(object $subject, string $graphName): array
{
$transitions = $this->getStateMachine($subject, $graphName)->getPossibleTransitions();

return array_map(
fn (string $transition) => new Transition($transition, null, null),
$transitions,
);
}

private function getStateMachine(object $subject, string $graphName): \SM\StateMachine\StateMachineInterface
{
return $this->winzouStateMachineFactory->get($subject, $graphName);
}
}