Skip to content

Commit

Permalink
feature #202 Remove winzou state machine dependency (loic425)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.7-dev branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Bug fix?        | no
| New feature?    |yes
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | 
| License         | MIT


Commits
-------

7bb221d Remove winzou state machine dependency
  • Loading branch information
pamil authored Nov 5, 2020
2 parents d244cf0 + 7bb221d commit 81618a8
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 38 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jobs:
-
name: Restrict winzou/state-machine-bundle version
if: matrix.winzou-version != ''
run: composer require "winzou/state-machine-bundle:${{ matrix.winzou-version }}" --no-update --no-scripts
run: composer require --dev "winzou/state-machine-bundle:${{ matrix.winzou-version }}" --no-update --no-scripts

-
name: Restrict twig/twig version
Expand Down Expand Up @@ -100,7 +100,13 @@ jobs:
run: composer test

-
name: Run smoke tests without optional dependencies
name: Run smoke tests without friendsofsymfony/rest-bundle willdurand/hateoas-bundle jms/serializer-bundle packages
run: |
composer remove friendsofsymfony/rest-bundle willdurand/hateoas-bundle jms/serializer-bundle --dev --no-scripts
(cd src/Bundle/test && app/console cache:clear --env=test_without_fosrest)
-
name: Run smoke tests without winzou/state-machine-bundle packages
run: |
composer remove winzou/state-machine-bundle --dev --no-scripts
(cd src/Bundle/test && app/console cache:clear --env=test_without_state_machine)
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@
"symfony/twig-bundle": "^4.4 || ^5.1",
"symfony/validator": "^4.4 || ^5.1",
"symfony/yaml": "^4.4 || ^5.1",
"webmozart/assert": "^1.8",
"winzou/state-machine-bundle": "^0.3.2 || ^0.4.3 || ^0.5"
"webmozart/assert": "^1.8"
},
"replace": {
"sylius/resource": "self.version"
},
"conflict": {
"friendsofsymfony/rest-bundle": "<3.0 >=4.0",
"jms/serializer-bundle": "<3.5 >=4.0",
"willdurand/hateoas-bundle": "<2.0 >=3.0"
"willdurand/hateoas-bundle": "<2.0 >=3.0",
"winzou/state-machine-bundle": "<0.3.2 || >=0.4.0 <0.4.3"
},
"require-dev": {
"doctrine/orm": "^2.5",
Expand All @@ -70,7 +70,8 @@
"symfony/dependency-injection": "^4.4 || ^5.1",
"twig/twig": "^2.12 || ^3.0",
"vimeo/psalm": "3.17.1",
"willdurand/hateoas-bundle": "^2.0"
"willdurand/hateoas-bundle": "^2.0",
"winzou/state-machine-bundle": "^0.3.2 || ^0.4.3 || ^0.5"
},
"suggest": {
"doctrine/orm": "^2.5",
Expand Down
1 change: 1 addition & 0 deletions src/Bundle/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/test/app/cache
/test/app/db.sql
/test/app/logs
/test/config/db.sql
/test/var
19 changes: 15 additions & 4 deletions src/Bundle/Controller/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class ResourceController
/** @var EventDispatcherInterface */
protected $eventDispatcher;

/** @var StateMachineInterface */
/** @var StateMachineInterface|null */
protected $stateMachine;

/** @var ResourceUpdateHandlerInterface */
Expand All @@ -103,7 +103,7 @@ public function __construct(
FlashHelperInterface $flashHelper,
AuthorizationCheckerInterface $authorizationChecker,
EventDispatcherInterface $eventDispatcher,
StateMachineInterface $stateMachine,
?StateMachineInterface $stateMachine,
ResourceUpdateHandlerInterface $resourceUpdateHandler,
ResourceDeleteHandlerInterface $resourceDeleteHandler
) {
Expand Down Expand Up @@ -198,7 +198,8 @@ public function createAction(Request $request): Response
}

if ($configuration->hasStateMachine()) {
$this->stateMachine->apply($configuration, $newResource);
$stateMachine = $this->getStateMachine();
$stateMachine->apply($configuration, $newResource);
}

$this->repository->add($newResource);
Expand Down Expand Up @@ -450,6 +451,7 @@ public function bulkDeleteAction(Request $request): Response

public function applyStateMachineTransitionAction(Request $request): Response
{
$stateMachine = $this->getStateMachine();
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request);

$this->isGrantedOr403($configuration, ResourceActions::UPDATE);
Expand All @@ -475,7 +477,7 @@ public function applyStateMachineTransitionAction(Request $request): Response
return $this->redirectHandler->redirectToResource($configuration, $resource);
}

