Skip to content

Commit

Permalink
minor #20492 [Workflow] Clarify validator API + fixed unknown "scalar…
Browse files Browse the repository at this point in the history
…" marking store (ro0NL)

This PR was squashed before being merged into the 3.2-dev branch (closes #20492).

Discussion
----------

[Workflow] Clarify validator API + fixed unknown "scalar" marking store

| Q             | A
| ------------- | ---
| Branch?       | "master"
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no, if merged in 3.2
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | comma-separated list of tickets fixed by the PR, if any
| License       | MIT
| Doc PR        | reference to the documentation PR, if any

See also https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ValidateWorkflowsPass.php#L48

Commits
-------

a9cb38b [Workflow] Clarify validator API + fixed unknown "scalar" marking store
  • Loading branch information
lyrixx committed Nov 15, 2016
2 parents b8cae3f + a9cb38b commit e4e6380
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 71 deletions.
Expand Up @@ -15,7 +15,6 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\Workflow\Validator\DefinitionValidatorInterface;
use Symfony\Component\Workflow\Validator\SinglePlaceWorkflowValidator;
use Symfony\Component\Workflow\Validator\StateMachineValidator;
use Symfony\Component\Workflow\Validator\WorkflowValidator;

Expand All @@ -24,11 +23,6 @@
*/
class ValidateWorkflowsPass implements CompilerPassInterface
{
/**
* @var DefinitionValidatorInterface[]
*/
private $validators = array();

public function process(ContainerBuilder $container)
{
$taggedServices = $container->findTaggedServiceIds('workflow.definition');
Expand All @@ -45,7 +39,7 @@ public function process(ContainerBuilder $container)
throw new RuntimeException(sprintf('The "marking_store" for the tag "workflow.definition" of service "%s" must be set.', $id));
}

$this->getValidator($tag)->validate($definition, $tag['name']);
$this->createValidator($tag)->validate($definition, $tag['name']);
}
}
}
Expand All @@ -55,23 +49,16 @@ public function process(ContainerBuilder $container)
*
* @return DefinitionValidatorInterface
*/
private function getValidator($tag)
private function createValidator($tag)
{
if ($tag['type'] === 'state_machine') {
$name = 'state_machine';
$class = StateMachineValidator::class;
} elseif ($tag['marking_store'] === 'scalar') {
$name = 'single_place';
$class = SinglePlaceWorkflowValidator::class;
} else {
$name = 'workflow';
$class = WorkflowValidator::class;
if ('state_machine' === $tag['type']) {
return new StateMachineValidator();
}

if (empty($this->validators[$name])) {
$this->validators[$name] = new $class();
if ('single_state' === $tag['marking_store']) {
return new WorkflowValidator(true);
}

return $this->validators[$name];
return new WorkflowValidator();
}
}
Expand Up @@ -3,13 +3,11 @@
namespace Symfony\Component\Workflow\Tests\Validator;

use Symfony\Component\Workflow\Definition;
use Symfony\Component\Workflow\Marking;
use Symfony\Component\Workflow\Tests\WorkflowTest;
use Symfony\Component\Workflow\Transition;
use Symfony\Component\Workflow\Validator\SinglePlaceWorkflowValidator;
use Symfony\Component\Workflow\Workflow;
use Symfony\Component\Workflow\Validator\WorkflowValidator;

class SinglePlaceWorkflowValidatorTest extends WorkflowTest
class WorkflowValidatorTest extends WorkflowTest
{
/**
* @expectedException \Symfony\Component\Workflow\Exception\InvalidDefinitionException
Expand All @@ -19,7 +17,7 @@ public function testSinglePlaceWorkflowValidatorAndComplexWorkflow()
{
$definition = $this->createComplexWorkflow();

(new SinglePlaceWorkflowValidator())->validate($definition, 'foo');
(new WorkflowValidator(true))->validate($definition, 'foo');
}

public function testSinglePlaceWorkflowValidatorAndSimpleWorkflow()
Expand All @@ -28,6 +26,6 @@ public function testSinglePlaceWorkflowValidatorAndSimpleWorkflow()
$transition = new Transition('t1', 'a', 'b');
$definition = new Definition($places, array($transition));

(new SinglePlaceWorkflowValidator())->validate($definition, 'foo');
(new WorkflowValidator(true))->validate($definition, 'foo');
}
}
Expand Up @@ -23,8 +23,6 @@ interface DefinitionValidatorInterface
* @param Definition $definition
* @param string $name
*
* @return bool
*
* @throws InvalidDefinitionException on invalid definition
*/
public function validate(Definition $definition, $name);
Expand Down

This file was deleted.

Expand Up @@ -62,7 +62,5 @@ public function validate(Definition $definition, $name)
}
$transitionFromNames[$from][$transition->getName()] = true;
}

return true;
}
}
25 changes: 25 additions & 0 deletions src/Symfony/Component/Workflow/Validator/WorkflowValidator.php
Expand Up @@ -12,13 +12,38 @@
namespace Symfony\Component\Workflow\Validator;

use Symfony\Component\Workflow\Definition;
use Symfony\Component\Workflow\Exception\InvalidDefinitionException;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class WorkflowValidator implements DefinitionValidatorInterface
{
private $singlePlace;

/**
* @param bool $singlePlace
*/
public function __construct($singlePlace = false)
{
$this->singlePlace = $singlePlace;
}

public function validate(Definition $definition, $name)
{
if ($this->singlePlace) {
foreach ($definition->getTransitions() as $transition) {
if (1 < count($transition->getTos())) {
throw new InvalidDefinitionException(
sprintf(
'The marking store of workflow "%s" can not store many places. But the transition "%s" has too many output (%d). Only one is accepted.',
$name,
$transition->getName(),
count($transition->getTos())
)
);
}
}
}
}
}

0 comments on commit e4e6380

Please sign in to comment.