Skip to content

Commit

Permalink
Merge branch 'feature-configuration-support'
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-gerarts committed Jul 13, 2019
2 parents 3d43eda + 2301a91 commit 5babeb8
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 5 deletions.
22 changes: 22 additions & 0 deletions DependencyInjection/AutoMapperPlusExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
*/
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')
Expand All @@ -28,5 +31,24 @@ public function load(array $configs, ContainerBuilder $container)

$container->registerForAutoconfiguration(AutoMapperConfiguratorInterface::class)
->addTag('automapper_plus.configurator');

// 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']]);
}
}
26 changes: 26 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace AutoMapperPlus\AutoMapperPlusBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('auto_mapper_plus');

$rootNode
->children()
->arrayNode('options')
->children()
->booleanNode('create_unregistered_mappings')->end()
->end()
->end() // options
->end();

return $treeBuilder;
}
}
43 changes: 43 additions & 0 deletions DependencyInjection/DefaultOptionsConfigurator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace AutoMapperPlus\AutoMapperPlusBundle\DependencyInjection;

use AutoMapperPlus\AutoMapperPlusBundle\AutoMapperConfiguratorInterface;
use AutoMapperPlus\Configuration\AutoMapperConfigInterface;
use AutoMapperPlus\Configuration\Options;

class DefaultOptionsConfigurator implements AutoMapperConfiguratorInterface
{
/**
* @var array
*/
private $defaultOptions;

public function __construct(array $defaultOptions)
{
$this->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;
}
}
}
38 changes: 33 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php
Expand Down Expand Up @@ -58,7 +58,9 @@ class AutoMapperConfig implements AutoMapperConfiguratorInterface

If you use autowiring, the configurators will be picked up automatically.
Alternatively, you'll have to register the class as a service and tag it
with `automapper_plus.configurator`.
with `automapper_plus.configurator`. You can optionally add a priority parameter
to the tag.


```yaml
demo.automapper_configurator:
Expand All @@ -70,6 +72,32 @@ demo.automapper_configurator:
You can register all your mappings in a single configurator class, or spread it
across multiple classes. The choice is yours!

## Configuration

The options for the mapper can be configured. Create a `config/packages/auto_mapper_plus.yaml`
file (or add to your `config.yaml` for older Symfony versions) with the following contents:


```yaml
auto_mapper_plus:
options:
create_unregistered_mappings: true
```
These options correspond with the ones of the [Options object](https://github.com/mark-gerarts/automapper-plus#the-options-object).
Full reference:
```
auto_mapper_plus:
options:
# Only one option for now, more coming soon!
create_unregistered_mappings: true
```
Using the configuration is completely optional, you can just set the options directly
on the `Options` object in one of your configurators using `$config->getOptions()`.

## Further reading
For more info regarding the automapper itself, check out the
[project page](https://www.github.com/mark-gerarts/automapper-plus).
7 changes: 7 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ services:
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 }

# Add aliases so that autowiring works
AutoMapperPlus\AutoMapperInterface: '@automapper_plus.mapper'
AutoMapperPlus\AutoMapperPlusBundle\AutoMapperFactory: '@automapper_plus.mapper_factory'
Expand Down

0 comments on commit 5babeb8

Please sign in to comment.