-
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #613 Configure specific state machine component for a resourc…
…e (loic425) This PR was merged into the 1.11 branch. Discussion ---------- | Q | A | --------------- | ----- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Related tickets | | License | MIT Commits ------- 3da36b9 Configure specific state machine component for a resource 85cad9f Change state_machine to controller_state_machine
- Loading branch information
Showing
8 changed files
with
224 additions
and
2 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/Bundle/DependencyInjection/Compiler/RegisterResourceStateMachinePass.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,51 @@ | ||
<?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 Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
final class RegisterResourceStateMachinePass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
if (!$container->hasParameter('sylius.resources')) { | ||
return; | ||
} | ||
|
||
/** @var array $resources */ | ||
$resources = $container->getParameter('sylius.resources'); | ||
|
||
foreach ($resources as $alias => $configuration) { | ||
[$applicationName, $resourceName] = explode('.', $alias, 2); | ||
$stateMachineId = sprintf('%s.controller_state_machine.%s', $applicationName, $resourceName); | ||
|
||
$stateMachineComponent = $configuration['state_machine_component'] ?? null; | ||
|
||
if (null === $stateMachineComponent) { | ||
$container->setAlias($stateMachineId, 'sylius.resource_controller.state_machine'); | ||
|
||
continue; | ||
} | ||
|
||
$specificStateMachineId = sprintf('sylius.resource_controller.state_machine.%s', $stateMachineComponent); | ||
|
||
if (!$container->hasDefinition($specificStateMachineId)) { | ||
throw new \LogicException(sprintf('State machine "%s" is not available.', $stateMachineComponent)); | ||
} | ||
|
||
$container->setAlias($stateMachineId, $specificStateMachineId); | ||
} | ||
} | ||
} |
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
127 changes: 127 additions & 0 deletions
127
src/Bundle/Tests/DependencyInjection/Compiler/RegisterResourceStateMachinePassTest.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,127 @@ | ||
<?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 DependencyInjection\Compiler; | ||
|
||
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase; | ||
use Sylius\Bundle\ResourceBundle\Controller\StateMachine; | ||
use Sylius\Bundle\ResourceBundle\Controller\Workflow; | ||
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterResourceStateMachinePass; | ||
use Sylius\Bundle\ResourceBundle\Tests\DependencyInjection\Compiler\AuthorClass; | ||
use Sylius\Bundle\ResourceBundle\Tests\DependencyInjection\Compiler\BookClass; | ||
use Sylius\Bundle\ResourceBundle\Tests\DependencyInjection\Compiler\PullRequestClass; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
|
||
final class RegisterResourceStateMachinePassTest extends AbstractCompilerPassTestCase | ||
{ | ||
/** @test */ | ||
public function it_registers_state_machine_components_for_resources(): void | ||
{ | ||
$this->registerService('sylius.resource_controller.state_machine', StateMachine::class); | ||
$this->makeSymfonyWorkflowAvailable(); | ||
$this->makeWinzouStateMachineAvailable(); | ||
|
||
$this->setDefinition('sylius.resource_registry', new Definition()); | ||
|
||
$this->setParameter( | ||
'sylius.resources', | ||
[ | ||
'app.book' => ['state_machine_component' => 'symfony', 'classes' => ['model' => BookClass::class]], | ||
'app.author' => ['state_machine_component' => 'winzou', 'classes' => ['model' => AuthorClass::class]], | ||
'app.pull_request' => ['classes' => ['model' => PullRequestClass::class]], | ||
], | ||
); | ||
|
||
$this->compile(); | ||
|
||
$this->assertContainerBuilderHasAlias('app.controller_state_machine.book', 'sylius.resource_controller.state_machine.symfony'); | ||
$this->assertContainerBuilderHasAlias('app.controller_state_machine.author', 'sylius.resource_controller.state_machine.winzou'); | ||
$this->assertContainerBuilderHasAlias('app.controller_state_machine.pull_request', 'sylius.resource_controller.state_machine'); | ||
} | ||
|
||
/** @test */ | ||
public function it_throws_an_exception_when_specific_symfony_state_machine_component_is_not_available(): void | ||
{ | ||
$this->registerService('sylius.resource_controller.state_machine', StateMachine::class); | ||
$this->makeWinzouStateMachineAvailable(); | ||
|
||
$this->setDefinition('sylius.resource_registry', new Definition()); | ||
|
||
$this->setParameter( | ||
'sylius.resources', | ||
[ | ||
'app.book' => ['state_machine_component' => 'symfony', 'classes' => ['model' => BookClass::class]], | ||
'app.author' => ['state_machine_component' => 'winzou', 'classes' => ['model' => AuthorClass::class]], | ||
'app.pull_request' => ['classes' => ['model' => PullRequestClass::class]], | ||
], | ||
); | ||
|
||
$error = false; | ||
|
||
try { | ||
$this->compile(); | ||
} catch (\LogicException $exception) { | ||
$error = true; | ||
$message = $exception->getMessage(); | ||
} | ||
|
||
$this->assertTrue($error, 'Should not compile'); | ||
$this->assertEquals('State machine "symfony" is not available.', $message); | ||
} | ||
|
||
/** @test */ | ||
public function it_throws_an_exception_when_specific_winzou_state_machine_component_is_not_available(): void | ||
{ | ||
$this->registerService('sylius.resource_controller.state_machine', Workflow::class); | ||
$this->makeSymfonyWorkflowAvailable(); | ||
|
||
$this->setDefinition('sylius.resource_registry', new Definition()); | ||
|
||
$this->setParameter( | ||
'sylius.resources', | ||
[ | ||
'app.book' => ['state_machine_component' => 'symfony', 'classes' => ['model' => BookClass::class]], | ||
'app.author' => ['state_machine_component' => 'winzou', 'classes' => ['model' => AuthorClass::class]], | ||
'app.pull_request' => ['classes' => ['model' => PullRequestClass::class]], | ||
], | ||
); | ||
|
||
$error = false; | ||
|
||
try { | ||
$this->compile(); | ||
} catch (\LogicException $exception) { | ||
$error = true; | ||
$message = $exception->getMessage(); | ||
} | ||
|
||
$this->assertTrue($error, 'Should not compile'); | ||
$this->assertEquals('State machine "winzou" is not available.', $message); | ||
} | ||
|
||
protected function registerCompilerPass(ContainerBuilder $container): void | ||
{ | ||
$container->addCompilerPass(new RegisterResourceStateMachinePass()); | ||
} | ||
|
||
private function makeWinzouStateMachineAvailable(): void | ||
{ | ||
$this->registerService('sylius.resource_controller.state_machine.winzou', StateMachine::class); | ||
} | ||
|
||
private function makeSymfonyWorkflowAvailable(): void | ||
{ | ||
$this->registerService('sylius.resource_controller.state_machine.symfony', Workflow::class); | ||
} | ||
} |
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