diff --git a/app/config/services.yaml b/app/config/services.yaml index 9e634c9b..3377b17c 100644 --- a/app/config/services.yaml +++ b/app/config/services.yaml @@ -19,6 +19,7 @@ services: Smile\GdprDump\Phar\Minify\MinifierInterface: tags: ['compiler.minifier'] + # Service id will be replaced by an alias during ConverterAliasPass Smile\GdprDump\Converter\: resource: '../../src/Converter' diff --git a/src/Converter/ConverterFactory.php b/src/Converter/ConverterFactory.php index 9c79defb..f0777d4d 100644 --- a/src/Converter/ConverterFactory.php +++ b/src/Converter/ConverterFactory.php @@ -4,8 +4,9 @@ namespace Smile\GdprDump\Converter; -use Psr\Container\ContainerInterface; use RuntimeException; +use Smile\GdprDump\DependencyInjection\Compiler\ConverterAliasPass; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; use UnexpectedValueException; @@ -153,7 +154,7 @@ private function createConverter(string $name, array $parameters = []): Converte { try { /** @var ConverterInterface $converter */ - $converter = $this->container->get($name); + $converter = $this->container->get(ConverterAliasPass::ALIAS_PREFIX . $name); } catch (ServiceNotFoundException) { throw new RuntimeException(sprintf('The converter "%s" is not defined.', $name)); } diff --git a/src/DependencyInjection/Compiler/ConverterAliasPass.php b/src/DependencyInjection/Compiler/ConverterAliasPass.php index ceb261df..3fc888ea 100644 --- a/src/DependencyInjection/Compiler/ConverterAliasPass.php +++ b/src/DependencyInjection/Compiler/ConverterAliasPass.php @@ -11,8 +11,13 @@ class ConverterAliasPass implements CompilerPassInterface { + public const ALIAS_PREFIX = 'converter.'; + /** - * @inheritdoc + * Replace the default service id of converters (class names) by an alias. + * + * Using an alias as the service id allows the converter factory to fetch a converter + * with the alias specified in the config file (e.g. "randomizeText"). */ public function process(ContainerBuilder $container): void { @@ -37,6 +42,7 @@ private function getConverterAlias(Definition $definition): string $parts = explode('\\', $className); - return lcfirst(array_pop($parts)); + // Add a prefix to prevent any conflict with other services + return self::ALIAS_PREFIX . lcfirst(array_pop($parts)); } } diff --git a/tests/unit/Converter/ConverterFactoryTest.php b/tests/unit/Converter/ConverterFactoryTest.php index cb884462..727af99d 100644 --- a/tests/unit/Converter/ConverterFactoryTest.php +++ b/tests/unit/Converter/ConverterFactoryTest.php @@ -11,6 +11,7 @@ use Smile\GdprDump\Converter\Proxy\Conditional; use Smile\GdprDump\Converter\Proxy\Faker; use Smile\GdprDump\Converter\Proxy\Unique; +use Smile\GdprDump\DependencyInjection\Compiler\ConverterAliasPass; use Smile\GdprDump\Faker\FakerService; use Smile\GdprDump\Tests\Framework\Mock\Converter\ConverterMock; use Symfony\Component\DependencyInjection\Container; @@ -221,13 +222,13 @@ private function createFactory(): ConverterFactory $this->returnCallback( fn (string $value) => match ($value) { // Converters used in the context of this unit test - 'cache' => new Cache(), - 'chain' => new Chain(), - 'conditional' => new Conditional(), - 'faker' => new Faker(new FakerService()), - 'mock' => new ConverterMock(), - 'notExists' => throw new ServiceNotFoundException($value), - 'unique' => new Unique(), + $this->getServiceId('cache') => new Cache(), + $this->getServiceId('chain') => new Chain(), + $this->getServiceId('conditional') => new Conditional(), + $this->getServiceId('faker') => new Faker(new FakerService()), + $this->getServiceId('mock') => new ConverterMock(), + $this->getServiceId('notExists') => throw new ServiceNotFoundException($value), + $this->getServiceId('unique') => new Unique(), default => throw new UnexpectedValueException( sprintf('The converter "%s" was not expected in this unit case.', $value) ), @@ -237,4 +238,12 @@ private function createFactory(): ConverterFactory return new ConverterFactory($containerMock); } + + /** + * Get the service id from a container name. + */ + private function getServiceId(string $name): string + { + return ConverterAliasPass::ALIAS_PREFIX . $name; + } }