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 all 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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: ~
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
<errorLevel type="info">
<referencedMethod name="PHPUnit\Framework\TestCase::__construct" />
<referencedMethod name="Symfony\Bundle\SecurityBundle\Security\_FirewallMap::getFirewallConfig" />
<referencedMethod name="Symfony\Component\Workflow\Registry::get" />
<referencedMethod name="ApiPlatform\Core\Util\RequestParser::getQueryString" />
<referencedMethod name="ApiPlatform\Util\RequestParser::getQueryString" />
<referencedMethod name="ApiPlatform\Core\Util\RequestParser::parseRequestParams" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ framework:
test: ~
mailer:
dsn: 'null://null'
workflows: ~

security:
firewalls:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ framework:
paths: ['%kernel.project_dir%/config/serialization']
mailer:
dsn: 'null://null'
workflows: ~

doctrine_migrations:
storage:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()
->end()
->arrayNode('state_machine')
->addDefaultsIfNotSet()
->children()
->scalarNode('default_adapter')->defaultValue('winzou_state_machine')->end()
->arrayNode('graphs_to_adapters_mapping')
->useAttributeAsKey('graph_name')
->scalarPrototype()->end()
->end()
->end()
->end()
Comment on lines +101 to +110
Copy link
Member

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

->end()
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public function load(array $configs, ContainerBuilder $container): void
$container->setParameter('sylius_core.order_by_identifier', $config['order_by_identifier']);
$container->setParameter('sylius_core.catalog_promotions.batch_size', $config['catalog_promotions']['batch_size']);
$container->setParameter('sylius_core.price_history.batch_size', $config['price_history']['batch_size']);
$container->setParameter('sylius_core.state_machine.default_adapter', $config['state_machine']['default_adapter']);
$container->setParameter('sylius_core.state_machine.graphs_to_adapters_mapping', $config['state_machine']['graphs_to_adapters_mapping']);

/** @var string $env */
$env = $container->getParameter('kernel.environment');
Expand Down
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,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>
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];
}
}
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
{
}
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;
}
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,
);
}
}
43 changes: 43 additions & 0 deletions src/Sylius/Bundle/CoreBundle/StateMachine/Transition.php
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;
}
}
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.

}
Loading
Loading