Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 7 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ install:
- (cd src/Component && composer update --prefer-dist --no-scripts)

before_script:
- (cd src/Bundle/test && app/console doctrine:database:create)
- (cd src/Bundle/test && app/console doctrine:schema:create)
- (cd src/Bundle/test && bin/console doctrine:database:create)
- (cd src/Bundle/test && bin/console doctrine:schema:create)

script:
- composer validate --strict
Expand All @@ -61,4 +61,8 @@ script:

# Test the whole bundle without FOSRestBundle dependency
- 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)
- (cd src/Bundle/test && bin/console cache:clear --env=test_without_fosrest)

# Test the whole bundle without WinzouStateMachineBundle dependency
- composer remove winzou/state-machine-bundle --dev --no-scripts
- (cd src/Bundle/test && bin/console cache:clear --env=test_without_state_machine)
17 changes: 8 additions & 9 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,9 @@
"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",
"symfony/dotenv": "^4.4 || ^5.1"
},
"suggest": {
"doctrine/orm": "^2.5",
Expand All @@ -94,11 +96,8 @@
"psr-4": {
"Sylius\\Bundle\\ResourceBundle\\spec\\": "src/Bundle/spec/",
"Sylius\\Component\\Resource\\spec\\": "src/Component/spec/",
"AppBundle\\": "src/Bundle/test/src/AppBundle/"
},
"classmap": [
"src/Bundle/test/app/AppKernel.php"
]
"App\\": "src/Bundle/test/src/"
}
},
"scripts": {
"analyse": [
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<php>
<ini name="error_reporting" value="-1" />

<server name="KERNEL_CLASS" value="AppKernel" />
<server name="KERNEL_CLASS" value="App\Kernel" />
<server name="IS_DOCTRINE_ORM_SUPPORTED" value="true" />
<server name="SHELL_VERBOSITY" value="-1" />
</php>
Expand Down
19 changes: 15 additions & 4 deletions src/Bundle/Controller/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class ResourceController extends AbstractController
/** @var EventDispatcherInterface */
protected $eventDispatcher;

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

/** @var ResourceUpdateHandlerInterface */
Expand All @@ -99,7 +99,7 @@ public function __construct(
FlashHelperInterface $flashHelper,
AuthorizationCheckerInterface $authorizationChecker,
EventDispatcherInterface $eventDispatcher,
StateMachineInterface $stateMachine,
?StateMachineInterface $stateMachine,
ResourceUpdateHandlerInterface $resourceUpdateHandler,
ResourceDeleteHandlerInterface $resourceDeleteHandler
) {
Expand Down Expand Up @@ -194,7 +194,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 @@ -446,6 +447,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 @@ -471,7 +473,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 @@ -550,4 +552,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 @@ -78,7 +78,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
1 change: 1 addition & 0 deletions src/Bundle/test/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
APP_ENV=test
48 changes: 0 additions & 48 deletions src/Bundle/test/app/AppKernel.php

This file was deleted.

62 changes: 0 additions & 62 deletions src/Bundle/test/app/config/config.yml

This file was deleted.

6 changes: 0 additions & 6 deletions src/Bundle/test/app/config/parameters.yml

This file was deleted.

Loading