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

refactor: refactor ObjectToObjectTransformer for future optimization #231

Merged
merged 7 commits into from
Oct 14, 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 1.11.1

* refactor: refactor `ObjectToObjectTransformer` for future optimization

## 1.11.0

* refactor: spin off `resolveTargetClass()` to separate class
Expand Down
51 changes: 43 additions & 8 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
use Rekalogika\Mapper\Transformer\ObjectToObjectMetadata\Implementation\CachingObjectToObjectMetadataFactory;
use Rekalogika\Mapper\Transformer\ObjectToObjectMetadata\Implementation\ObjectToObjectMetadataFactory;
use Rekalogika\Mapper\Transformer\ObjectToObjectMetadata\Implementation\ProxyResolvingObjectToObjectMetadataFactory;
use Rekalogika\Mapper\TransformerProcessor\ObjectProcessor\DefaultObjectProcessorFactory;
use Rekalogika\Mapper\TransformerProcessor\PropertyProcessor\CachingPropertyProcessorFactory;
use Rekalogika\Mapper\TransformerProcessor\PropertyProcessor\DefaultPropertyProcessorFactory;
use Rekalogika\Mapper\TransformerRegistry\Implementation\CachingTransformerRegistry;
use Rekalogika\Mapper\TransformerRegistry\Implementation\TransformerRegistry;
use Rekalogika\Mapper\TypeResolver\Implementation\CachingTypeResolver;
Expand Down Expand Up @@ -227,10 +230,7 @@
->set(ObjectToObjectTransformer::class)
->args([
'$objectToObjectMetadataFactory' => service('rekalogika.mapper.object_to_object_metadata_factory'),
'$propertyMapperLocator' => tagged_locator('rekalogika.mapper.property_mapper'),
'$subMapperFactory' => service('rekalogika.mapper.sub_mapper.factory'),
'$proxyFactory' => service('rekalogika.mapper.proxy.factory'),
'$propertyAccessor' => service(PropertyAccessorInterface::class),
'$objectProcessorFactory' => service('rekalogika.mapper.transformer_processor.object_processor_factory'),
])
->tag('rekalogika.mapper.transformer', ['priority' => -900]);

Expand Down Expand Up @@ -273,10 +273,7 @@
)
->args([
'$objectToObjectMetadataFactory' => service('rekalogika.mapper.object_to_object_metadata_factory'),
'$propertyMapperLocator' => tagged_locator('rekalogika.mapper.property_mapper'),
'$subMapperFactory' => service('rekalogika.mapper.sub_mapper.factory'),
'$proxyFactory' => service('rekalogika.mapper.proxy.factory'),
'$propertyAccessor' => service(PropertyAccessorInterface::class),
'$objectProcessorFactory' => service('rekalogika.mapper.transformer_processor.object_processor_factory'),
]);

# mapping cache warmer
Expand Down Expand Up @@ -382,6 +379,44 @@
service($createCache($services, 'transformer_registry')),
]);

# transformer processor, object processor factory

$services
->set(
'rekalogika.mapper.transformer_processor.object_processor_factory',
DefaultObjectProcessorFactory::class,
)
->args([
'$propertyMapperLocator' => tagged_locator('rekalogika.mapper.property_mapper'),
'$subMapperFactory' => service('rekalogika.mapper.sub_mapper.factory'),
'$proxyFactory' => service('rekalogika.mapper.proxy.factory'),
'$propertyAccessor' => service(PropertyAccessorInterface::class),
'$propertyProcessorFactory' => service('rekalogika.mapper.transformer_processor.property_processor_factory'),
]);

# transformer processor, property processor factory

$services
->set(
'rekalogika.mapper.transformer_processor.property_processor_factory',
DefaultPropertyProcessorFactory::class,
)
->args([
'$propertyAccessor' => service(PropertyAccessorInterface::class),
'$subMapperFactory' => service('rekalogika.mapper.sub_mapper.factory'),
'$propertyMapperLocator' => tagged_locator('rekalogika.mapper.property_mapper'),
]);

