Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions config/services/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ services:

CleverAge\ProcessBundle\Task\:
resource: '../../src/Task/*'
exclude: '../../src/Task/UnavailableTask.php'
autowire: true
public: true
shared: false
Expand Down
4 changes: 3 additions & 1 deletion config/services/transformer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions docs/01-quick_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/CleverAgeProcessBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);

/*
* This file is part of the CleverAge/ProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CleverAge\ProcessBundle\DependencyInjection\Compiler;

use CleverAge\ProcessBundle\Task\Serialization\DenormalizerTask;
use CleverAge\ProcessBundle\Task\Serialization\DeserializerTask;
use CleverAge\ProcessBundle\Task\Serialization\NormalizerTask;
use CleverAge\ProcessBundle\Task\Serialization\SerializerTask;
use CleverAge\ProcessBundle\Task\UnavailableTask;
use CleverAge\ProcessBundle\Task\Validation\ValidatorTask;
use CleverAge\ProcessBundle\Transformer\Serialization\DenormalizeTransformer;
use CleverAge\ProcessBundle\Transformer\Serialization\NormalizeTransformer;
use CleverAge\ProcessBundle\Transformer\TransformerInterface;
use CleverAge\ProcessBundle\Transformer\UnavailableTransformer;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;

/**
* Replaces the tasks and transformers whose dependencies are not available by services failing with an explicit
* message when they are used, instead of failing the container compilation (autowiring) for every application.
*
* @internal
*/
final class UnavailableServicesCompilerPass implements CompilerPassInterface
{
public const SERIALIZER_MESSAGE = 'The "%s" service requires the Symfony serializer, which does not seem enabled: consider enabling framework.serializer (see https://symfony.com/doc/current/reference/configuration/framework.html#reference-serializer-enabled).';

public const VALIDATOR_MESSAGE = 'The "%s" service requires the Symfony validator, which does not seem enabled: consider enabling framework.validation (see https://symfony.com/doc/current/reference/configuration/framework.html#reference-validation-enabled).';

public function process(ContainerBuilder $container): void
{
if (!$container->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<class-string> $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);
}
}
}
38 changes: 38 additions & 0 deletions src/Task/UnavailableTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

/*
* This file is part of the CleverAge/ProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CleverAge\ProcessBundle\Task;

use CleverAge\ProcessBundle\Model\ProcessState;
use CleverAge\ProcessBundle\Model\TaskInterface;

/**
* Replaces a task whose dependencies are not available (e.g. the Symfony serializer is not enabled), so that the
* container still compiles: the task fails only when it is executed, with an explicit message.
*
* @internal
*
* @see \CleverAge\ProcessBundle\DependencyInjection\Compiler\UnavailableServicesCompilerPass
*/
final readonly class UnavailableTask implements TaskInterface
{
public function __construct(
private string $message,
) {
}

public function execute(ProcessState $state): void
{
throw new \LogicException($this->message);
}
}
42 changes: 42 additions & 0 deletions src/Transformer/UnavailableTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

/*
* This file is part of the CleverAge/ProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CleverAge\ProcessBundle\Transformer;

/**
* Replaces a transformer whose dependencies are not available (e.g. the Symfony serializer is not enabled), so that
* the container still compiles and the transformer code is still known: it fails only when it is used, with an
* explicit message.
*
* @internal
*
* @see \CleverAge\ProcessBundle\DependencyInjection\Compiler\UnavailableServicesCompilerPass
*/
final readonly class UnavailableTransformer implements TransformerInterface
{
public function __construct(
private string $code,
private string $message,
) {
}

public function transform(mixed $value, array $options = []): mixed
{
throw new \LogicException($this->message);
}

public function getCode(): string
{
return $this->code;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

declare(strict_types=1);

/*
* This file is part of the CleverAge/ProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CleverAge\ProcessBundle\Tests\DependencyInjection\Compiler;

use CleverAge\ProcessBundle\DependencyInjection\CleverAgeProcessExtension;
use CleverAge\ProcessBundle\DependencyInjection\Compiler\UnavailableServicesCompilerPass;
use CleverAge\ProcessBundle\DependencyInjection\Configuration;
use CleverAge\ProcessBundle\Task\Serialization\DenormalizerTask;
use CleverAge\ProcessBundle\Task\Serialization\DeserializerTask;
use CleverAge\ProcessBundle\Task\Serialization\NormalizerTask;
use CleverAge\ProcessBundle\Task\Serialization\SerializerTask;
use CleverAge\ProcessBundle\Task\UnavailableTask;
use CleverAge\ProcessBundle\Task\Validation\ValidatorTask;
use CleverAge\ProcessBundle\Transformer\Serialization\DenormalizeTransformer;
use CleverAge\ProcessBundle\Transformer\Serialization\NormalizeTransformer;
use CleverAge\ProcessBundle\Transformer\UnavailableTransformer;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

#[CoversClass(UnavailableServicesCompilerPass::class)]
#[UsesClass(CleverAgeProcessExtension::class)]
#[UsesClass(Configuration::class)]
#[UsesClass(NormalizeTransformer::class)]
#[UsesClass(DenormalizeTransformer::class)]
class UnavailableServicesCompilerPassTest extends TestCase
{
private const SERIALIZER_TASKS = [NormalizerTask::class, DenormalizerTask::class, SerializerTask::class, DeserializerTask::class];

public function testServicesAreKeptWhenTheirDependenciesAreAvailable(): void
{
$container = $this->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;
}
}
40 changes: 40 additions & 0 deletions tests/Task/UnavailableTaskTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

/*
* This file is part of the CleverAge/ProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CleverAge\ProcessBundle\Tests\Task;

use CleverAge\ProcessBundle\Configuration\ProcessConfiguration;
use CleverAge\ProcessBundle\Model\ProcessHistory;
use CleverAge\ProcessBundle\Model\ProcessState;
use CleverAge\ProcessBundle\Task\UnavailableTask;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(UnavailableTask::class)]
#[UsesClass(ProcessConfiguration::class)]
#[UsesClass(ProcessHistory::class)]
#[UsesClass(ProcessState::class)]
class UnavailableTaskTest extends TestCase
{
public function testExecuteThrowsTheMessage(): void
{
$processConfiguration = new ProcessConfiguration('test', []);
$state = new ProcessState($processConfiguration, new ProcessHistory($processConfiguration));

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The serializer is not enabled');

(new UnavailableTask('The serializer is not enabled'))->execute($state);
}
}
Loading
Loading