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 @@ -6,6 +6,7 @@ Latest
* [#392](https://github.com/cleverage/process-bundle/issues/392) Documentation: reset of the process configurations between two messages of a worker (`kernel.reset`, #355), permissions of the JSON buffer of `process_launcher`, `ProcessManager` subclasses in `UPGRADE.md` (v5.5), broken link in the CHANGELOG.

## Fixes
* [#412](https://github.com/cleverage/process-bundle/issues/412) The codes of the processes, of their tasks and of the generic transformers keep their dashes (`normalizeKeys(false)`): the Symfony configuration replaced them by underscores, while the references to them kept the dash and failed (`Unknown process demo.x-y`, `No task configuration with code : next-task`, `No transformer with code : to-upper`). Their former name (with underscores) still works, deprecated since 5.7, until v6.0 (see `UPGRADE.md`). Update documentation, add tests.
* [#408](https://github.com/cleverage/process-bundle/issues/408) Regular expressions of `preg_match`, `preg_filter`, `slugify` (`replace`) and of the conditions (`match_regexp`, `not_match_regexp` of `filter`, `column_aggregator`, `array_filter`, `unset`): an invalid pattern is rejected when the options are resolved (`InvalidOptionsException`), instead of a PHP warning with the first value then "no match"; a PCRE error while matching (invalid UTF-8 with the `u` modifier, backtracking limit) throws an `UnexpectedValueException`, instead of being taken for "no match" (both `match_regexp` and `not_match_regexp` excluded the input). Update documentation, add tests.
* [#410](https://github.com/cleverage/process-bundle/issues/410) `xpath_evaluator`: the queries are evaluated with `\DOMXPath::evaluate()`, an expression that does not return nodes (`count(...)`, `string(...)`, a comparison) returns its value (`float`, `string`, `bool`) instead of `null`; new `namespaces` option (prefix => URI) to query the elements of a namespace, including the default namespace of a document (documented). Documentation: the `mb_*` functions in the examples of `callback`, `generic_transformers` and `array_map`. Add tests.
* [#402](https://github.com/cleverage/process-bundle/issues/402) `date_parser`: a date or a time out of range (`2024-02-31`, `25:61`), parsed by `\DateTime::createFromFormat()` with a warning and overflowing to the next month or day, throws an `UnexpectedValueException`, as documented for a value that cannot be parsed. A scalar (e.g. an `int` timestamp with the `U` format) or `\Stringable` input is cast to string, an array or another object throws an `UnexpectedValueException` instead of a `TypeError`. Update documentation, add tests.
Expand Down
16 changes: 16 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
Upgrade Guide
=============

## v5.7

### Dashes in the codes

The codes of the processes, of their tasks and of the generic transformers keep their dashes. Before 5.7, the Symfony
configuration replaced them by underscores (unless the code also had an underscore): `demo.x-y` was registered as
`demo.x_y`, while the references to it kept the dash and failed (`Unknown process demo.x-y`, `No task configuration
with code : next-task`, `No transformer with code : to-upper`). Such codes now work as written, and `cleverage:process:list`
shows them with their dashes.

If you used the former name (with underscores) of such a code, it still works but is deprecated since 5.7
(`Since cleverage/process-bundle 5.7: Using the process "demo.x-y" by the name "demo.x_y"...`), and will not be found in
6.0: use the code as written in the configuration, in the commands (`cleverage:process:execute demo.x-y`), the
`process_executor` tasks, the task references (`outputs`, `error_outputs`, `entry_point`, `end_point`), the transformer
references and the applications storing process codes (e.g. the schedules of the UI bundle).

## v5.6

Two deprecations, and some behaviours change: check the points below.
Expand Down
1 change: 1 addition & 0 deletions config/services/registry.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ services:
public: false
arguments:
- ~ # Service locator of the transformers by code, see TransformerLocatorCompilerPass
- [] # Former names of the generic transformers whose code has a dash, see CleverAgeProcessExtension
CleverAge\ProcessBundle\Registry\TransformerRegistry:
alias: cleverage_process.registry.transformer
CleverAge\ProcessBundle\Registry\TransformerRegistryInterface:
Expand Down
4 changes: 4 additions & 0 deletions docs/reference/01-process_definition.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ Process attributes
The process code (key of the `configurations` array) must be unique in the whole application: a process cannot be
defined twice, even in different files.

The codes of the processes, of their tasks and of the generic transformers are kept as written, dashes included
(`demo.x-y`). Before 5.7, their dashes were replaced by underscores (`demo.x_y`), but not those of the references to
them: the former name still works, with a deprecation, until v6.0.

**description**: optional string to describe a process. Displayed in process list and help. Should not be too long
(~ one line). Default is empty.

Expand Down
3 changes: 2 additions & 1 deletion docs/reference/03-generic_transformers_definition.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@ clever_age_process:
prefix: product
```

As any transformer code, generic transformer codes must be unique: use a prefix to avoid conflicts.
As any transformer code, generic transformer codes must be unique: use a prefix to avoid conflicts. A dash is kept in
the code (it was replaced by an underscore before 5.7, the former name is deprecated).
47 changes: 47 additions & 0 deletions src/Configuration/DeprecatedCodeAliases.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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\Configuration;

/**
* Before 5.7, the dashes of the process, task and generic transformer codes were replaced by underscores by the Symfony
* configuration (key normalization), unless the code also contained an underscore: "demo.x-y" was registered as
* "demo.x_y". The codes are now kept as written; their former name is a deprecated alias, until v6.0.
*
* @internal
*/
final class DeprecatedCodeAliases
{
/**
* @param iterable<int|string> $codes
*
* @return array<string, string> former name => code
*/
public static function create(iterable $codes): array
{
$codes = array_map(strval(...), [...$codes]);
$aliases = [];
foreach ($codes as $code) {
if (!str_contains($code, '-') || str_contains($code, '_')) {
continue;
}
$alias = str_replace('-', '_', $code);
// Both were defined: none was renamed
if (!\in_array($alias, $codes, true)) {
$aliases[$alias] = $code;
}
}

return $aliases;
}
}
8 changes: 8 additions & 0 deletions src/DependencyInjection/CleverAgeProcessExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use CleverAge\ProcessBundle\Attribute\AsTask;
use CleverAge\ProcessBundle\Attribute\AsTransformer;
use CleverAge\ProcessBundle\Configuration\DeprecatedCodeAliases;
use CleverAge\ProcessBundle\Configuration\TaskConfiguration;
use CleverAge\ProcessBundle\Model\ProcessStopCheckerInterface;
use CleverAge\ProcessBundle\Model\TaskInterface;
Expand Down Expand Up @@ -77,6 +78,13 @@ static function (ChildDefinition $definition, AsTransformer $attribute): void {
$processConfigurationRegistry->replaceArgument(1, $config['default_error_strategy']);
$processConfigurationRegistry->replaceArgument(2, $config['logs']);

// Former names of the generic transformers whose code has a dash (deprecated, see DeprecatedCodeAliases)
$genericTransformerCodes = array_keys($config['generic_transformers']);
if ([] !== array_filter($genericTransformerCodes, static fn (int|string $code): bool => str_contains((string) $code, '-'))) {
$container->getDefinition('cleverage_process.registry.transformer')
->replaceArgument(1, DeprecatedCodeAliases::create($genericTransformerCodes));
}

// Automatic transformer creation from config
foreach ($config['generic_transformers'] as $transformerCode => $transformerConfig) {
$transformerDefinition = new Definition(GenericTransformer::class);
Expand Down
6 changes: 6 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ protected function appendRootTransformersConfigDefinition(NodeBuilder $definitio
{
$transformersArrayDefinition = $definition->arrayNode('generic_transformers')
->useAttributeAsKey('code')
// Keep the dashes of the codes (replaced by underscores before 5.7, see DeprecatedCodeAliases)
->normalizeKeys(false)
->arrayPrototype();

// Process list
Expand Down Expand Up @@ -120,6 +122,8 @@ protected function appendRootProcessConfigDefinition(NodeBuilder $definition): v
{
$configurationsArrayDefinition = $definition->arrayNode('configurations')
->useAttributeAsKey('code')
// Keep the dashes of the codes (replaced by underscores before 5.7, see DeprecatedCodeAliases)
->normalizeKeys(false)
->arrayPrototype();

// Process list
Expand Down Expand Up @@ -178,6 +182,8 @@ protected function appendProcessConfigDefinition(NodeBuilder $definition): void
$tasksArrayDefinition = $definition
->arrayNode('tasks')
->useAttributeAsKey('code')
// Keep the dashes of the codes (replaced by underscores before 5.7, see DeprecatedCodeAliases)
->normalizeKeys(false)
->arrayPrototype();

// Process list
Expand Down
107 changes: 105 additions & 2 deletions src/Registry/ProcessConfigurationRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CleverAge\ProcessBundle\Registry;

use CleverAge\ProcessBundle\Configuration\DeprecatedCodeAliases;
use CleverAge\ProcessBundle\Configuration\ProcessConfiguration;
use CleverAge\ProcessBundle\Configuration\TaskConfiguration;
use CleverAge\ProcessBundle\Exception\InvalidProcessConfigurationException;
Expand All @@ -33,6 +34,11 @@ class ProcessConfigurationRegistry implements ProcessConfigurationRegistryInterf
*/
protected array $processConfigurations = [];

/**
* @var array<string, string>|null
*/
private ?array $deprecatedProcessCodes = null;

/**
* @param array<string, array<string, mixed>> $rawConfiguration
* @param array{success_level?: string, failed_level?: string} $defaultLogLevels
Expand All @@ -50,6 +56,7 @@ public function getProcessConfiguration(string $processCode): ProcessConfigurati
if (!$this->hasProcessConfiguration($processCode)) {
throw MissingProcessException::create($processCode);
}
$processCode = $this->resolveDeprecatedProcessCode($processCode);
$this->resolveConfiguration($processCode);

return $this->processConfigurations[$processCode];
Expand Down Expand Up @@ -92,15 +99,111 @@ public function reset(): void

public function hasProcessConfiguration(string $processCode): bool
{
return \array_key_exists($processCode, $this->rawConfiguration);
return \array_key_exists($processCode, $this->rawConfiguration)
|| \array_key_exists($processCode, $this->getDeprecatedProcessCodes());
}

/**
* @return array<string, string> former name of the processes whose code has a dash => code
*/
private function getDeprecatedProcessCodes(): array
{
return $this->deprecatedProcessCodes ??= DeprecatedCodeAliases::create(array_keys($this->rawConfiguration));
}

private function resolveDeprecatedProcessCode(string $processCode): string
{
if (\array_key_exists($processCode, $this->rawConfiguration)) {
return $processCode;
}
$code = $this->getDeprecatedProcessCodes()[$processCode];
// @deprecated since 5.7, the former name will not be found in v6.0
trigger_deprecation(
'cleverage/process-bundle',
'5.7',
'Using the process "%s" by the name "%s", with underscores instead of dashes, is deprecated: use its code "%s". The dashes of the codes were replaced by underscores before 5.7, the former name will not be found in v6.0.',
$code,
$processCode,
$code,
);

return $code;
}

/**
* Task references using the former name of a task whose code has a dash (deprecated).
*
* @param array<string, mixed> $rawProcessConfiguration
*
* @return array<string, mixed>
*/
private function resolveDeprecatedTaskReferences(string $processCode, array $rawProcessConfiguration): array
{
/** @var array<string, mixed> $tasks */
$tasks = $rawProcessConfiguration['tasks'];
// Only when a reference is unknown (an unknown reference is reported by checkTaskReferences())
$references = [$rawProcessConfiguration['entry_point'] ?? null, $rawProcessConfiguration['end_point'] ?? null];
foreach ($tasks as $rawTaskConfiguration) {
foreach (['outputs', 'error_outputs', 'errors'] as $key) {
$references = [...$references, ...(\is_array($rawTaskConfiguration[$key] ?? null) ? $rawTaskConfiguration[$key] : [])];
}
}
$unknownReferences = array_filter(
$references,
static fn (mixed $reference): bool => \is_string($reference) && !\array_key_exists($reference, $tasks)
);
if ([] === $unknownReferences) {
return $rawProcessConfiguration;
}
$aliases = DeprecatedCodeAliases::create(array_keys($tasks));
$resolve = static function (string $reference, string $key) use ($aliases, $processCode): string {
if (!\array_key_exists($reference, $aliases)) {
return $reference;
}
// @deprecated since 5.7, the former name will not be found in v6.0
trigger_deprecation(
'cleverage/process-bundle',
'5.7',
'Referencing the task "%s" of the process "%s" by the name "%s" in "%s", with underscores instead of dashes, is deprecated: use its code "%s". The dashes of the codes were replaced by underscores before 5.7, the former name will not be found in v6.0.',
$aliases[$reference],
$processCode,
$reference,
$key,
$aliases[$reference],
);

return $aliases[$reference];
};
foreach (['entry_point', 'end_point'] as $key) {
if (\is_string($rawProcessConfiguration[$key] ?? null)) {
$rawProcessConfiguration[$key] = $resolve($rawProcessConfiguration[$key], $key);
}
}
foreach ($tasks as $taskCode => $rawTaskConfiguration) {
if (!\is_array($rawTaskConfiguration)) {
continue;
}
foreach (['outputs', 'error_outputs', 'errors'] as $key) {
if (\is_array($rawTaskConfiguration[$key] ?? null)) {
$rawTaskConfiguration[$key] = array_map(
static fn (mixed $reference): mixed => \is_string($reference) ? $resolve($reference, $key) : $reference,
$rawTaskConfiguration[$key]
);
}
}
$tasks[$taskCode] = $rawTaskConfiguration;
}
$rawProcessConfiguration['tasks'] = $tasks;

return $rawProcessConfiguration;
}

protected function resolveConfiguration(string $processCode): void
{
if (\array_key_exists($processCode, $this->processConfigurations)) {
return;
}
$rawProcessConfiguration = $this->rawConfiguration[$processCode];
$rawProcessConfiguration = $this->resolveDeprecatedTaskReferences($processCode, $this->rawConfiguration[$processCode]);
/** @var TaskConfiguration[] $taskConfigurations */
$taskConfigurations = [];
/** @var array<string, string> $errorOutputsKeys */
Expand Down
20 changes: 20 additions & 0 deletions src/Registry/TransformerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ class TransformerRegistry implements TransformerRegistryInterface

/**
* @param ServiceProviderInterface<TransformerInterface>|null $locator
* @param array<string, string> $deprecatedCodes former name of the generic transformers
* whose code has a dash => code (internal)
*/
public function __construct(
protected ?ServiceProviderInterface $locator = null,
private readonly array $deprecatedCodes = [],
) {
}

Expand Down Expand Up @@ -80,6 +83,18 @@ public function getTransformers(): array

public function getTransformer(string $code): TransformerInterface
{
if (!$this->hasRegisteredTransformer($code) && isset($this->deprecatedCodes[$code])) {
// @deprecated since 5.7, the former name will not be found in v6.0
trigger_deprecation(
'cleverage/process-bundle',
'5.7',
'Using the generic transformer "%s" by the name "%s", with underscores instead of dashes, is deprecated: use its code "%s". The dashes of the codes were replaced by underscores before 5.7, the former name will not be found in v6.0.',
$this->deprecatedCodes[$code],
$code,
$this->deprecatedCodes[$code],
);
$code = $this->deprecatedCodes[$code];
}
if (isset($this->transformers[$code])) {
return $this->transformers[$code];
}
Expand All @@ -101,6 +116,11 @@ public function getTransformer(string $code): TransformerInterface
}

public function hasTransformer(string $code): bool
{
return $this->hasRegisteredTransformer($code) || isset($this->deprecatedCodes[$code]);
}

private function hasRegisteredTransformer(string $code): bool
{
return isset($this->transformers[$code]) || ($this->locator?->has($code) ?? false);
}
Expand Down
1 change: 1 addition & 0 deletions tests/Command/ExecuteProcessCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#[UsesClass(ProcessConfigurationRegistry::class)]
#[UsesClass(ConsoleProcessEvent::class)]
#[UsesClass(JsonStreamFile::class)]
#[UsesClass(\CleverAge\ProcessBundle\Configuration\DeprecatedCodeAliases::class)]
class ExecuteProcessCommandTest extends TestCase
{
private string $outputFile;
Expand Down
1 change: 1 addition & 0 deletions tests/Command/ListProcessCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#[UsesClass(TaskConfiguration::class)]
#[UsesClass(ProcessConfigurationRegistry::class)]
#[UsesClass(InvalidProcessConfigurationException::class)]
#[UsesClass(\CleverAge\ProcessBundle\Configuration\DeprecatedCodeAliases::class)]
class ListProcessCommandTest extends TestCase
{
public function testThePublicProcessesAreListedSortedByCode(): void
Expand Down
34 changes: 34 additions & 0 deletions tests/Configuration/DeprecatedCodeAliasesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\Configuration;

use CleverAge\ProcessBundle\Configuration\DeprecatedCodeAliases;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(DeprecatedCodeAliases::class)]
class DeprecatedCodeAliasesTest extends TestCase
{
/**
* The former names follow the key normalization of the Symfony configuration: the dashes were replaced unless the
* code also had an underscore, or the code with underscores was also defined.
*/
public function testCreate(): void
{
self::assertSame(
['demo.x_y' => 'demo.x-y', 'a_b_c' => 'a-b-c'],
DeprecatedCodeAliases::create(['demo.x-y', 'a-b-c', 'no_dash', 'dash-and_underscore', 'both-defined', 'both_defined', 'plain', 0])
);
}
}
Loading
Loading