Skip to content

Commit e4e6380

Browse files
committed
minor #20492 [Workflow] Clarify validator API + fixed unknown "scalar" 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
2 parents b8cae3f + a9cb38b commit e4e6380

File tree

6 files changed

+36
-71
lines changed

6 files changed

+36
-71
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ValidateWorkflowsPass.php

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1717
use Symfony\Component\Workflow\Validator\DefinitionValidatorInterface;
18-
use Symfony\Component\Workflow\Validator\SinglePlaceWorkflowValidator;
1918
use Symfony\Component\Workflow\Validator\StateMachineValidator;
2019
use Symfony\Component\Workflow\Validator\WorkflowValidator;
2120

@@ -24,11 +23,6 @@
2423
*/
2524
class ValidateWorkflowsPass implements CompilerPassInterface
2625
{
27-
/**
28-
* @var DefinitionValidatorInterface[]
29-
*/
30-
private $validators = array();
31-
3226
public function process(ContainerBuilder $container)
3327
{
3428
$taggedServices = $container->findTaggedServiceIds('workflow.definition');
@@ -45,7 +39,7 @@ public function process(ContainerBuilder $container)
4539
throw new RuntimeException(sprintf('The "marking_store" for the tag "workflow.definition" of service "%s" must be set.', $id));
4640
}
4741

48-
$this->getValidator($tag)->validate($definition, $tag['name']);
42+
$this->createValidator($tag)->validate($definition, $tag['name']);
4943
}
5044
}
5145
}
@@ -55,23 +49,16 @@ public function process(ContainerBuilder $container)
5549
*
5650
* @return DefinitionValidatorInterface
5751
*/
58-
private function getValidator($tag)
52+
private function createValidator($tag)
5953
{
60-
if ($tag['type'] === 'state_machine') {
61-
$name = 'state_machine';
62-
$class = StateMachineValidator::class;
63-
} elseif ($tag['marking_store'] === 'scalar') {
64-
$name = 'single_place';
65-
$class = SinglePlaceWorkflowValidator::class;
66-
} else {
67-
$name = 'workflow';
68-
$class = WorkflowValidator::class;
54+
if ('state_machine' === $tag['type']) {
55+
return new StateMachineValidator();
6956
}
7057

71-
if (empty($this->validators[$name])) {
72-
$this->validators[$name] = new $class();
58+
if ('single_state' === $tag['marking_store']) {
59+
return new WorkflowValidator(true);
7360
}
7461

75-
return $this->validators[$name];
62+
return new WorkflowValidator();
7663
}
7764
}

src/Symfony/Component/Workflow/Tests/Validator/SinglePlaceWorkflowValidatorTest.php renamed to src/Symfony/Component/Workflow/Tests/Validator/WorkflowValidatorTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
namespace Symfony\Component\Workflow\Tests\Validator;
44

55
use Symfony\Component\Workflow\Definition;
6-
use Symfony\Component\Workflow\Marking;
76
use Symfony\Component\Workflow\Tests\WorkflowTest;
87
use Symfony\Component\Workflow\Transition;
9-
use Symfony\Component\Workflow\Validator\SinglePlaceWorkflowValidator;
10-
use Symfony\Component\Workflow\Workflow;
8+
use Symfony\Component\Workflow\Validator\WorkflowValidator;
119

12-
class SinglePlaceWorkflowValidatorTest extends WorkflowTest
10+
class WorkflowValidatorTest extends WorkflowTest
1311
{
1412
/**
1513
* @expectedException \Symfony\Component\Workflow\Exception\InvalidDefinitionException
@@ -19,7 +17,7 @@ public function testSinglePlaceWorkflowValidatorAndComplexWorkflow()
1917
{
2018
$definition = $this->createComplexWorkflow();
2119

22-
(new SinglePlaceWorkflowValidator())->validate($definition, 'foo');
20+
(new WorkflowValidator(true))->validate($definition, 'foo');
2321
}
2422

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

31-
(new SinglePlaceWorkflowValidator())->validate($definition, 'foo');
29+
(new WorkflowValidator(true))->validate($definition, 'foo');
3230
}
3331
}

src/Symfony/Component/Workflow/Validator/DefinitionValidatorInterface.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ interface DefinitionValidatorInterface
2323
* @param Definition $definition
2424
* @param string $name
2525
*
26-
* @return bool
27-
*
2826
* @throws InvalidDefinitionException on invalid definition
2927
*/
3028
public function validate(Definition $definition, $name);

src/Symfony/Component/Workflow/Validator/SinglePlaceWorkflowValidator.php

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/Symfony/Component/Workflow/Validator/StateMachineValidator.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,5 @@ public function validate(Definition $definition, $name)
6262
}
6363
$transitionFromNames[$from][$transition->getName()] = true;
6464
}
65-
66-
return true;
6765
}
6866
}

src/Symfony/Component/Workflow/Validator/WorkflowValidator.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,38 @@
1212
namespace Symfony\Component\Workflow\Validator;
1313

1414
use Symfony\Component\Workflow\Definition;
15+
use Symfony\Component\Workflow\Exception\InvalidDefinitionException;
1516

1617
/**
1718
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
1819
*/
1920
class WorkflowValidator implements DefinitionValidatorInterface
2021
{
22+
private $singlePlace;
23+
24+
/**
25+
* @param bool $singlePlace
26+
*/
27+
public function __construct($singlePlace = false)
28+
{
29+
$this->singlePlace = $singlePlace;
30+
}
31+
2132
public function validate(Definition $definition, $name)
2233
{
34+
if ($this->singlePlace) {
35+
foreach ($definition->getTransitions() as $transition) {
36+
if (1 < count($transition->getTos())) {
37+
throw new InvalidDefinitionException(
38+
sprintf(
39+
'The marking store of workflow "%s" can not store many places. But the transition "%s" has too many output (%d). Only one is accepted.',
40+
$name,
41+
$transition->getName(),
42+
count($transition->getTos())
43+
)
44+
);
45+
}
46+
}
47+
}
2348
}
2449
}

0 commit comments

Comments
 (0)