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 @@ -8,6 +8,7 @@ Latest
* [#234](https://github.com/cleverage/process-bundle/issues/234) ArrayFirstTransformer: restore the meaning of the `allow_not_iterable` option, inverted since v4.0. By default (`false`), a non-iterable input now throws an `UnexpectedValueException` (it was returned unchanged); with `allow_not_iterable: true`, it is returned unchanged (a scalar or `null` threw a `TypeError`, an object returned its first property). Set `allow_not_iterable: true` to keep the former default behaviour (see `UPGRADE.md`). An iterable object (`\Traversable`) returns its first element (`false` or its first property before), a generator already run throws an `UnexpectedValueException`. Update documentation, add tests.
* [#507](https://github.com/cleverage/process-bundle/issues/507) `csv_reader`, `input_csv_reader`: a line that cannot be read (number of columns not matching the headers, line that cannot be parsed) sends `['csv_file' => <path>, 'csv_line' => <line number>, 'csv_values' => <values read>]` to the error output, instead of the input of the task (`null`, or the file path for `input_csv_reader`, see `UPGRADE.md`), and `csv_file` / `csv_line` are in the error context of the logged error. `CsvResource::readLine()` throws an `InvalidCsvLineException` (an `UnexpectedValueException`) carrying the values read. Update documentation, add tests.
* [#111](https://github.com/cleverage/process-bundle/issues/111) The root of the configuration is renamed from `clever_age_process` to `cleverage_process` (alias of the extension), the former one is no longer recognized (see `UPGRADE.md`). Update documentation, add tests.
* [#280](https://github.com/cleverage/process-bundle/issues/280) The `code` attribute of the `cleverage.transformer` tag (or `#[AsTransformer]`) is required: a tagged service without code fails the container compilation. `TransformerInterface::getCode()` (and the `getCode()` method of the transformers of the bundle, except `GenericTransformer` and `UnavailableTransformer`), `TransformerRegistry::addTransformer()` and `RegistryCompilerPass`, deprecated since 5.4, are removed; the service locator is a required argument of the `TransformerRegistry` constructor; `TransformerRegistryInterface::getTransformerCodes()` gives the codes without instantiating the transformers (see `UPGRADE.md`). Update documentation, add tests.

## Changes
* [#428](https://github.com/cleverage/process-bundle/issues/428) Tests on PHPUnit 12.5 and 13 (PHPUnit 10 and 11 dropped), with the lowest dependencies on PHP 8.3 and 8.5.
Expand Down
32 changes: 32 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,38 @@ A line that `csv_reader` or `input_csv_reader` cannot read (number of columns no
cannot be parsed) sends `['csv_file' => <path>, 'csv_line' => <line number>, 'csv_values' => <values read>]` to the
error output. Before, `input_csv_reader` sent its input, the file path: read it from `[csv_file]` in the error branch.

### Code of the transformers

The code of a transformer is only given by the `code` attribute of its `cleverage.transformer` tag, or by the
`#[AsTransformer(code: '...')]` attribute with autoconfiguration:

- a service tagged `cleverage.transformer` without `code` (deprecated since 5.4), e.g. a transformer autoconfigured
without `#[AsTransformer]`, fails the container compilation (`The "App\Transformer\VatTransformer" transformer service
(...) is tagged "cleverage.transformer" without a "code" attribute...`): add the attribute or the `code` of the tag,
or disable its autoconfiguration (`autoconfigure: false` on its service definition, declared after the `App\:`
resource) if it is not a transformer of the registry, e.g. a class only injected in a service (`#[Exclude]` would not
register it as a service at all). A decorator of a transformer is not concerned: Symfony does not add the
autoconfiguration tags to decorators;
- a transformer of the bundle redeclared in your application (e.g.
`CleverAge\ProcessBundle\Transformer\MappingTransformer` with other arguments) replaces the definition of the bundle
and loses its tag: declare the tag with the code of the bundle (`{ name: cleverage.transformer, code: mapping }`, see
`config/services/transformer.yaml` of the bundle), the `#[AsTransformer]` attribute cannot be added to a class of the
bundle;
- `TransformerInterface::getCode()` is removed (deprecated since 5.4), as well as the `getCode()` method of the
transformers of the bundle (except `GenericTransformer` and `UnavailableTransformer`): remove the calls to it
(`parent::getCode()` included), and the `#[\Override]` attribute on your own `getCode()` methods (PHP fails with
`has #[\Override] attribute, but no matching parent method exists`), or the methods themselves;
- `TransformerRegistry::addTransformer()` is removed (deprecated since 5.4), and the service locator is a required
argument of the `TransformerRegistry` constructor. In your tests, build the registry with a locator:
`new TransformerRegistry(new ServiceLocator(['app_vat' => static fn () => new VatTransformer()]))`;
- `TransformerRegistryInterface` declares `getTransformerCodes()` (the codes, without instantiating the transformers):
implement it in your own implementations;
- `RegistryCompilerPass` is removed (deprecated since 5.4), it was no longer used by the bundle;
- a class extending `TransformerRegistry` (`@final` since 5.4) must declare the `$locator` property as non-nullable
(`protected ServiceProviderInterface $locator`), and the transformers it adds to `$transformers` itself are no longer
given by `hasTransformer()`, `getTransformer()` and `getTransformers()`, which only read the locator: register them in
the locator.

## v5.10

### Stop strategy and error branch ending on a stop
Expand Down
10 changes: 6 additions & 4 deletions docs/01-quick_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,11 @@ bundle configures them:
usages), with a `monolog.logger` tag on the `cleverage_process_task` channel;
- services implementing `CleverAge\ProcessBundle\Transformer\TransformerInterface` get the `cleverage.transformer` tag
(needed by the transformer registry) and a `monolog.logger` tag on the `cleverage_process_transformer` channel. Add
the `#[CleverAge\ProcessBundle\Attribute\AsTransformer(code: '...')]` attribute to their class, with the code returned
by `getCode()`: the transformer is then only instantiated when a process uses it (see
[transformers](03-custom_tasks.md#transformers)).
the `#[CleverAge\ProcessBundle\Attribute\AsTransformer(code: '...')]` attribute to their class to give their code: the
transformer is then only instantiated when a process uses it (see [transformers](03-custom_tasks.md#transformers)). A
transformer autoconfigured without this attribute fails the container compilation, even when it is only injected in a
task: give it a code, or disable its autoconfiguration (`autoconfigure: false` on its service definition, declared
after the `App\:` resource).

Without autoconfiguration, declare these settings yourself:

Expand All @@ -135,7 +137,7 @@ services:
App\Transformer\VatTransformer: # One definition per transformer, for its code
autowire: true
tags:
- { name: cleverage.transformer, code: app_vat } # Needed by the transformer registry, code returned by getCode()
- { name: cleverage.transformer, code: app_vat } # Needed by the transformer registry, with its code
- { name: monolog.logger, channel: cleverage_process_transformer } # Optional, see logging

App\Task\:
Expand Down
22 changes: 6 additions & 16 deletions docs/03-custom_tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,10 @@ must skip the state (`ProcessState::setSkipped(true)`) instead of outputting the
## Transformers

Transformers are another kind of service. They implement `CleverAge\ProcessBundle\Transformer\TransformerInterface`
(`transform(mixed $value, array $options = []): mixed` and `getCode(): string`) or
(`transform(mixed $value, array $options = []): mixed`) or
`CleverAge\ProcessBundle\Transformer\ConfigurableTransformerInterface` (which adds
`configureOptions(OptionsResolver $resolver): void`) and are declared with a `cleverage.transformer` tag whose `code`
attribute is the code returned by `getCode()`: with autoconfiguration, add the
attribute gives their code: with autoconfiguration, add the
`CleverAge\ProcessBundle\Attribute\AsTransformer` attribute to the class (the `cleverage.transformer` and
`monolog.logger` tags are added for you).

Expand All @@ -272,11 +272,6 @@ class VatTransformer implements ConfigurableTransformerInterface
$resolver->setDefault('rate', 0.2);
$resolver->setAllowedTypes('rate', ['float', 'int']);
}

public function getCode(): string
{
return 'app_vat';
}
}
```

Expand All @@ -297,16 +292,11 @@ services:

Once properly set up, they are registered in the `CleverAge\ProcessBundle\Registry\TransformerRegistry` (used by the
`TransformerTask` and every transformer using a sub-list of transformers) under their code, in a service locator: a
transformer is only instantiated when a process uses it. The code of the tag must be the one returned by
`TransformerInterface::getCode()` (an exception is thrown otherwise when the transformer is used). Two transformers
cannot share the same code (the container compilation fails).

`TransformerInterface::getCode()` is deprecated since 5.4: the code is given by the tag or the attribute, and the method
will be removed from the interface in v6.0. Until then, keep implementing it, returning the same code.
transformer is only instantiated when a process uses it. Two transformers cannot share the same code, and a transformer
tagged without `code` (e.g. autoconfigured without the attribute) cannot be registered: in both cases, the container
compilation fails. A service tagged with several codes is registered under each of them.

A transformer tagged without `code` (e.g. autoconfigured without the attribute) still works, but it is instantiated with
the registry and registered with `TransformerRegistry::addTransformer()`, which is deprecated since 5.4: the `code` will
be required in v6.0.
`TransformerRegistry::getTransformerCodes()` gives the registered codes without instantiating the transformers.

When creating a new transformer for your project, use an internal prefix in your codes to avoid conflicts with potential
standard additions.
Expand Down
8 changes: 5 additions & 3 deletions docs/cookbooks/01-common_setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ With autoconfiguration (`autoconfigure: true`, set in the `_defaults` of the Sym
the bundle makes the services implementing `CleverAge\ProcessBundle\Model\TaskInterface` not shared, tags
the ones implementing `CleverAge\ProcessBundle\Transformer\TransformerInterface` with `cleverage.transformer`, and binds
both to their logging channel (`monolog.logger` tags). Add the `#[AsTransformer(code: '...')]` attribute to your
transformer classes, so that they are only instantiated when used (see [transformers](../03-custom_tasks.md#transformers)).
transformer classes, to give their code: they are then only instantiated when used (see
[transformers](../03-custom_tasks.md#transformers)). A transformer autoconfigured without this attribute fails the
container compilation.

Without autoconfiguration, example of a generic declaration for all the tasks and transformers of your application:

Expand All @@ -22,7 +24,7 @@ services:
tags:
- { name: monolog.logger, channel: cleverage_process_task }

# One definition per transformer, the code of the tag being the one returned by getCode()
# One definition per transformer, for the code of its tag
App\Transformer\VatTransformer:
autowire: true
tags:
Expand Down Expand Up @@ -53,7 +55,7 @@ services:

An `_instanceof` rule cannot give a code per transformer: declare the `cleverage.transformer` tag with its `code` on each
transformer, or use the `#[AsTransformer]` attribute with autoconfiguration. A `cleverage.transformer` tag without
`code` is deprecated since 5.4.
`code` fails the container compilation.

Configuration
-------------
Expand Down
3 changes: 1 addition & 2 deletions src/Attribute/AsTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
/**
* Registers a transformer service under its code, so that it is only instantiated when a process uses it.
*
* Requires autoconfiguration; equivalent to a `cleverage.transformer` tag with a `code` attribute. The code must be the
* one returned by `TransformerInterface::getCode()`.
* Requires autoconfiguration; equivalent to a `cleverage.transformer` tag with a `code` attribute.
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
final readonly class AsTransformer
Expand Down
57 changes: 0 additions & 57 deletions src/DependencyInjection/Compiler/RegistryCompilerPass.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@
use Symfony\Component\DependencyInjection\Reference;

/**
* Registers the `cleverage.transformer` tagged services in the transformer registry: in a service locator indexed by
* the `code` attribute of the tag (lazy), or, without `code` (deprecated), with an addTransformer() call (eager).
* Registers the `cleverage.transformer` tagged services in the transformer registry, in a service locator indexed by
* the `code` attribute of the tag (lazy): a tagged service without any `code` attribute fails the compilation.
*
* The autoconfiguration of TransformerInterface adds a tag without `code` to every transformer: it is ignored when an
* other tag of the service (e.g. added by #[AsTransformer]) gives a code.
*
* An abstract definition (a parent, never instantiated) is skipped when it has no code, and fails the compilation when
* it has one: its child definitions must be tagged instead.
*
* @internal
*/
Expand All @@ -47,10 +53,20 @@ public function process(ContainerBuilder $container): void
}
$codes[] = $attributes['code'];
}
if ($container->getDefinition($id)->isAbstract()) {
if ([] === $codes) {
// A parent definition, never instantiated: its children are registered with their own tags
continue;
}
throw new InvalidArgumentException(\sprintf('The "%s" transformer service is abstract, it cannot be registered with the code "%s": tag its child definitions instead.', $id, $codes[0]));
}
if ([] === $codes) {
// Deprecated: instantiated with the registry, addTransformer() triggers the deprecation
$registry->addMethodCall('addTransformer', [new Reference($id)]);
continue;
$class = $container->getDefinition($id)->getClass() ?? $id;
if (str_starts_with($class, 'CleverAge\\ProcessBundle\\')) {
// A transformer of the bundle redeclared by the application: its tag, with its code, is lost
throw new InvalidArgumentException(\sprintf('The "%s" transformer service (%s) is tagged "cleverage.transformer" without a "code" attribute: a transformer of the bundle redeclared in the application must keep the tag of the bundle, with its code ({ name: cleverage.transformer, code: ... }, see the config/services/transformer.yaml file of the bundle).', $id, $class));
}
throw new InvalidArgumentException(\sprintf('The "%s" transformer service (%s) is tagged "cleverage.transformer" without a "code" attribute: add the #[AsTransformer(code: \'...\')] attribute to its class or the "code" attribute to its tag ({ name: cleverage.transformer, code: ... }), or disable its autoconfiguration if it is not a transformer of the registry.', $id, $class));
}
foreach (array_unique($codes) as $code) {
if (isset($ids[$code])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,15 @@ private function replace(ContainerBuilder $container, string $message, array $cl
$original = $container->getDefinition($id);
$text = \sprintf($message, $class);
if (is_a($class, TransformerInterface::class, true)) {
// The code is needed by the transformer registry: the one of the tag (see TransformerLocatorCompilerPass),
// or, for a definition tagged without code (deprecated), the one returned by the bundle transformer
$code = $this->getTagCode($original)
// @phpstan-ignore method.deprecated (transformer registered without code, until v6.0)
?? (new \ReflectionClass($class))->newInstanceWithoutConstructor()->getCode();
$definition = new Definition(UnavailableTransformer::class, [$code, $text]);
// The code is the one of the tag (see TransformerLocatorCompilerPass). A definition tagged without code
// is kept: TransformerLocatorCompilerPass fails the compilation. An untagged one (not in the registry,
// e.g. only injected in a service) is replaced, its code being its class: it is never looked up by
// code, only given by UnavailableTransformer::getCode().
$code = $this->getTagCode($original);
if (null === $code && $original->hasTag('cleverage.transformer')) {
continue;
}
$definition = new Definition(UnavailableTransformer::class, [$code ?? $class, $text]);
} else {
$definition = new Definition(UnavailableTask::class, [$text]);
$definition->setShared(false);
Expand Down
Loading