10000 [Workflow] Move code from ValidateWorkflowsPass to the FrameworkExten… · symfony/symfony@7c4afd4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7c4afd4

Browse files
committed
[Workflow] Move code from ValidateWorkflowsPass to the FrameworkExtension
1 parent c7fe1b6 commit 7c4afd4

File tree

8 files changed

+98
-106
lines changed

8 files changed

+98
-106
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -583,19 +583,15 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
583583
$places = array_map(function (array $place) {
584584
return $place['name'];
585585
}, $workflow['places']);
586+
$initialPlace = $workflow['initial_place'] ?? null;
586587

587588
// Create a Definition
588589
$definitionDefinition = new Definition(Workflow\Definition::class);
589590
$definitionDefinition->setPublic(false);
590591
$definitionDefinition->addArgument($places);
591592
$definitionDefinition->addArgument($transitions);
592-
$definitionDefinition->addArgument($workflow['initial_place'] ?? null);
593+
$definitionDefinition->addArgument($initialPlace);
593594
$definitionDefinition->addArgument($metadataStoreDefinition);
594-
$definitionDefinition->addTag('workflow.definition', array(
595-
'name' => $name,
596-
'type' => $type,
597-
'marking_store' => isset($workflow['marking_store']['type']) ? $workflow['marking_store']['type'] : null,
598-
));
599595

600596
// Create MarkingStore
601597
if (isset($workflow['marking_store']['type'])) {
@@ -620,6 +616,30 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
620616
$container->setDefinition(sprintf('%s.definition', $workflowId), $definitionDefinition);
621617
$container->registerAliasForArgument($workflowId, WorkflowInterface::class, $name.'.'.$type);
622618

619+
// Validate Workflow
620+
$validator = null;
621+
switch (true) {
622+
case 'state_machine' === $workflow['type']:
623+
$validator = new Workflow\Validator\StateMachineValidator();
624+
break;
625+
case isset($workflow['marking_store']) && 'single_state' === $workflow['marking_store']:
626+
$validator = new Workflow\Validator\StateMachineValidator(true);
627+
break;
628+
case isset($workflow['marking_store']) && 'multiple_state' === $workflow['marking_store']:
629+
$validator = new Workflow\Validator\StateMachineValidator(false);
630+
break;
631+
}
632+
if ($validator) {
633+
$realDefinition = (new Workflow\DefinitionBuilder($places))
634+
->addTransitions(array_map(function (Reference $ref) use ($container): Workflow\Transition {
635+
return $container->get((string) $ref);
636+
}, $transitions))
637+
->setInitialPlace($initialPlace)
638+
->build()
639+
;
640+
$validator->validate($realDefinition, $name);
641+
}
642+
623643
// Add workflow to Registry
624644
if ($workflow['supports']) {
625645
foreach ($workflow['supports'] as $supportedClassName) {

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
use Symfony\Component\Translation\DependencyInjection\TranslatorPass;
5353
use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass;
5454
use Symfony\Component\Validator\DependencyInjection\AddValidatorInitializersPass;
55-
use Symfony\Component\Workflow\DependencyInjection\ValidateWorkflowsPass;
5655

5756
/**
5857
* Bundle.
@@ -110,7 +109,6 @@ public function build(ContainerBuilder $container)
110109
$container->addCompilerPass(new DataCollectorTranslatorPass());
111110
$container->addCompilerPass(new ControllerArgumentValueResolverPass());
112111
$container->addCompilerPass(new CachePoolPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 32);
113-
$this->addCompilerPassIfExists($container, ValidateWorkflowsPass::class);
114112
$container->addCompilerPass(new CachePoolClearerPass(), PassConfig::TYPE_AFTER_REMOVING);
115113
$container->addCompilerPass(new CachePoolPrunerPass(), PassConfig::TYPE_AFTER_REMOVING);
116114
$this->addCompilerPassIfExists($container, FormPass::class);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
use Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest;
4+
5+
$container->loadFromExtension('framework', array(
6+
'workflows' => array(
7+
'my_workflow' => array(
8+
'type' => 'state_machine',
9+
'supports' => array(
10+
FrameworkExtensionTest::class,
11+
),
12+
'places' => array(
13+
'first',
14+
'middle',
15+
'last',
16+
),
17+
'transitions' => array(
18+
'go' => array(
19+
'from' => array(
20+
'first',
21+
),
22+
'to' => array(
23+
'middle',
24+
'last',
25+
),
26+
),
27+
),
28+
),
29+
),
30+
));
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:framework="http://symfony.com/schema/dic/symfony"
6+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
7+
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
8+
9+
<framework:config>
10+
<framework:workflow name="my_workflow" type="state_machine">
11+
<framework:support>Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest</framework:support>
12+
<framework:place name="first" />
13+
<framework:place name="middle" />
14+
<framework:place name="last" />
15+
<framework:transition name="go">
16+
<framework:from>first</framework:from>
17+
<framework:to>middle</framework:to>
18+
<framework:to>last</framework:to>
19+
</framework:transition>
20+
</framework:workflow>
21+
</framework:config>
22+
</container>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
framework:
2+
workflows:
3+
my_workflow:
4+
type: state_machine
5+
supports:
6+
- Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest
7+
places: [first, middle, last]
8+
transitions:
9+
go:
10+
from: first
11+
to: [last, middle ]

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ public function testWorkflows()
215215
$workflowDefinition->getArgument(0),
216216
'Places are passed to the workflow definition'
217217
);
218-
$this->assertSame(array('workflow.definition' => array(array('name' => 'article', 'type' => 'workflow', 'marking_store' => 'multiple_state'))), $workflowDefinition->getTags());
219218
$this->assertCount(4, $workflowDefinition->getArgument(1));
220219
$this->assertSame('draft', $workflowDefinition->getArgument(2));
221220

@@ -237,7 +236,6 @@ public function testWorkflows()
237236
$stateMachineDefinition->getArgument(0),
238237
'Places are passed to the state machine definition'
239238
);
240-
$this->assertSame(array('workflow.definition' => array(array('name' => 'pull_request', 'type' => 'state_machine', 'marking_store' => 'single_state'))), $stateMachineDefinition->getTags());
241239
$this->assertCount(9, $stateMachineDefinition->getArgument(1));
242240
$this->assertSame('start', $stateMachineDefinition->getArgument(2));
243241

@@ -272,6 +270,15 @@ public function testWorkflows()
272270
$this->assertGreaterThan(0, \count($registryDefinition->getMethodCalls()));
273271
}
274272

273+
/**
274+
* @expectedException \Symfony\Component\Workflow\Exception\InvalidDefinitionException
275+
* @expectedExceptionMessage A transition from a place/state must have an unique name. Multiple transitions named "go" from place/state "first" where found on StateMachine "my_workflow".
276+
*/
277+
public function testWorkflowAreValidated()
278+
{
279+
$this->createContainerFromFile('workflow_not_valid');
280+
}
281+
275282
/**
276283
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
277284
* @expectedExceptionMessage "type" and "service" cannot be used together.

src/Symfony/Component/Workflow/DependencyInjection/ValidateWorkflowsPass.php

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

src/Symfony/Component/Workflow/Tests/DependencyInjection/ValidateWorkflowsPassTest.php

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

0 commit comments

Comments
 (0)
0