Skip to content

Commit 95375dc

Browse files
author
Kirill Nesmeyanov
committed
Improve metadata cache driver
1 parent 6e84222 commit 95375dc

File tree

5 files changed

+21
-33
lines changed

5 files changed

+21
-33
lines changed

src/Mapper.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ private function createTypeParser(PlatformInterface $platform): TypeParserFacade
5858

5959
private function createTypeRepository(PlatformInterface $platform): TypeRepositoryFacadeInterface
6060
{
61-
$runtime = TypeRepository::createFromPlatform($platform, $this->parser);
61+
$runtime = new InMemoryTypeRepository(
62+
delegate: TypeRepository::createFromPlatform(
63+
platform: $platform,
64+
parser: $this->parser,
65+
)
66+
);
6267

6368
if (($tracer = $this->config->getTracer()) !== null) {
6469
$runtime = new TraceableTypeRepository($tracer, $runtime);
@@ -70,7 +75,7 @@ private function createTypeRepository(PlatformInterface $platform): TypeReposito
7075

7176
return new TypeRepositoryFacade(
7277
parser: $this->parser,
73-
runtime: new InMemoryTypeRepository($runtime),
78+
runtime: $runtime,
7479
);
7580
}
7681

src/Mapping/Driver/Psr16CachedDriver.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,16 @@ public function getClassMetadata(
2727
): ClassMetadata {
2828
$index = $this->getKey($class);
2929

30-
$result = $this->cache->get(
31-
$index,
32-
fn(): ClassMetadata
33-
=> parent::getClassMetadata($class, $types, $parser)
34-
);
30+
$result = $this->cache->get($index);
3531

36-
if ($result instanceof \Closure) {
37-
$result = $result();
38-
39-
$this->cache->set($index, $result, $this->ttl);
32+
if ($result instanceof ClassMetadata) {
33+
return $result;
4034
}
4135

36+
$result = parent::getClassMetadata($class, $types, $parser);
37+
38+
$this->cache->set($index, $result, $this->ttl);
39+
4240
return $result;
4341
}
4442
}

src/Runtime/Repository/TypeDecorator/LoggableType.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
final class LoggableType extends TypeDecorator
1616
{
1717
public function __construct(
18-
private readonly ?LoggerInterface $logger,
18+
private readonly LoggerInterface $logger,
1919
TypeInterface $delegate,
2020
) {
2121
parent::__construct($delegate);
@@ -39,10 +39,6 @@ private function getLoggerArguments(mixed $value, Context $context): array
3939

4040
public function match(mixed $value, Context $context): bool
4141
{
42-
if ($this->logger === null) {
43-
return parent::match($value, $context);
44-
}
45-
4642
$this->logger->debug(
4743
'Matching by the {type_name}',
4844
$this->getLoggerArguments($value, $context),
@@ -62,10 +58,6 @@ public function match(mixed $value, Context $context): bool
6258

6359
public function cast(mixed $value, Context $context): mixed
6460
{
65-
if ($this->logger === null) {
66-
return parent::cast($value, $context);
67-
}
68-
6961
$this->logger->debug(
7062
'Casting by the {type_name}',
7163
$this->getLoggerArguments($value, $context),

src/Runtime/Repository/TypeDecorator/TraceableType.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ final class TraceableType extends TypeDecorator
2121
private readonly string $name;
2222

2323
public function __construct(
24-
private readonly ?TracerInterface $tracer,
24+
private readonly TracerInterface $tracer,
2525
TypeInterface $delegate,
2626
) {
2727
parent::__construct($delegate);
@@ -61,10 +61,6 @@ private static function getDirection(Context $context): Context\DirectionInterfa
6161

6262
public function match(mixed $value, Context $context): bool
6363
{
64-
if ($this->tracer === null) {
65-
return parent::match($value, $context);
66-
}
67-
6864
$span = $this->tracer->start(\sprintf('Match %s', $this->name));
6965

7066
try {
@@ -84,10 +80,6 @@ public function match(mixed $value, Context $context): bool
8480

8581
public function cast(mixed $value, Context $context): mixed
8682
{
87-
if ($this->tracer === null) {
88-
return parent::cast($value, $context);
89-
}
90-
9183
$span = $this->tracer->start(\sprintf('Cast %s', $this->name));
9284

9385
try {

src/Runtime/Repository/TypeDecorator/TypeDecorator.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ public function cast(mixed $value, Context $context): mixed
3636
return $this->delegate->cast($value, $context);
3737
}
3838

39-
/**
40-
* @return array<array-key, mixed>
41-
*/
4239
public function __serialize(): array
4340
{
44-
return ['delegate' => $this->delegate];
41+
throw new \LogicException(<<<'MESSAGE'
42+
Cannot serialize a type decorator.
43+
44+
Please disable cache in case you are using debug mode.
45+
MESSAGE);
4546
}
4647
}

0 commit comments

Comments
 (0)