Skip to content

Commit 9c61d57

Browse files
committed
fix: Broken caching of type adapters
1 parent c8c231c commit 9c61d57

File tree

4 files changed

+45
-79
lines changed

4 files changed

+45
-79
lines changed

src/GoodPhp/Serialization/Serializer/Registry/Cache/CachingTypeAdapterRegistry.php

-35
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace GoodPhp\Serialization\Serializer\Registry\Cache;
4+
5+
use Ds\Map;
6+
use GoodPhp\Reflection\Reflection\Attributes\ArrayAttributes;
7+
use GoodPhp\Reflection\Reflection\Attributes\Attributes;
8+
use GoodPhp\Reflection\Type\Type;
9+
use GoodPhp\Serialization\Serializer;
10+
use GoodPhp\Serialization\Serializer\Registry\TypeAdapterRegistry;
11+
use GoodPhp\Serialization\TypeAdapter\TypeAdapter;
12+
use GoodPhp\Serialization\TypeAdapter\TypeAdapterFactory;
13+
use Illuminate\Support\Arr;
14+
15+
final class MemoizingTypeAdapterRegistry implements TypeAdapterRegistry
16+
{
17+
/** @var array<class-string, array<>> */
18+
private array $resolved = [];
19+
20+
public function __construct(
21+
private readonly TypeAdapterRegistry $delegate,
22+
) {
23+
}
24+
25+
public function forType(string $typeAdapterType, Serializer $serializer, Type $type, Attributes $attributes = new ArrayAttributes(), TypeAdapterFactory $skipPast = null): TypeAdapter
26+
{
27+
$this->resolved[$typeAdapterType][(string) $type] ??= new \WeakMap();
28+
29+
$matchingFactories = $this->resolved[$typeAdapterType][(string) $type][$skipPast ?? $serializer] ??= [];
30+
31+
$attributesFactoryPair = Arr::first($matchingFactories, fn (array $pair) => $attributes->allEqual($pair[0]));
32+
33+
if ($attributesFactoryPair) {
34+
return $attributesFactoryPair[1];
35+
}
36+
37+
$factory = $this->delegate->forType($typeAdapterType, $serializer, $type, $attributes, $skipPast);
38+
39+
$this->resolved[$typeAdapterType][(string) $type][$skipPast ?? $serializer][] = [$attributes, $factory];
40+
41+
return $factory;
42+
}
43+
}

src/GoodPhp/Serialization/Serializer/Registry/Cache/ResolvedKey.php

-42
This file was deleted.

src/GoodPhp/Serialization/SerializerBuilder.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use GoodPhp\Reflection\Type\Type;
88
use GoodPhp\Serialization\Hydration\ConstructorHydrator;
99
use GoodPhp\Serialization\Hydration\Hydrator;
10-
use GoodPhp\Serialization\Serializer\Registry\Cache\CachingTypeAdapterRegistry;
10+
use GoodPhp\Serialization\Serializer\Registry\Cache\MemoizingTypeAdapterRegistry;
1111
use GoodPhp\Serialization\Serializer\Registry\Factory\FactoryTypeAdapterRegistryBuilder;
1212
use GoodPhp\Serialization\Serializer\TypeAdapterRegistrySerializer;
1313
use GoodPhp\Serialization\TypeAdapter\Json\FromPrimitiveJsonTypeAdapterFactory;
@@ -158,7 +158,7 @@ public function build(): Serializer
158158
->addFactoryLast(new FromPrimitiveJsonTypeAdapterFactory());
159159

160160
return new TypeAdapterRegistrySerializer(
161-
new CachingTypeAdapterRegistry($typeAdapterRegistryBuilder->build()),
161+
new MemoizingTypeAdapterRegistry($typeAdapterRegistryBuilder->build()),
162162
$this->reflector()
163163
);
164164
}

0 commit comments

Comments
 (0)