From 7a311bb08c6f837dae9f025a987f63061790414a Mon Sep 17 00:00:00 2001 From: Joel Wurtz Date: Tue, 12 Mar 2024 09:22:40 +0100 Subject: [PATCH] feat(service): use direct injection into constructor --- composer.json | 4 +- .../AutoMapperExtension.php | 8 +- .../Compiler/TransformerFactoryPass.php | 15 ++- src/Resources/config/services_82.xml | 112 ++++++++++++++++++ 4 files changed, 130 insertions(+), 9 deletions(-) create mode 100644 src/Resources/config/services_82.xml diff --git a/composer.json b/composer.json index 29ff909..4d8c261 100644 --- a/composer.json +++ b/composer.json @@ -22,10 +22,10 @@ "phpstan/phpdoc-parser": "^1.25" }, "require-dev": { - "nikic/php-parser": "^4.18 || ^5.0", "moneyphp/money": "^4.2", - "phpunit/phpunit": "^9.0", + "nikic/php-parser": "^4.18 || ^5.0", "symfony/framework-bundle": "^6.0 || ^7.0", + "symfony/phpunit-bridge": "^7.0", "symfony/uid": "^6.3 || ^7.0", "symfony/yaml": "^6.3 || ^7.0" }, diff --git a/src/DependencyInjection/AutoMapperExtension.php b/src/DependencyInjection/AutoMapperExtension.php index 03a6ce3..ae492fc 100644 --- a/src/DependencyInjection/AutoMapperExtension.php +++ b/src/DependencyInjection/AutoMapperExtension.php @@ -43,16 +43,14 @@ public function load(array $configs, ContainerBuilder $container): void if (class_exists(Generator::class)) { $loader->load('generator.xml'); + $loader->load('services.xml'); } else { + // AutoMapper 8.2 $loader->load('mapper_generator.xml'); - } - - if (class_exists(CustomTransformersRegistry::class)) { $loader->load('custom_transformers.xml'); + $loader->load('services_82.xml'); } - $loader->load('services.xml'); - $container->getDefinition(MapperGeneratorMetadataFactory::class) ->replaceArgument(5, $config['date_time_format']) ->replaceArgument(6, $config['map_private_properties']) diff --git a/src/DependencyInjection/Compiler/TransformerFactoryPass.php b/src/DependencyInjection/Compiler/TransformerFactoryPass.php index a12e69a..8bb4fad 100644 --- a/src/DependencyInjection/Compiler/TransformerFactoryPass.php +++ b/src/DependencyInjection/Compiler/TransformerFactoryPass.php @@ -2,6 +2,7 @@ namespace AutoMapper\Bundle\DependencyInjection\Compiler; +use AutoMapper\Generator\Generator; use AutoMapper\Transformer\ChainTransformerFactory; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait; @@ -13,10 +14,20 @@ class TransformerFactoryPass implements CompilerPassInterface public function process(ContainerBuilder $container): void { + $selectors = []; + + foreach ($this->findAndSortTaggedServices('automapper.transformer_factory', $container) as $definition) { + $selectors[] = $definition; + } + $definition = $container->getDefinition(ChainTransformerFactory::class); - foreach ($this->findAndSortTaggedServices('automapper.transformer_factory', $container) as $factory) { - $definition->addMethodCall('addTransformerFactory', [$factory]); + if (class_exists(Generator::class)) { + foreach ($selectors as $selector) { + $definition->addMethodCall('addTransformerFactory', [$selector]); + } + } else { + $definition->replaceArgument(0, $selectors); } } } diff --git a/src/Resources/config/services_82.xml b/src/Resources/config/services_82.xml new file mode 100644 index 0000000..a364ad5 --- /dev/null +++ b/src/Resources/config/services_82.xml @@ -0,0 +1,112 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Symfony_Mapper_ + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + %automapper.cache_dir% + + + + + + + + +