Skip to content

Commit

Permalink
feat: Ability to change reload strategy from AutoMapper::create()
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMeshok committed Sep 16, 2024
1 parent c3a8fb1 commit b613607
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- [GH#180](https://github.com/jolicode/automapper/pull/180) Add configuration to generate code with strict types
- [GH#183](https://github.com/jolicode/automapper/pull/183) Ability to change reload strategy from AutoMapper::create()

### Fixed
- [GH#184](https://github.com/jolicode/automapper/pull/184) Fix error when mapping from stdClass to constructor with nullable/optional arguments
Expand Down
2 changes: 1 addition & 1 deletion src/AutoMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public static function create(
if (null === $cacheDirectory) {
$loader = new EvalLoader($mapperGenerator, $metadataFactory);
} else {
$loader = new FileLoader($mapperGenerator, $metadataFactory, $cacheDirectory, $lockFactory);
$loader = new FileLoader($mapperGenerator, $metadataFactory, $cacheDirectory, $lockFactory, $configuration->reloadStrategy);
}

return new self($loader, $customTransformerRegistry, $metadataRegistry, $providerRegistry, $expressionLanguageProvider);
Expand Down
6 changes: 6 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace AutoMapper;

use AutoMapper\Loader\FileReloadStrategy;

final readonly class Configuration
{
public function __construct(
Expand Down Expand Up @@ -40,6 +42,10 @@ public function __construct(
* Add declare(strict_types=1) to generated code.
*/
public bool $strictTypes = false,
/**
* The strategy to use to generate the mappers between each request.
*/
public FileReloadStrategy $reloadStrategy = FileReloadStrategy::ON_CHANGE,
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public function load(array $configs, ContainerBuilder $container): void
->setArgument('$autoRegister', $config['auto_register'])
->setArgument('$mapPrivateProperties', $config['map_private_properties'])
->setArgument('$allowReadOnlyTargetToPopulate', $config['allow_readonly_target_to_populate'])
->setArgument('$reloadStrategy', $reloadStrategy = FileReloadStrategy::from($config['loader']['reload_strategy'] ?? (
$container->getParameter('kernel.debug') ? FileReloadStrategy::ALWAYS->value : FileReloadStrategy::NEVER->value
)))
;

if ($config['map_private_properties']) {
Expand All @@ -83,13 +86,9 @@ public function load(array $configs, ContainerBuilder $container): void
->setAlias(ClassLoaderInterface::class, EvalLoader::class)
;
} else {
$isDebug = $container->getParameter('kernel.debug');
$generateStrategy = $config['loader']['reload_strategy'] ?? ($isDebug ? FileReloadStrategy::ALWAYS->value : FileReloadStrategy::NEVER->value);
$generateStrategy = FileReloadStrategy::tryFrom($generateStrategy);

$container
->getDefinition(FileLoader::class)
->replaceArgument(4, $generateStrategy);
->replaceArgument(4, $reloadStrategy);

$container
->setAlias(ClassLoaderInterface::class, FileLoader::class)
Expand Down

0 comments on commit b613607

Please sign in to comment.