-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
725 additions
and
395 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Jane\AutoMapper; | ||
|
||
interface AutoMapperRegisterInterface | ||
{ | ||
public function register(MapperConfigurationInterface $configuration): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Jane\AutoMapper\Bundle; | ||
|
||
use Jane\AutoMapper\AbstractAutoMapper; | ||
use Jane\AutoMapper\Bundle\Configuration\ConfigurationPassInterface; | ||
use Jane\AutoMapper\MapperConfigurationInterface; | ||
|
||
class AutoMapper extends AbstractAutoMapper | ||
{ | ||
/** @var ConfigurationPassInterface[] */ | ||
private $configurationPass = []; | ||
|
||
public function addConfigurationPass(ConfigurationPassInterface $configurationPass) | ||
{ | ||
$this->configurationPass[] = $configurationPass; | ||
} | ||
|
||
public function register(MapperConfigurationInterface $configuration): void | ||
{ | ||
foreach ($this->configurationPass as $configurationPass) { | ||
$configurationPass->process($configuration); | ||
} | ||
|
||
parent::register($configuration); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/AutoMapper/Bundle/Configuration/ConfigurationPassInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Jane\AutoMapper\Bundle\Configuration; | ||
|
||
use Jane\AutoMapper\MapperConfigurationInterface; | ||
|
||
interface ConfigurationPassInterface | ||
{ | ||
public function process(MapperConfigurationInterface $configuration); | ||
} |
30 changes: 30 additions & 0 deletions
30
src/AutoMapper/Bundle/Configuration/RestrictConfigurationPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Jane\AutoMapper\Bundle\Configuration; | ||
|
||
use Jane\AutoMapper\MapperConfigurationInterface; | ||
|
||
class RestrictConfigurationPass implements ConfigurationPassInterface | ||
{ | ||
private $source; | ||
|
||
private $target; | ||
|
||
private $configurationPass; | ||
|
||
public function __construct($source, $target, ConfigurationPassInterface $configurationPass) | ||
{ | ||
$this->source = $source; | ||
$this->target = $target; | ||
$this->configurationPass = $configurationPass; | ||
} | ||
|
||
public function process(MapperConfigurationInterface $configuration) | ||
{ | ||
if ($configuration->getSource() !== $this->source || $configuration->getTarget() !== $this->target) { | ||
return; | ||
} | ||
|
||
$this->configurationPass->process($configuration); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/AutoMapper/Bundle/DependencyInjection/Compiler/MapperConfigurationPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Jane\AutoMapper\Bundle\DependencyInjection\Compiler; | ||
|
||
use Jane\AutoMapper\Bundle\AutoMapper; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
class MapperConfigurationPass implements CompilerPassInterface | ||
{ | ||
use PriorityTaggedServiceTrait; | ||
|
||
public function process(ContainerBuilder $container) | ||
{ | ||
$definition = $container->getDefinition(AutoMapper::class); | ||
|
||
foreach ($this->findAndSortTaggedServices('jane_auto_mapper.mapper_configuration', $container) as $mapper) { | ||
$definition->addMethodCall('register', [$mapper]); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/AutoMapper/Bundle/DependencyInjection/Compiler/TransformerFactoryPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Jane\AutoMapper\Bundle\DependencyInjection\Compiler; | ||
|
||
use Jane\AutoMapper\Compiler\Transformer\ChainTransformerFactory; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
class TransformerFactoryPass implements CompilerPassInterface | ||
{ | ||
use PriorityTaggedServiceTrait; | ||
|
||
public function process(ContainerBuilder $container) | ||
{ | ||
$definition = $container->getDefinition(ChainTransformerFactory::class); | ||
|
||
foreach ($this->findAndSortTaggedServices('jane_auto_mapper.transformer_factory', $container) as $factory) { | ||
$definition->addMethodCall('addTransformerFactory', [$factory]); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/AutoMapper/Bundle/DependencyInjection/Configuration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Jane\AutoMapper\Bundle\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('jane_auto_mapper'); | ||
|
||
$rootNode | ||
->children() | ||
->booleanNode('private')->end() | ||
->arrayNode('mappings') | ||
->prototype('array') | ||
->children() | ||
->scalarNode('source')->isRequired()->end() | ||
->scalarNode('target')->isRequired()->end() | ||
->scalarNode('pass')->end() | ||
->end() | ||
->end() | ||
->end() | ||
; | ||
|
||
return $treeBuilder; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/AutoMapper/Bundle/DependencyInjection/JaneAutoMapperExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Jane\AutoMapper\Bundle\DependencyInjection; | ||
|
||
use Jane\AutoMapper\Bundle\AutoMapper; | ||
use Jane\AutoMapper\Bundle\Configuration\RestrictConfigurationPass; | ||
use Jane\AutoMapper\Extractor\PrivateReflectionExtractor; | ||
use Jane\AutoMapper\MapperConfiguration; | ||
use Jane\AutoMapper\MapperConfigurationFactory; | ||
use Jane\AutoMapper\MapperConfigurationInterface; | ||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
|
||
class JaneAutoMapperExtension extends Extension | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function load(array $configs, ContainerBuilder $container) | ||
{ | ||
$configuration = $this->getConfiguration($configs, $container); | ||
$config = $this->processConfiguration($configuration, $configs); | ||
|
||
$container->registerForAutoconfiguration(MapperConfigurationInterface::class)->addTag('jane_auto_mapper.mapper_configuration'); | ||
|
||
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); | ||
$loader->load('services.xml'); | ||
|
||
foreach ($config['mappings'] as $mapping) { | ||
$this->createMapperConfigurationDefinition($container, $mapping); | ||
} | ||
|
||
if ($config['private']) { | ||
$container | ||
->getDefinition(PrivateReflectionExtractor::class) | ||
->addTag('property_info.list_extractor', ['priority' => -500]) | ||
->addTag('property_info.access_extractor', ['priority' => -1500]) | ||
; | ||
} | ||
} | ||
|
||
private function createMapperConfigurationDefinition(ContainerBuilder $container, $config) | ||
{ | ||
$serviceName = 'Mapping_' . $config['source'] . '_' . $config['target']; | ||
$definition = $container->register($serviceName, MapperConfiguration::class); | ||
$definition->setFactory([new Reference(MapperConfigurationFactory::class), 'create']); | ||
$definition->addArgument($config['source']); | ||
$definition->addArgument($config['target']); | ||
$definition->addTag('jane_auto_mapper.mapper_configuration'); | ||
|
||
if (isset($config['pass'])) { | ||
$restrictConfigurationPass = $container->register($serviceName . '_RestrictPass', RestrictConfigurationPass::class); | ||
$restrictConfigurationPass->addArgument($config['source']); | ||
$restrictConfigurationPass->addArgument($config['target']); | ||
$restrictConfigurationPass->addArgument(new Reference($config['pass'])); | ||
|
||
$definition = $container->getDefinition(AutoMapper::class); | ||
$definition->addMethodCall('addConfigurationPass', [new Reference($serviceName . '_RestrictPass')]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Jane\AutoMapper\Bundle; | ||
|
||
use Jane\AutoMapper\Bundle\DependencyInjection\Compiler\MapperConfigurationPass; | ||
use Jane\AutoMapper\Bundle\DependencyInjection\Compiler\TransformerFactoryPass; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
|
||
class JaneAutoMapperBundle extends Bundle | ||
{ | ||
public function build(ContainerBuilder $container) | ||
{ | ||
parent::build($container); | ||
|
||
$container->addCompilerPass(new MapperConfigurationPass()); | ||
$container->addCompilerPass(new TransformerFactoryPass()); | ||
} | ||
} |
Oops, something went wrong.