$services
->set(
'rekalogika.mapper.transformer_processor.property_processor_factory.caching',
CachingPropertyProcessorFactory::class,
)
->decorate('rekalogika.mapper.transformer_processor.property_processor_factory')
->args([
service('.inner'),
]);

# sub mapper

$services
Expand Down
50 changes: 39 additions & 11 deletions src/MapperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
use Rekalogika\Mapper\Transformer\MetadataUtil\MetadataUtilLocator;
use Rekalogika\Mapper\Transformer\ObjectToObjectMetadata\ObjectToObjectMetadataFactoryInterface;
use Rekalogika\Mapper\Transformer\TransformerInterface;
use Rekalogika\Mapper\TransformerProcessor\ObjectProcessor\DefaultObjectProcessorFactory;
use Rekalogika\Mapper\TransformerProcessor\ObjectProcessorFactoryInterface;
use Rekalogika\Mapper\TransformerProcessor\PropertyProcessor\CachingPropertyProcessorFactory;
use Rekalogika\Mapper\TransformerProcessor\PropertyProcessor\DefaultPropertyProcessorFactory;
use Rekalogika\Mapper\TransformerProcessor\PropertyProcessorFactoryInterface;
use Rekalogika\Mapper\TransformerRegistry\Implementation\TransformerRegistry;
use Rekalogika\Mapper\TransformerRegistry\TransformerRegistryInterface;
use Rekalogika\Mapper\TypeResolver\Implementation\CachingTypeResolver;
Expand Down Expand Up @@ -197,6 +202,10 @@ class MapperFactory

private ?ProxyFactoryInterface $proxyFactory = null;

private ?ObjectProcessorFactoryInterface $objectProcessorFactory = null;

private ?PropertyProcessorFactoryInterface $propertyProcessorFactory = null;

private ?MappingCommand $mappingCommand = null;

private ?TryCommand $tryCommand = null;
Expand Down Expand Up @@ -403,17 +412,10 @@ protected function getNullTransformer(): TransformerInterface

protected function getObjectToObjectTransformer(): TransformerInterface
{
if (null === $this->objectToObjectTransformer) {
$this->objectToObjectTransformer = new ObjectToObjectTransformer(
objectToObjectMetadataFactory: $this->getObjectToObjectMetadataFactory(),
propertyMapperLocator: $this->getPropertyMapperLocator(),
subMapperFactory: $this->getSubMapperFactory(),
proxyFactory: $this->getProxyFactory(),
propertyAccessor: $this->getPropertyAccessor(),
);
}

return $this->objectToObjectTransformer;
return $this->objectToObjectTransformer ??= new ObjectToObjectTransformer(
$this->getObjectToObjectMetadataFactory(),
$this->getObjectProcessorFactory(),
);
}

protected function getObjectToStringTransformer(): TransformerInterface
Expand Down Expand Up @@ -899,6 +901,32 @@ protected function getProxyFactory(): ProxyFactoryInterface
return $this->proxyFactory;
}

//
// transformer processor
//

protected function getObjectProcessorFactory(): ObjectProcessorFactoryInterface
{
return $this->objectProcessorFactory ??= new DefaultObjectProcessorFactory(
propertyMapperLocator: $this->getPropertyMapperLocator(),
subMapperFactory: $this->getSubMapperFactory(),
proxyFactory: $this->getProxyFactory(),
propertyAccessor: $this->getPropertyAccessor(),
propertyProcessorFactory: $this->getPropertyProcessorFactory(),
);
}

protected function getPropertyProcessorFactory(): PropertyProcessorFactoryInterface
{
return $this->propertyProcessorFactory ??= new CachingPropertyProcessorFactory(
new DefaultPropertyProcessorFactory(
propertyAccessor: $this->getPropertyAccessor(),
subMapperFactory: $this->getSubMapperFactory(),
propertyMapperLocator: $this->getPropertyMapperLocator(),
),
);
}

//
// command
//
Expand Down
Loading