Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Workflow] Add a way to add more definition validator
- Loading branch information
Showing
7 changed files
with
187 additions
and
0 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
25 changes: 25 additions & 0 deletions
25
src/Symfony/Component/Workflow/CacheWarmer/DefinitionAndValidator.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,25 @@ | ||
<?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\CacheWarmer; | ||
|
||
use Symfony\Component\Workflow\Definition; | ||
use Symfony\Component\Workflow\Validator\DefinitionValidatorInterface; | ||
|
||
final class DefinitionAndValidator | ||
{ | ||
public function __construct( | ||
public readonly DefinitionValidatorInterface $validator, | ||
public readonly Definition $definition, | ||
public readonly string $name, | ||
) { | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/Symfony/Component/Workflow/CacheWarmer/DefinitionValidatorCacheWarmer.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,42 @@ | ||
<?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\CacheWarmer; | ||
|
||
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; | ||
|
||
final class DefinitionValidatorCacheWarmer implements CacheWarmerInterface | ||
{ | ||
/** | ||
* @param iterable<DefinitionAndValidator> $definitionAndValidators | ||
*/ | ||
public function __construct( | ||
private readonly iterable $definitionAndValidators, | ||
) { | ||
} | ||
|
||
public function isOptional(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function warmUp(string $cacheDir, ?string $buildDir = null): array | ||
{ | ||
foreach ($this->definitionAndValidators as $definitionAndValidator) { | ||
$definitionAndValidator | ||
->validator | ||
->validate($definitionAndValidator->definition, $definitionAndValidator->name) | ||
; | ||
} | ||
|
||
return []; | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
src/Symfony/Component/Workflow/DependencyInjection/DefinitionValidatorPass.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,84 @@ | ||
<?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\Definition; | ||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\Workflow\CacheWarmer\DefinitionAndValidator; | ||
use Symfony\Component\Workflow\Validator\ConfiguredDefinitionValidatorInterface; | ||
|
||
/** | ||
* @author Grégoire Pineau <lyrixx@lyrixx.info> | ||
*/ | ||
class DefinitionValidatorPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
if (!$container->hasDefinition('workflow.cache_warmer.definition_validator')) { | ||
return; | ||
} | ||
|
||
$definitions = []; | ||
foreach ($container->findTaggedServiceIds('workflow.definition') as $id => $attributes) { | ||
$name = $attributes[0]['name'] ?? throw new InvalidArgumentException(sprintf('The "name" attribute is mandatory for the "workflow.definition" tag. Check the tag for service "%s".', $id)); | ||
$definitions[$name] = new Reference($id); | ||
} | ||
|
||
$definitionAndValidators = []; | ||
foreach ($container->findTaggedServiceIds('workflow.definition_validator') as $id => $attributes) { | ||
$def = $container->getDefinition($id); | ||
|
||
$class = $def->getClass(); | ||
|
||
if (!$r = $container->getReflectionClass($class)) { | ||
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id)); | ||
} | ||
if (!$r->isSubclassOf(ConfiguredDefinitionValidatorInterface::class)) { | ||
throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, ConfiguredDefinitionValidatorInterface::class)); | ||
} | ||
|
||
foreach ($class::getSupportedWorkflows() as $name) { | ||
Check failure on line 52 in src/Symfony/Component/Workflow/DependencyInjection/DefinitionValidatorPass.php GitHub Actions / PsalmUndefinedClass
Check failure on line 52 in src/Symfony/Component/Workflow/DependencyInjection/DefinitionValidatorPass.php GitHub Actions / PsalmUndefinedClass
|
||
if ('*' === $name) { | ||
foreach ($definitions as $definitionName => $definition) { | ||
$definitionAndValidators[] = new Definition( | ||
DefinitionAndValidator::class, | ||
[ | ||
new Reference($id), | ||
$definition, | ||
$definitionName, | ||
] | ||
); | ||
} | ||
} elseif (isset($definitions[$name])) { | ||
$definitionAndValidators[] = new Definition( | ||
DefinitionAndValidator::class, | ||
[ | ||
new Reference($id), | ||
$definitions[$name], | ||
$name, | ||
] | ||
); | ||
} else { | ||
throw new InvalidArgumentException(sprintf('The workflow "%s" does not exist. Check the "getConfiguration()" method of the service "%s".', $name, $id)); | ||
} | ||
} | ||
} | ||
|
||
$container | ||
->getDefinition('workflow.cache_warmer.definition_validator') | ||
->replaceArgument(0, $definitionAndValidators) | ||
; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Symfony/Component/Workflow/Validator/ConfiguredDefinitionValidatorInterface.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,23 @@ | ||
<?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\Validator; | ||
|
||
/** | ||
* @author Grégoire Pineau <lyrixx@lyrixx.info> | ||
*/ | ||
interface ConfiguredDefinitionValidatorInterface extends DefinitionValidatorInterface | ||
{ | ||
/** | ||
* @return list<string> A list of workflow name, or "*" for all workflows | ||
*/ | ||
public static function getSupportedWorkflows(): iterable; | ||
} |