Skip to content

Commit

Permalink
Recover ability to register AST transformer factories in Symfony Bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
Korbeil committed May 14, 2024
1 parent e2728c7 commit 1ad7432
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/_nav.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [Transformer](mapping/transformer.md)
- [Provider](mapping/provider.md)
- [Mapping inheritance](mapping/inheritance.md)
- [AST transformer factory](mapping/ast_transformer_factory.md)
- [Symfony Bundle](bundle/index.md)
- [Installation](bundle/installation.md)
- [Configuration](bundle/configuration.md)
Expand Down
26 changes: 26 additions & 0 deletions docs/mapping/ast_transformer_factory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# AST transformer factory

> [!WARNING]
> AST transformer factories are for very advanced usage and often not required, please use them with caution
Sometimes we need to manage more complex objects that need specific behavior during mapping. For example the
`Money\Money` object [from the Money PHP library]((https://github.com/moneyphp/money)) has a lot of properties we
don't want to manage and can confuse the AutoMapper since it will try to map any properties.

You could do it with a [custom transformer](./transformer.md) but it means you will have to declare each property that
needs to use this custom transformer. That is why AST transformer factory is here, it will automatically generate code
to map your data thanks to AST.

You can see [such a class in our test suite](https://github.com/jolicode/automapper/blob/main/tests/Fixtures/Transformer/MoneyTransformerFactory.php)
as a working example. You will need to register the factory in the `AutoMapper` instance.

```php
use AutoMapper\AutoMapper;

$autoMapper = AutoMapper::create(transformerFactories: [new MoneyTransformerFactory()]);
```

> [!NOTE]
> When using the Symfony Bundle version of the AutoMapper, you just need to implements the `TransformerFactoryInterface`
> interface and if you have autoconfiguration enabled, you do not need to register the factory manually as the tag will
> be automatically added.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use AutoMapper\Symfony\Bundle\ReflectionClassRecursiveIterator;
use AutoMapper\Transformer\PropertyTransformer\PropertyTransformerInterface;
use AutoMapper\Transformer\SymfonyUidTransformerFactory;
use AutoMapper\Transformer\TransformerFactoryInterface;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -89,6 +90,8 @@ public function load(array $configs, ContainerBuilder $container): void
$container->setParameter('automapper.cache_dir', $config['loader']['cache_dir']);
}

$container->registerForAutoconfiguration(TransformerFactoryInterface::class)->addTag('automapper.transformer_factory');

if (class_exists(AbstractUid::class)) {
$container
->getDefinition(SymfonyUidTransformerFactory::class)
Expand Down

0 comments on commit 1ad7432

Please sign in to comment.