Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Ability to change reload strategy from AutoMapper::create() #183

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading