Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
feat(service): use direct injection into constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
joelwurtz authored and Korbeil committed Mar 14, 2024
1 parent 3cb1ad3 commit 7a311bb
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 9 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
8 changes: 3 additions & 5 deletions src/DependencyInjection/AutoMapperExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down
15 changes: 13 additions & 2 deletions src/DependencyInjection/Compiler/TransformerFactoryPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
}
112 changes: 112 additions & 0 deletions src/Resources/config/services_82.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?xml version="1.0" encoding="UTF-8"?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="AutoMapper\Bundle\AutoMapper">
<argument type="service" id="AutoMapper\Loader\ClassLoaderInterface" />
<argument type="service" id="AutoMapper\Transformer\ChainTransformerFactory" />
<argument type="service" id="AutoMapper\MapperGeneratorMetadataFactoryInterface" />

</service>
<service id="AutoMapper\AutoMapperInterface" alias="AutoMapper\Bundle\AutoMapper" public="true" />
<service id="AutoMapper\AutoMapperRegistryInterface" alias="AutoMapper\Bundle\AutoMapper" public="true" />

<service id="AutoMapper\Extractor\SourceTargetMappingExtractor">
<argument type="service" id="automapper.property_info" />
<argument type="service" id="automapper.property_info.reflection_extractor" />
<argument type="service" id="automapper.property_info.reflection_extractor" />
<argument type="service" id="AutoMapper\Transformer\TransformerFactoryInterface" />
</service>

<service id="AutoMapper\Extractor\FromTargetMappingExtractor">
<argument type="service" id="automapper.property_info" />
<argument type="service" id="automapper.property_info.reflection_extractor" />
<argument type="service" id="automapper.property_info.reflection_extractor" />
<argument type="service" id="AutoMapper\Transformer\TransformerFactoryInterface" />
<argument type="service" id="serializer.mapping.class_metadata_factory" />
</service>

<service id="AutoMapper\Extractor\FromSourceMappingExtractor">
<argument type="service" id="automapper.property_info" />
<argument type="service" id="automapper.property_info.reflection_extractor" />
<argument type="service" id="automapper.property_info.reflection_extractor" />
<argument type="service" id="AutoMapper\Transformer\TransformerFactoryInterface" />
<argument type="service" id="serializer.mapping.class_metadata_factory" />
</service>

<service id="AutoMapper\MapperGeneratorMetadataFactory">
<argument type="service" id="AutoMapper\Extractor\SourceTargetMappingExtractor" />
<argument type="service" id="AutoMapper\Extractor\FromSourceMappingExtractor" />
<argument type="service" id="AutoMapper\Extractor\FromTargetMappingExtractor" />
<argument>Symfony_Mapper_</argument>
<argument>true</argument>
<argument></argument> <!-- Date time format -->
<argument></argument> <!-- map_private_properties -->
</service>
<service id="AutoMapper\MapperGeneratorMetadataFactoryInterface" alias="AutoMapper\MapperGeneratorMetadataFactory" />

<service id="AutoMapper\Loader\ClassLoaderInterface" alias="AutoMapper\Loader\FileLoader" />

<service id="automapper.mapping.class_discriminator_from_class_metadata" class="Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata">
<argument type="service" id="serializer.mapping.class_metadata_factory" />
</service>

<service id="AutoMapper\Transformer\ChainTransformerFactory">
<argument type="collection" />
</service>

<service id="AutoMapper\Transformer\TransformerFactoryInterface" alias="AutoMapper\Transformer\ChainTransformerFactory" />

<service id="AutoMapper\Normalizer\AutoMapperNormalizer">
<argument type="service" id="AutoMapper\AutoMapperInterface" />
</service>

<service id="AutoMapper\Transformer\MultipleTransformerFactory">
<tag name="automapper.transformer_factory" priority="1002" />
</service>

<service id="AutoMapper\Transformer\NullableTransformerFactory">
<tag name="automapper.transformer_factory" priority="1001" />
</service>

<service id="AutoMapper\Transformer\UniqueTypeTransformerFactory">
<tag name="automapper.transformer_factory" priority="1000" />
</service>

<service id="AutoMapper\Transformer\EnumTransformerFactory">
<tag name="automapper.transformer_factory" priority="-999" />
</service>

<service id="AutoMapper\Transformer\DateTimeTransformerFactory">
<tag name="automapper.transformer_factory" priority="-1000" />
</service>

<service id="AutoMapper\Transformer\BuiltinTransformerFactory">
<tag name="automapper.transformer_factory" priority="-1001" />
</service>

<service id="AutoMapper\Transformer\ArrayTransformerFactory">
<tag name="automapper.transformer_factory" priority="-1002" />
</service>

<service id="AutoMapper\Transformer\ObjectTransformerFactory">
<tag name="automapper.transformer_factory" priority="-1003" />
</service>

<service id="AutoMapper\Transformer\SymfonyUidTransformerFactory" />

<service id="AutoMapper\Bundle\CacheWarmup\CacheWarmer">
<argument type="service" id="AutoMapper\AutoMapperRegistryInterface" />
<argument type="tagged_iterator" tag="automapper.cache_warmer_loader" />
<argument type="string">%automapper.cache_dir%</argument>
<tag name="kernel.cache_warmer" />
</service>

<service id="AutoMapper\Bundle\CacheWarmup\ConfigurationCacheWarmerLoader">
<argument/> <!-- mappers list from config -->
<tag name="automapper.cache_warmer_loader" />
</service>
</services>
</container>

0 comments on commit 7a311bb

Please sign in to comment.