if (!$this->stateMachine->can($configuration, $resource)) {
if (!$stateMachine->can($configuration, $resource)) {
throw new BadRequestHttpException();
}

Expand Down Expand Up @@ -570,4 +572,13 @@ protected function createRestView(RequestConfiguration $configuration, $data, in

return $this->viewHandler->handle($configuration, $view);
}

protected function getStateMachine(): StateMachineInterface
{
if (null === $this->stateMachine) {
throw new \LogicException('You can not use the "state-machine" if Winzou State Machine Bundle is not available. Try running "composer require winzou/state-machine-bundle".');
}

return $this->stateMachine;
}
}
6 changes: 3 additions & 3 deletions src/Bundle/Controller/ResourceUpdateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

final class ResourceUpdateHandler implements ResourceUpdateHandlerInterface
{
/** @var StateMachineInterface */
/** @var StateMachineInterface|null */
private $stateMachine;

public function __construct(StateMachineInterface $stateMachine)
public function __construct(?StateMachineInterface $stateMachine)
{
$this->stateMachine = $stateMachine;
}
Expand All @@ -31,7 +31,7 @@ public function handle(
RequestConfiguration $requestConfiguration,
ObjectManager $manager
): void {
if ($requestConfiguration->hasStateMachine()) {
if (null !== $this->stateMachine && $requestConfiguration->hasStateMachine()) {
$this->stateMachine->apply($requestConfiguration, $resource);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* 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\ResourceBundle\DependencyInjection\Compiler;

use Sylius\Bundle\ResourceBundle\Controller\StateMachine;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use winzou\Bundle\StateMachineBundle\winzouStateMachineBundle;

final class RegisterStateMachinePass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container): void
{
$bundles = $container->getParameter('kernel.bundles');
$winzouStateMachineEnabled = in_array(winzouStateMachineBundle::class, $bundles, true);

if (!$winzouStateMachineEnabled) {
return;
}

$stateMachineDefinition = $container->register('sylius.resource_controller.state_machine', StateMachine::class);
$stateMachineDefinition->setPublic(false);
$stateMachineDefinition->addArgument(new Reference('sm.factory'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use SM\Factory\FactoryInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use winzou\Bundle\StateMachineBundle\winzouStateMachineBundle;

/**
* Marks WinzouStateMachineBundle's services as public for compatibility with both Symfony 3.4 and 4.0+.
Expand All @@ -30,6 +31,13 @@ final class WinzouStateMachinePass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
$bundles = $container->getParameter('kernel.bundles');
$winzouStateMachineEnabled = in_array(winzouStateMachineBundle::class, $bundles, true);

if (!$winzouStateMachineEnabled) {
return;
}

if ($container->hasDefinition('sm.factory') && !$container->hasDefinition(FactoryInterface::class)) {
$container->setAlias(FactoryInterface::class, 'sm.factory');
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Bundle/DependencyInjection/Driver/AbstractDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected function addController(ContainerBuilder $container, MetadataInterface
new Reference('sylius.resource_controller.flash_helper'),
new Reference('sylius.resource_controller.authorization_checker'),
new Reference('sylius.resource_controller.event_dispatcher'),
new Reference('sylius.resource_controller.state_machine'),
new Reference('sylius.resource_controller.state_machine', ContainerInterface::NULL_ON_INVALID_REFERENCE),
new Reference('sylius.resource_controller.resource_update_handler'),
new Reference('sylius.resource_controller.resource_delete_handler'),
])
Expand Down
5 changes: 1 addition & 4 deletions src/Bundle/Resources/config/services/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,8 @@
<service id="sylius.resource_controller.view_handler" class="Sylius\Bundle\ResourceBundle\Controller\ViewHandler" public="false">
<argument type="service" id="fos_rest.view_handler" on-invalid="null" />
</service>
<service id="sylius.resource_controller.state_machine" class="Sylius\Bundle\ResourceBundle\Controller\StateMachine" public="false">
<argument type="service" id="sm.factory" />
</service>
<service id="sylius.resource_controller.resource_update_handler" class="Sylius\Bundle\ResourceBundle\Controller\ResourceUpdateHandler" public="false">
<argument type="service" id="sylius.resource_controller.state_machine" />
<argument type="service" id="sylius.resource_controller.state_machine" on-invalid="null" />
</service>
<service id="sylius.resource_controller.resource_delete_handler" class="Sylius\Bundle\ResourceBundle\Controller\ResourceDeleteHandler" public="false" />
</services>
Expand Down
2 changes: 2 additions & 0 deletions src/Bundle/SyliusResourceBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterFormBuilderPass;
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterResourceRepositoryPass;
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterResourcesPass;
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterStateMachinePass;
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\WinzouStateMachinePass;
use Sylius\Bundle\ResourceBundle\DependencyInjection\PagerfantaExtension;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
Expand All @@ -38,6 +39,7 @@ public function build(ContainerBuilder $container): void
parent::build($container);

$container->addCompilerPass(new WinzouStateMachinePass());
$container->addCompilerPass(new RegisterStateMachinePass());
$container->addCompilerPass(new RegisterResourcesPass());
$container->addCompilerPass(new DoctrineTargetEntitiesResolverPass(new TargetEntitiesResolver()), PassConfig::TYPE_BEFORE_OPTIMIZATION, 1);
$container->addCompilerPass(new RegisterResourceRepositoryPass());
Expand Down
26 changes: 6 additions & 20 deletions src/Bundle/test/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,14 @@

class AppKernel extends Kernel
{
public function registerBundles()
public function registerBundles(): iterable
{
$bundles = [
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new Sylius\Bundle\ResourceBundle\SyliusResourceBundle(),
new BabDev\PagerfantaBundle\BabDevPagerfantaBundle(),
new Symfony\Bundle\TwigBundle\TwigBundle(),
new winzou\Bundle\StateMachineBundle\winzouStateMachineBundle(),
new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),
new AppBundle\AppBundle(),
];

if ($this->getEnvironment() !== 'test_without_fosrest') {
$bundles[] = new FOS\RestBundle\FOSRestBundle();
$bundles[] = new JMS\SerializerBundle\JMSSerializerBundle();
$bundles[] = new Bazinga\Bundle\HateoasBundle\BazingaHateoasBundle();
$bundles[] = new Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundle();
$bundles[] = new Nelmio\Alice\Bridge\Symfony\NelmioAliceBundle();
$contents = require $this->getProjectDir() . '/config/bundles.php';
foreach ($contents as $class => $envs) {
if ($envs[$this->environment] ?? $envs['all'] ?? false) {
yield new $class();
}
}

return $bundles;
}

public function registerContainerConfiguration(LoaderInterface $loader): void
Expand Down
28 changes: 28 additions & 0 deletions src/Bundle/test/config/bundles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
Sylius\Bundle\ResourceBundle\SyliusResourceBundle::class => ['all' => true],
BabDev\PagerfantaBundle\BabDevPagerfantaBundle::class => ['all' => true],
Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle::class => ['all' => true],
AppBundle\AppBundle::class => ['all' => true],
FOS\RestBundle\FOSRestBundle::class => ['test' => true],
JMS\SerializerBundle\JMSSerializerBundle::class => ['test' => true],
Bazinga\Bundle\HateoasBundle\BazingaHateoasBundle::class => ['test' => true],
Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundle::class => ['test' => true],
Nelmio\Alice\Bridge\Symfony\NelmioAliceBundle::class => ['test' => true],
winzou\Bundle\StateMachineBundle\winzouStateMachineBundle::class => ['test' => true, 'test_without_fosrest' => true],
];
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use SM\Callback\CallbackFactoryInterface;
use SM\Callback\CascadeTransitionCallback;
use SM\Factory\FactoryInterface;
use SM\StateMachine\StateMachineInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
Expand All @@ -26,6 +27,10 @@ final class WinzouStateMachinePassTest extends WebTestCase
/** @test */
public function all_winzou_services_are_public(): void
{
if (!class_exists(StateMachineInterface::class)) {
$this->markTestSkipped('State machine is not installed');
}

/** @var ContainerBuilder $container */
$container = self::createClient()->getContainer();

Expand Down

0 comments on commit 81618a8

Please sign in to comment.