Skip to content

Commit

Permalink
Add a prefix before converter service names
Browse files Browse the repository at this point in the history
  • Loading branch information
guvra committed Feb 12, 2024
1 parent 0ba0c34 commit 41aea0c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
1 change: 1 addition & 0 deletions app/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
5 changes: 3 additions & 2 deletions src/Converter/ConverterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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));
}
Expand Down
10 changes: 8 additions & 2 deletions src/DependencyInjection/Compiler/ConverterAliasPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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));
}
}
23 changes: 16 additions & 7 deletions tests/unit/Converter/ConverterFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
),
Expand All @@ -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;
}
}

0 comments on commit 41aea0c

Please sign in to comment.