Skip to content

Commit 238b84b

Browse files
lyrixxfabpot
authored andcommitted
[Workflow] Add support for executing custom workflow definition validators during the container compilation
1 parent 029e266 commit 238b84b

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Workflow\DependencyInjection;
13+
14+
use Symfony\Component\Config\Resource\FileResource;
15+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Exception\LogicException;
18+
19+
/**
20+
* @author Grégoire Pineau <[email protected]>
21+
*/
22+
class WorkflowValidatorPass implements CompilerPassInterface
23+
{
24+
public function process(ContainerBuilder $container): void
25+
{
26+
foreach ($container->findTaggedServiceIds('workflow') as $attributes) {
27+
foreach ($attributes as $attribute) {
28+
foreach ($attribute['definition_validators'] ?? [] as $validatorClass) {
29+
$container->addResource(new FileResource($container->getReflectionClass($validatorClass)->getFileName()));
30+
31+
$realDefinition = $container->get($attribute['definition_id'] ?? throw new \LogicException('The "definition_id" attribute is required.'));
32+
(new $validatorClass())->validate($realDefinition, $attribute['name'] ?? throw new \LogicException('The "name" attribute is required.'));
33+
}
34+
}
35+
}
36+
}
37+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Workflow\Tests\DependencyInjection;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\Workflow\Definition;
17+
use Symfony\Component\Workflow\DependencyInjection\WorkflowValidatorPass;
18+
use Symfony\Component\Workflow\Validator\DefinitionValidatorInterface;
19+
use Symfony\Component\Workflow\WorkflowInterface;
20+
21+
class WorkflowValidatorPassTest extends TestCase
22+
{
23+
private ContainerBuilder $container;
24+
private WorkflowValidatorPass $compilerPass;
25+
26+
protected function setUp(): void
27+
{
28+
$this->container = new ContainerBuilder();
29+
$this->compilerPass = new WorkflowValidatorPass();
30+
}
31+
32+
public function testNothingToDo()
33+
{
34+
$this->compilerPass->process($this->container);
35+
36+
$this->assertFalse(DefinitionValidator::$called);
37+
}
38+
39+
public function testValidate()
40+
{
41+
$this
42+
->container
43+
->register('my.workflow', WorkflowInterface::class)
44+
->addTag('workflow', [
45+
'definition_id' => 'my.workflow.definition',
46+
'name' => 'my.workflow',
47+
'definition_validators' => [DefinitionValidator::class],
48+
])
49+
;
50+
51+
$this
52+
->container
53+
->register('my.workflow.definition', Definition::class)
54+
->setArguments([
55+
'$places' => [],
56+
'$transitions' => [],
57+
])
58+
;
59+
60+
$this->compilerPass->process($this->container);
61+
62+
$this->assertTrue(DefinitionValidator::$called);
63+
}
64+
}
65+
66+
class DefinitionValidator implements DefinitionValidatorInterface
67+
{
68+
public static bool $called = false;
69+
70+
public function validate(Definition $definition, string $name): void
71+
{
72+
self::$called = true;
73+
}
74+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
},
2626
"require-dev": {
2727
"psr/log": "^1|^2|^3",
28+
"symfony/config": "^6.4|^7.0",
2829
"symfony/dependency-injection": "^6.4|^7.0",
2930
"symfony/error-handler": "^6.4|^7.0",
3031
"symfony/event-dispatcher": "^6.4|^7.0",

0 commit comments

Comments
 (0)