Skip to content
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
5 changes: 4 additions & 1 deletion src/Generator/InjectMapperMethodStatementsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace AutoMapper\Generator;

use AutoMapper\LazyMapper;
use AutoMapper\Metadata\Dependency;
use AutoMapper\Metadata\GeneratorMetadata;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar;
use PhpParser\Node\Stmt;

Expand Down Expand Up @@ -53,7 +55,8 @@ public function getStatements(Expr\Variable $automapperRegistryVariable, Generat
new Expr\PropertyFetch(new Expr\Variable('this'), 'mappers'),
new Scalar\String_($dependency->mapperDependency->name)
),
new Expr\MethodCall($automapperRegistryVariable, 'getMapper', [
new Expr\New_(new Name(LazyMapper::class), [
new Arg($automapperRegistryVariable),
new Arg(new Scalar\String_($dependency->mapperDependency->source)),
new Arg(new Scalar\String_($dependency->mapperDependency->target)),
])
Expand Down
87 changes: 87 additions & 0 deletions src/LazyMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace AutoMapper;

/**
* @template Source of object|array<mixed>
* @template Target of object|array<mixed>
*
* @phpstan-import-type MapperContextArray from MapperContext
*
* @internal
*
* @implements MapperInterface<Source, Target>
*/
class LazyMapper implements MapperInterface
{
/** @var MapperInterface<Source, Target>|null */
private ?MapperInterface $mapper = null;

public function __construct(
private readonly AutoMapperRegistryInterface $registry,
/** @var 'array'|class-string<object> */
private readonly string $source,
/** @var 'array'|class-string<object> */
private readonly string $target,
) {
}

public function getTargetIdentifiers(mixed $value): mixed
{
$mapper = $this->getMapper();

if ($mapper instanceof GeneratedMapper) {
return $mapper->getTargetIdentifiers($value);
}

return null;
}

public function getSourceHash(mixed $value): string
{
$mapper = $this->getMapper();

if ($mapper instanceof GeneratedMapper) {
return $mapper->getSourceHash($value);
}

return '';
}

public function getTargetHash(mixed $value): string
{
$mapper = $this->getMapper();

if ($mapper instanceof GeneratedMapper) {
return $mapper->getTargetHash($value);
}

return '';
}

public function &map(mixed $value, array $context = []): mixed
{
return $this->getMapper()->map($value, $context);
}

/**
* @return MapperInterface<Source, Target>
*/
public function getMapper(): MapperInterface
{
if ($this->mapper === null) {
/** @var MapperInterface<Source, Target> $mapper */
$mapper = $this->registry->getMapper($this->source, $this->target);

if ($mapper instanceof GeneratedMapper) {
$mapper->registerMappers($this->registry);
}

$this->mapper = $mapper;
}

return $this->mapper;
}
}