From 85c79e69eac2cfa94abbee1e050767daa2ce81be Mon Sep 17 00:00:00 2001 From: Mark Gerarts Date: Sat, 13 Jul 2019 19:17:41 +0200 Subject: [PATCH 1/3] Set up support for defining options in (yaml) configuration --- .../AutoMapperPlusExtension.php | 22 ++++++++++ .../Compiler/ConfigurationLoaderPass.php | 7 +++ DependencyInjection/Configuration.php | 26 +++++++++++ .../DefaultOptionsConfigurator.php | 43 +++++++++++++++++++ Resources/config/services.yml | 7 +++ 5 files changed, 105 insertions(+) create mode 100644 DependencyInjection/Configuration.php create mode 100644 DependencyInjection/DefaultOptionsConfigurator.php diff --git a/DependencyInjection/AutoMapperPlusExtension.php b/DependencyInjection/AutoMapperPlusExtension.php index 1437bfe..0549a62 100644 --- a/DependencyInjection/AutoMapperPlusExtension.php +++ b/DependencyInjection/AutoMapperPlusExtension.php @@ -14,15 +14,37 @@ */ class AutoMapperPlusExtension extends Extension { + private const DEFAULT_OPTIONS_CONFIGURATOR_SERVICE_ID = 'automapper_plus.default_options_configurator'; + /** * @inheritdoc */ public function load(array $configs, ContainerBuilder $container) { + // Load our services. $loader = new YamlFileLoader( $container, new FileLocator(__DIR__ . '/../Resources/config') ); $loader->load('services.yml'); + + // Set up the handling of the configuration. The options defined are + // passed to a configurator with a very high priority. + $configuration = new Configuration(); + $config = $this->processConfiguration($configuration, $configs); + $this->applyConfiguration($config, $container); + } + + private function applyConfiguration(array $config, ContainerBuilder $container): void + { + if (empty($config['options'])) { + // No need for the service if there aren't any options. + $container->removeDefinition(self::DEFAULT_OPTIONS_CONFIGURATOR_SERVICE_ID); + return; + } + + $container + ->getDefinition(self::DEFAULT_OPTIONS_CONFIGURATOR_SERVICE_ID) + ->setArguments([$config['options']]); } } diff --git a/DependencyInjection/Compiler/ConfigurationLoaderPass.php b/DependencyInjection/Compiler/ConfigurationLoaderPass.php index 3c165fe..1c5a5f0 100644 --- a/DependencyInjection/Compiler/ConfigurationLoaderPass.php +++ b/DependencyInjection/Compiler/ConfigurationLoaderPass.php @@ -20,6 +20,13 @@ public function process(ContainerBuilder $container) { $mapperFactory = $container->getDefinition('automapper_plus.mapper_factory'); $configurators = $container->findTaggedServiceIds('automapper_plus.configurator'); + uasort($configurators, function ($serviceA, $serviceB): int { + $priorityA = $serviceA[0]['priority'] ?? 0; + $priorityB = $serviceB[0]['priority'] ?? 0; + + return - ($priorityA - $priorityB); + }); + foreach ($configurators as $id => $_) { $mapperFactory->addMethodCall('addConfigureCallback', [new Reference($id)]); } diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php new file mode 100644 index 0000000..bc2286c --- /dev/null +++ b/DependencyInjection/Configuration.php @@ -0,0 +1,26 @@ +root('auto_mapper_plus'); + + $rootNode + ->children() + ->arrayNode('options') + ->children() + ->booleanNode('create_unregistered_mappings')->end() + ->end() + ->end() // options + ->end(); + + return $treeBuilder; + } +} diff --git a/DependencyInjection/DefaultOptionsConfigurator.php b/DependencyInjection/DefaultOptionsConfigurator.php new file mode 100644 index 0000000..7b8d47c --- /dev/null +++ b/DependencyInjection/DefaultOptionsConfigurator.php @@ -0,0 +1,43 @@ +defaultOptions = $defaultOptions; + } + + public function configure(AutoMapperConfigInterface $config): void + { + $options = $config->getOptions(); + + foreach ($this->defaultOptions as $name => $value) { + $this->applyOption($options, $name, $value); + } + } + + private function applyOption(Options $options, string $name, $value): void + { + switch ($name) { + case 'create_unregistered_mappings': + if ($value) { + $options->createUnregisteredMappings(); + } + else { + $options->dontCreateUnregisteredMappings(); + } + break; + } + } +} diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 568803c..2a59dc7 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -13,3 +13,10 @@ services: automapper_plus.mapper: class: AutoMapperPlus\AutoMapper factory: ['@automapper_plus.mapper_factory', create] + + automapper_plus.default_options_configurator: + public: false + class: AutoMapperPlus\AutoMapperPlusBundle\DependencyInjection\DefaultOptionsConfigurator + arguments: [] + tags: + - { name: automapper_plus.configurator, priority: 999 } From cb333666c4a7a6cf0a8dbda4dde5c671cf920c75 Mon Sep 17 00:00:00 2001 From: Mark Gerarts Date: Sat, 13 Jul 2019 19:25:22 +0200 Subject: [PATCH 2/3] Revert @dev version constraint --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 76e2323..e1c410a 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,6 @@ "require": { "php": ">=7.0.0", "symfony/framework-bundle": "^3.3", - "mark-gerarts/auto-mapper-plus": "@dev" + "mark-gerarts/auto-mapper-plus": "^1.0" } } From 2301a9185cc6f95e13e291a5077d68bd4048e2f6 Mon Sep 17 00:00:00 2001 From: Mark Gerarts Date: Sat, 13 Jul 2019 20:17:38 +0200 Subject: [PATCH 3/3] Update the README --- README.md | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index edd3cb1..5bfc791 100644 --- a/README.md +++ b/README.md @@ -27,10 +27,10 @@ $bundles = [ ## Usage -The automapper is available as a service: `automapper_plus.mapper`. You can -register mapping configurations by creating a class that implements the -`AutoMapperConfiguratorInterface`. This configurator class will have to define -a `configure` method, that gets passed the configuration object: +The automapper is available as a service: `automapper_plus.mapper` (or just type hint +the `AutoMapperPlus\AutoMapperInterface`). You can register mapping configurations by +creating a class that implements the `AutoMapperConfiguratorInterface`. This configurator +class will have to define a `configure` method, that gets passed the configuration object: ```php getOptions()`. + ## Further reading For more info regarding the automapper itself, check out the [project page](https://www.github.com/mark-gerarts/automapper-plus).