From 08c0792f681b9f5d71148eac7e4a9244ee9edd57 Mon Sep 17 00:00:00 2001 From: Priyadi Iman Nurcahyo <1102197+priyadi@users.noreply.github.com> Date: Tue, 16 Jan 2024 11:27:21 +0700 Subject: [PATCH] refactor: Transformer is now lazy-loaded. --- src/Command/TryCommand.php | 4 +++- src/Command/TryPropertyCommand.php | 4 +++- src/MainTransformer/MainTransformer.php | 11 +++++++---- src/TransformerRegistry/SearchResultEntry.php | 7 ------- src/TransformerRegistry/TransformerRegistry.php | 2 -- tests/IntegrationTest/MappingTest.php | 9 ++++++++- 6 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/Command/TryCommand.php b/src/Command/TryCommand.php index 5be1b35..0025c68 100644 --- a/src/Command/TryCommand.php +++ b/src/Command/TryCommand.php @@ -104,10 +104,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int ->findBySourceAndTargetTypes([$sourceType], [$targetType]); foreach ($searchResult as $entry) { + $transformer = $this->transformerRegistry->get($entry->getTransformerServiceId()); + $rows[] = [ $entry->getMappingOrder(), $entry->getTransformerServiceId(), - $entry->getTransformer()::class, + $transformer::class, $this->typeResolver->getTypeString($entry->getSourceType()), $this->typeResolver->getTypeString($entry->getTargetType()), $entry->isVariantTargetType() ? 'variant' : 'invariant', diff --git a/src/Command/TryPropertyCommand.php b/src/Command/TryPropertyCommand.php index fba80c2..685a565 100644 --- a/src/Command/TryPropertyCommand.php +++ b/src/Command/TryPropertyCommand.php @@ -106,11 +106,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int ->findBySourceAndTargetTypes($sourceTypes, $targetTypes); foreach ($results as $result) { + $transformer = $this->transformerRegistry->get($result->getTransformerServiceId()); + $rows[] = [ $result->getMappingOrder(), $this->typeResolver->getTypeString($result->getSourceType()), $this->typeResolver->getTypeString($result->getTargetType()), - $result->getTransformer()::class, + $transformer::class, ]; } diff --git a/src/MainTransformer/MainTransformer.php b/src/MainTransformer/MainTransformer.php index 34a304e..bba423a 100644 --- a/src/MainTransformer/MainTransformer.php +++ b/src/MainTransformer/MainTransformer.php @@ -148,10 +148,6 @@ public function transform( } } - // inject the main transformer to the transformer if it is - // MainTransformerAwareInterface - $transformer = $this->processTransformer($searchEntry->getTransformer()); - // TransformerInterface doesn't accept MixedType, so we need to // convert it to null @@ -179,6 +175,13 @@ public function transform( } } + // get and prepare transformer + $transformer = $this->processTransformer( + $this->transformerRegistry->get( + $searchEntry->getTransformerServiceId() + ) + ); + // transform the source to the target try { diff --git a/src/TransformerRegistry/SearchResultEntry.php b/src/TransformerRegistry/SearchResultEntry.php index b046397..fd8c10e 100644 --- a/src/TransformerRegistry/SearchResultEntry.php +++ b/src/TransformerRegistry/SearchResultEntry.php @@ -14,7 +14,6 @@ namespace Rekalogika\Mapper\TransformerRegistry; use Rekalogika\Mapper\Transformer\Contracts\MixedType; -use Rekalogika\Mapper\Transformer\Contracts\TransformerInterface; use Symfony\Component\PropertyInfo\Type; class SearchResultEntry @@ -23,7 +22,6 @@ public function __construct( private int $mappingOrder, private Type|MixedType $sourceType, private Type|MixedType $targetType, - private TransformerInterface $transformer, private string $transformerServiceId, private bool $variantTargetType, ) { @@ -39,11 +37,6 @@ public function getTargetType(): Type|MixedType return $this->targetType; } - public function getTransformer(): TransformerInterface - { - return $this->transformer; - } - public function getMappingOrder(): int { return $this->mappingOrder; diff --git a/src/TransformerRegistry/TransformerRegistry.php b/src/TransformerRegistry/TransformerRegistry.php index 03eff98..0bf861e 100644 --- a/src/TransformerRegistry/TransformerRegistry.php +++ b/src/TransformerRegistry/TransformerRegistry.php @@ -64,7 +64,6 @@ public function findBySourceAndTargetType( mappingOrder: $mappingEntry->getOrder(), sourceType: $sourceType, targetType: $targetType, - transformer: $this->get($mappingEntry->getId()), transformerServiceId: $mappingEntry->getId(), variantTargetType: $mappingEntry->isVariantTargetType() ); @@ -81,7 +80,6 @@ public function findBySourceAndTargetType( mappingOrder: $mappingEntry->getOrder(), sourceType: $sourceType, targetType: $targetType, - transformer: $this->get($mappingEntry->getId()), transformerServiceId: $mappingEntry->getId(), variantTargetType: $mappingEntry->isVariantTargetType() ); diff --git a/tests/IntegrationTest/MappingTest.php b/tests/IntegrationTest/MappingTest.php index 91bfe8c..8615ed1 100644 --- a/tests/IntegrationTest/MappingTest.php +++ b/tests/IntegrationTest/MappingTest.php @@ -53,9 +53,16 @@ public function testMapping( $this->assertNotEmpty($searchResult); + $first = $searchResult[0] ?? null; + $this->assertNotNull($first); + + $transformer = $this->transformerRegistry->get( + $first->getTransformerServiceId() + ); + $this->assertInstanceOf( $transformerClass, - $searchResult[0]?->getTransformer() + $transformer, ); }