Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move ValidateWorkflowsPass to the Workflow component
- Loading branch information
Showing
9 changed files
with
114 additions
and
47 deletions.
There are no files selected for viewing
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
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
64 changes: 64 additions & 0 deletions
64
src/Symfony/Component/Workflow/DependencyInjection/ValidateWorkflowsPass.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,64 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Workflow\DependencyInjection; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Exception\RuntimeException; | ||
use Symfony\Component\Workflow\Validator\StateMachineValidator; | ||
use Symfony\Component\Workflow\Validator\WorkflowValidator; | ||
|
||
/** | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
*/ | ||
class ValidateWorkflowsPass implements CompilerPassInterface | ||
{ | ||
private $definitionTag; | ||
|
||
public function __construct($definitionTag = 'workflow.definition') | ||
{ | ||
$this->definitionTag = $definitionTag; | ||
} | ||
|
||
public function process(ContainerBuilder $container) | ||
{ | ||
$taggedServices = $container->findTaggedServiceIds($this->definitionTag, true); | ||
foreach ($taggedServices as $id => $tags) { | ||
foreach ($tags as $tag) { | ||
if (!array_key_exists('name', $tag)) { | ||
throw new RuntimeException(sprintf('The "name" for the tag "%s" of service "%s" must be set.', $this->definitionTag, $id)); | ||
} | ||
if (!array_key_exists('type', $tag)) { | ||
throw new RuntimeException(sprintf('The "type" for the tag "%s" of service "%s" must be set.', $this->definitionTag, $id)); | ||
} | ||
if (!array_key_exists('marking_store', $tag)) { | ||
throw new RuntimeException(sprintf('The "marking_store" for the tag "%s" of service "%s" must be set.', $this->definitionTag, $id)); | ||
} | ||
|
||
$this->createValidator($tag)->validate($container->get($id), $tag['name']); | ||
} | ||
} | ||
} | ||
|
||
private function createValidator($tag) | ||
{ | ||
if ('state_machine' === $tag['type']) { | ||
return new StateMachineValidator(); | ||
} | ||
|
||
if ('single_state' === $tag['marking_store']) { | ||
return new WorkflowValidator(true); | ||
} | ||
|
||
return new WorkflowValidator(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Symfony/Component/Workflow/Tests/DependencyInjection/ValidateWorkflowsPassTest.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,30 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Workflow\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\Workflow\Definition; | ||
use Symfony\Component\Workflow\DependencyInjection\ValidateWorkflowsPass; | ||
use Symfony\Component\Workflow\Transition; | ||
|
||
class ValidateWorkflowsPassTest extends TestCase | ||
{ | ||
public function testProcess() | ||
{ | ||
$container = $this->getMockBuilder(ContainerBuilder::class)->getMock(); | ||
$container | ||
->expects($this->once()) | ||
->method('findTaggedServiceIds') | ||
->with('workflow.definition') | ||
->willReturn(array('definition1' => array('workflow.definition' => array('name' => 'wf1', 'type' => 'state_machine', 'marking_store' => 'foo')))); | ||
|
||
$container | ||
->expects($this->once()) | ||
->method('get') | ||
->with('definition1') | ||
->willReturn(new Definition(array('a', 'b', 'c'), array(new Transition('t1', 'a', 'b'), new Transition('t2', 'a', 'c')))); | ||
|
||
(new ValidateWorkflowsPass())->process($container); | ||
} | ||
} |
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