diff --git a/config/services.yaml b/config/services.yaml index 654922d..8a6bc90 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -69,6 +69,7 @@ services: App\Mapping\AutoMapper: arguments: + - '%kernel.cache_dir%' - !tagged 'app.mapping.map_provider' # add more service definitions when explicit configuration is needed diff --git a/src/Mapping/AutoMapper.php b/src/Mapping/AutoMapper.php index 6cbf5cf..c9c10fc 100644 --- a/src/Mapping/AutoMapper.php +++ b/src/Mapping/AutoMapper.php @@ -7,22 +7,28 @@ class AutoMapper implements AutoMapperInterface { - public const SKIP_NULL_VALUES = 'skip_null_values'; + public const CACHE_DIR = 'automapper'; + + public const DEFAULT_CONTEXT = [ + 'skip_null_values' => true, + ]; private AutoMapperInterface $innerMapper; public function __construct( - iterable $mapProviders, + ?string $cacheDirectory = null, + iterable $mapProviders = [], ) { $this->innerMapper = InnerMapper::create( - providers: \iterator_to_array($mapProviders) + cacheDirectory: \sprintf('%s%s%s', $cacheDirectory, \DIRECTORY_SEPARATOR, self::CACHE_DIR), + providers: $mapProviders, ); } public function map(array|object $source, string|array|object $target, array $context = []): array|object|null { $context = [ - self::SKIP_NULL_VALUES => true, + ...self::DEFAULT_CONTEXT, ...$context, ];