|
21 | 21 | */
|
22 | 22 | final class ReflectionObjectMapperMetadataFactory implements ObjectMapperMetadataFactoryInterface
|
23 | 23 | {
|
| 24 | + private array $reflectionClassCache = []; |
| 25 | + private array $attributesCache = []; |
| 26 | + |
24 | 27 | public function create(object $object, ?string $property = null, array $context = []): array
|
25 | 28 | {
|
26 | 29 | try {
|
27 |
| - $refl = new \ReflectionClass($object); |
28 |
| - $mapTo = []; |
29 |
| - foreach (($property ? $refl->getProperty($property) : $refl)->getAttributes(Map::class, \ReflectionAttribute::IS_INSTANCEOF) as $mapAttribute) { |
30 |
| - $map = $mapAttribute->newInstance(); |
31 |
| - $mapTo[] = new Mapping($map->target, $map->source, $map->if, $map->transform); |
| 30 | + $key = $object::class.($property ?? ''); |
| 31 | + |
| 32 | + if (isset($this->attributesCache[$key])) { |
| 33 | + return $this->attributesCache[$key]; |
| 34 | + } |
| 35 | + |
| 36 | + $refl = $this->reflectionClassCache[$object::class] ??= new \ReflectionClass($object); |
| 37 | + $attributes = ($property ? $refl->getProperty($property) : $refl)->getAttributes(Map::class, \ReflectionAttribute::IS_INSTANCEOF); |
| 38 | + $mappings = []; |
| 39 | + foreach ($attributes as $attribute) { |
| 40 | + $map = $attribute->newInstance(); |
| 41 | + $mappings[] = new Mapping($map->target, $map->source, $map->if, $map->transform); |
32 | 42 | }
|
33 | 43 |
|
34 |
| - return $mapTo; |
| 44 | + return $this->attributesCache[$key] = $mappings; |
35 | 45 | } catch (\ReflectionException $e) {
|
36 | 46 | throw new MappingException($e->getMessage(), $e->getCode(), $e);
|
37 | 47 | }
|
|
0 commit comments