From cedf0fc8cc3851b214789f317b2b1f4772f6538e Mon Sep 17 00:00:00 2001 From: Nicolas Joubert Date: Wed, 14 Oct 2026 17:02:51 +0200 Subject: [PATCH] #278 - Do not fail the container compilation when the serializer or the validator is disabled - New internal UnavailableServicesCompilerPass: without serializer, the NormalizerTask, DenormalizerTask, SerializerTask, DeserializerTask services and the normalize / denormalize transformers are replaced by UnavailableTask / UnavailableTransformer (same id, public / shared / tags, transformer code kept), which throw an explicit LogicException when used; same for ValidatorTask without validator (its autowiring failed the compilation). - CheckSerializerCompilerPass is no longer registered, and deprecated. - Documentation (quick start) and tests. Co-Authored-By: Claude Opus 5.5 (1M context) --- CHANGELOG.md | 1 + config/services/task.yaml | 1 + config/services/transformer.yaml | 4 +- docs/01-quick_start.md | 12 ++- src/CleverAgeProcessBundle.php | 4 +- .../Compiler/CheckSerializerCompilerPass.php | 3 + .../UnavailableServicesCompilerPass.php | 86 +++++++++++++++ src/Task/UnavailableTask.php | 38 +++++++ src/Transformer/UnavailableTransformer.php | 42 ++++++++ .../UnavailableServicesCompilerPassTest.php | 101 ++++++++++++++++++ tests/Task/UnavailableTaskTest.php | 40 +++++++ .../UnavailableTransformerTest.php | 35 ++++++ 12 files changed, 361 insertions(+), 6 deletions(-) create mode 100644 src/DependencyInjection/Compiler/UnavailableServicesCompilerPass.php create mode 100644 src/Task/UnavailableTask.php create mode 100644 src/Transformer/UnavailableTransformer.php create mode 100644 tests/DependencyInjection/Compiler/UnavailableServicesCompilerPassTest.php create mode 100644 tests/Task/UnavailableTaskTest.php create mode 100644 tests/Transformer/UnavailableTransformerTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 448edcb8..34a933c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ Latest ------ ## Changes +* [#278](https://github.com/cleverage/process-bundle/issues/278) The container no longer fails to compile when the Symfony serializer or validator is not enabled: the tasks and transformers depending on them (`NormalizerTask`, `DenormalizerTask`, `SerializerTask`, `DeserializerTask`, `normalize` and `denormalize` transformers, `ValidatorTask`) are replaced by services failing with an explicit message when they are used (new internal `UnavailableServicesCompilerPass`). `CheckSerializerCompilerPass` is no longer used and deprecated. Update documentation, add tests. * [#272](https://github.com/cleverage/process-bundle/issues/272) DependencyInjection: autoconfiguration of the services implementing `TaskInterface` (`public`, not shared, `monolog.logger` tag on `cleverage_process_task`) and `TransformerInterface` (`cleverage.transformer` tag, `monolog.logger` tag on `cleverage_process_transformer`): the `_instanceof` rules or explicit tags are no longer needed with `autoconfigure: true`; a setting explicitly defined on a service wins. Add FQCN aliases to autowire `ProcessManager`, `ProcessConfigurationRegistry`, `TransformerRegistry` and `ContextualOptionResolver`. Update documentation, add tests. * [#274](https://github.com/cleverage/process-bundle/issues/274) CI: test the lowest versions allowed by `composer.json` (`--prefer-lowest`, PHP 8.2). Fix the lower bounds it revealed: require `psr/log` `^2|^3` (`Logger\AbstractLogger` and the loggers of the tests use the typed `LoggerInterface` signatures, a fatal error with `psr/log` 1) and `monolog/monolog` `^3` (the `Logger\*Processor` classes use `Monolog\LogRecord`, Monolog 3 only: a `TypeError` on every log of the `cleverage_process*` channels with Monolog 2). * [#273](https://github.com/cleverage/process-bundle/issues/273) Trigger the deprecations with `trigger_deprecation()` (`symfony/deprecation-contracts`, now required) instead of `@trigger_error(..., E_USER_DEPRECATED)`: the messages are prefixed with the package and the version that introduced the deprecation (e.g. `Since cleverage/process-bundle 5.1: ...`), as for the Symfony packages. Same `E_USER_DEPRECATED` level. Add tests. diff --git a/config/services/task.yaml b/config/services/task.yaml index 9fb96b4e..e94b034d 100644 --- a/config/services/task.yaml +++ b/config/services/task.yaml @@ -7,6 +7,7 @@ services: CleverAge\ProcessBundle\Task\: resource: '../../src/Task/*' + exclude: '../../src/Task/UnavailableTask.php' autowire: true public: true shared: false diff --git a/config/services/transformer.yaml b/config/services/transformer.yaml index 6d2c40ee..3f3ab9dc 100644 --- a/config/services/transformer.yaml +++ b/config/services/transformer.yaml @@ -6,7 +6,9 @@ services: CleverAge\ProcessBundle\Transformer\: resource: '../../src/Transformer/*' - exclude: '../../src/Transformer/GenericTransformer.php' + exclude: + - '../../src/Transformer/GenericTransformer.php' + - '../../src/Transformer/UnavailableTransformer.php' autowire: true autoconfigure: true public: true diff --git a/docs/01-quick_start.md b/docs/01-quick_start.md index 00be052b..43ba15dc 100644 --- a/docs/01-quick_start.md +++ b/docs/01-quick_start.md @@ -36,15 +36,21 @@ Remember to add the following line to `config/bundles.php` (not required if Symf CleverAge\ProcessBundle\CleverAgeProcessBundle::class => ['all' => true], ``` -Some tasks and transformers use the main Symfony serializer service, and the bundle checks at container compilation -that it is available: if it is not, the build fails with an explicit message. Make sure it is enabled -(see [`framework.serializer.enabled`](https://symfony.com/doc/current/reference/configuration/framework.html#reference-serializer-enabled)): +Some tasks and transformers use the Symfony serializer (`NormalizerTask`, `DenormalizerTask`, `SerializerTask`, +`DeserializerTask`, the `normalize` and `denormalize` transformers) or validator (`ValidatorTask`) services. If they are +not enabled, the container still compiles, and a process using one of these tasks or transformers fails with an explicit +message. To use them, enable the serializer +(see [`framework.serializer.enabled`](https://symfony.com/doc/current/reference/configuration/framework.html#reference-serializer-enabled)) +and the validator +(see [`framework.validation.enabled`](https://symfony.com/doc/current/reference/configuration/framework.html#reference-validation-enabled)): ```yaml # config/packages/framework.yaml framework: serializer: enabled: true + validation: + enabled: true ``` ## Global configuration diff --git a/src/CleverAgeProcessBundle.php b/src/CleverAgeProcessBundle.php index cd704afa..d8d7df9d 100644 --- a/src/CleverAgeProcessBundle.php +++ b/src/CleverAgeProcessBundle.php @@ -13,8 +13,8 @@ namespace CleverAge\ProcessBundle; -use CleverAge\ProcessBundle\DependencyInjection\Compiler\CheckSerializerCompilerPass; use CleverAge\ProcessBundle\DependencyInjection\Compiler\RegistryCompilerPass; +use CleverAge\ProcessBundle\DependencyInjection\Compiler\UnavailableServicesCompilerPass; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; @@ -31,7 +31,7 @@ public function build(ContainerBuilder $container): void new RegistryCompilerPass('cleverage_process.registry.transformer', 'cleverage.transformer', 'addTransformer') ); - $container->addCompilerPass(new CheckSerializerCompilerPass()); + $container->addCompilerPass(new UnavailableServicesCompilerPass()); } #[\Override] diff --git a/src/DependencyInjection/Compiler/CheckSerializerCompilerPass.php b/src/DependencyInjection/Compiler/CheckSerializerCompilerPass.php index 93006b1e..adb661c8 100644 --- a/src/DependencyInjection/Compiler/CheckSerializerCompilerPass.php +++ b/src/DependencyInjection/Compiler/CheckSerializerCompilerPass.php @@ -20,6 +20,9 @@ /** * Check the presence of the serializer (required for this bundle), and help the user to set it. + * + * @deprecated no longer used by the bundle, which compiles without the serializer: the services depending on it + * fail when they are used (see UnavailableServicesCompilerPass). Will be removed in v6.0. */ class CheckSerializerCompilerPass implements CompilerPassInterface { diff --git a/src/DependencyInjection/Compiler/UnavailableServicesCompilerPass.php b/src/DependencyInjection/Compiler/UnavailableServicesCompilerPass.php new file mode 100644 index 00000000..d9ddbea0 --- /dev/null +++ b/src/DependencyInjection/Compiler/UnavailableServicesCompilerPass.php @@ -0,0 +1,86 @@ +has('serializer') && !$container->has(DenormalizerInterface::class)) { + $this->replace($container, self::SERIALIZER_MESSAGE, [ + NormalizerTask::class, + DenormalizerTask::class, + SerializerTask::class, + DeserializerTask::class, + NormalizeTransformer::class, + DenormalizeTransformer::class, + ]); + } + + if (!$container->has('validator') && !$container->has(ValidatorInterface::class)) { + $this->replace($container, self::VALIDATOR_MESSAGE, [ValidatorTask::class]); + } + } + + /** + * @param list $ids + */ + private function replace(ContainerBuilder $container, string $message, array $ids): void + { + foreach ($ids as $id) { + if (!$container->hasDefinition($id)) { + continue; + } + $original = $container->getDefinition($id); + $text = \sprintf($message, $id); + if (is_a($id, TransformerInterface::class, true)) { + // The code is needed by the transformer registry: the bundle transformers return a constant + $code = (new \ReflectionClass($id))->newInstanceWithoutConstructor()->getCode(); + $definition = new Definition(UnavailableTransformer::class, [$code, $text]); + } else { + $definition = new Definition(UnavailableTask::class, [$text]); + $definition->setShared(false); + } + $definition->setPublic($original->isPublic()); + $definition->setTags($original->getTags()); + $container->setDefinition($id, $definition); + } + } +} diff --git a/src/Task/UnavailableTask.php b/src/Task/UnavailableTask.php new file mode 100644 index 00000000..a2f58c3a --- /dev/null +++ b/src/Task/UnavailableTask.php @@ -0,0 +1,38 @@ +message); + } +} diff --git a/src/Transformer/UnavailableTransformer.php b/src/Transformer/UnavailableTransformer.php new file mode 100644 index 00000000..f28bcb49 --- /dev/null +++ b/src/Transformer/UnavailableTransformer.php @@ -0,0 +1,42 @@ +message); + } + + public function getCode(): string + { + return $this->code; + } +} diff --git a/tests/DependencyInjection/Compiler/UnavailableServicesCompilerPassTest.php b/tests/DependencyInjection/Compiler/UnavailableServicesCompilerPassTest.php new file mode 100644 index 00000000..205662b1 --- /dev/null +++ b/tests/DependencyInjection/Compiler/UnavailableServicesCompilerPassTest.php @@ -0,0 +1,101 @@ +createContainer(serializer: true, validator: true); + + foreach ([...self::SERIALIZER_TASKS, NormalizeTransformer::class, DenormalizeTransformer::class, ValidatorTask::class] as $id) { + self::assertSame($id, $container->getDefinition($id)->getClass()); + } + } + + public function testSerializerServicesAreReplacedWithoutSerializer(): void + { + $container = $this->createContainer(serializer: false, validator: true); + + foreach (self::SERIALIZER_TASKS as $id) { + $definition = $container->getDefinition($id); + self::assertSame(UnavailableTask::class, $definition->getClass(), $id); + self::assertSame([\sprintf(UnavailableServicesCompilerPass::SERIALIZER_MESSAGE, $id)], $definition->getArguments()); + // Still fetched by the process manager, and logged in the tasks channel + self::assertTrue($definition->isPublic()); + self::assertFalse($definition->isShared()); + self::assertSame([['channel' => 'cleverage_process_task']], $definition->getTag('monolog.logger')); + } + foreach ([NormalizeTransformer::class => 'normalize', DenormalizeTransformer::class => 'denormalize'] as $id => $code) { + $definition = $container->getDefinition($id); + self::assertSame(UnavailableTransformer::class, $definition->getClass(), $id); + // Still registered under its code, so that using it gives the explicit message + self::assertSame([$code, \sprintf(UnavailableServicesCompilerPass::SERIALIZER_MESSAGE, $id)], $definition->getArguments()); + self::assertTrue($definition->hasTag('cleverage.transformer')); + } + self::assertSame(ValidatorTask::class, $container->getDefinition(ValidatorTask::class)->getClass()); + } + + public function testValidatorTaskIsReplacedWithoutValidator(): void + { + $container = $this->createContainer(serializer: true, validator: false); + + $definition = $container->getDefinition(ValidatorTask::class); + self::assertSame(UnavailableTask::class, $definition->getClass()); + self::assertSame([\sprintf(UnavailableServicesCompilerPass::VALIDATOR_MESSAGE, ValidatorTask::class)], $definition->getArguments()); + self::assertTrue($definition->isPublic()); + self::assertFalse($definition->isShared()); + self::assertSame(NormalizerTask::class, $container->getDefinition(NormalizerTask::class)->getClass()); + } + + private function createContainer(bool $serializer, bool $validator): ContainerBuilder + { + $container = new ContainerBuilder(); + (new CleverAgeProcessExtension())->load([], $container); + if ($serializer) { + $container->setDefinition('serializer', new Definition(\stdClass::class)); + } + if ($validator) { + $container->setDefinition('validator', new Definition(\stdClass::class)); + } + (new UnavailableServicesCompilerPass())->process($container); + + return $container; + } +} diff --git a/tests/Task/UnavailableTaskTest.php b/tests/Task/UnavailableTaskTest.php new file mode 100644 index 00000000..62e7e7d9 --- /dev/null +++ b/tests/Task/UnavailableTaskTest.php @@ -0,0 +1,40 @@ +expectException(\LogicException::class); + $this->expectExceptionMessage('The serializer is not enabled'); + + (new UnavailableTask('The serializer is not enabled'))->execute($state); + } +} diff --git a/tests/Transformer/UnavailableTransformerTest.php b/tests/Transformer/UnavailableTransformerTest.php new file mode 100644 index 00000000..b6d50538 --- /dev/null +++ b/tests/Transformer/UnavailableTransformerTest.php @@ -0,0 +1,35 @@ +getCode()); + } + + public function testTransformThrowsTheMessage(): void + { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('The serializer is not enabled'); + + (new UnavailableTransformer('normalize', 'The serializer is not enabled'))->transform('value'); + } +}