-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathPropertyInfoCacheExtractor.php
139 lines (111 loc) · 4.58 KB
/
PropertyInfoCacheExtractor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\PropertyInfo;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\PropertyInfo\Util\LegacyTypeConverter;
use Symfony\Component\TypeInfo\Type;
/**
* Adds a PSR-6 cache layer on top of an extractor.
*
* @author Kévin Dunglas <[email protected]>
*
* @final
*/
class PropertyInfoCacheExtractor implements PropertyInfoExtractorInterface, PropertyInitializableExtractorInterface
{
private array $arrayCache = [];
public function __construct(
private readonly PropertyInfoExtractorInterface $propertyInfoExtractor,
private readonly CacheItemPoolInterface $cacheItemPool,
) {
}
public function isReadable(string $class, string $property, array $context = []): ?bool
{
return $this->extract('isReadable', [$class, $property, $context]);
}
public function isWritable(string $class, string $property, array $context = []): ?bool
{
return $this->extract('isWritable', [$class, $property, $context]);
}
public function getShortDescription(string $class, string $property, array $context = []): ?string
{
return $this->extract('getShortDescription', [$class, $property, $context]);
}
public function getLongDescription(string $class, string $property, array $context = []): ?string
{
return $this->extract('getLongDescription', [$class, $property, $context]);
}
public function getProperties(string $class, array $context = []): ?array
{
return $this->extract('getProperties', [$class, $context]);
}
public function getType(string $class, string $property, array $context = []): ?Type
{
try {
$serializedArguments = serialize([$class, $property, $context]);
} catch (\Exception) {
// If arguments are not serializable, skip the cache
if (method_exists($this->propertyInfoExtractor, 'getType')) {
return $this->propertyInfoExtractor->getType($class, $property, $context);
}
return LegacyTypeConverter::toTypeInfoType($this->propertyInfoExtractor->getTypes($class, $property, $context));
}
// Calling rawurlencode escapes special characters not allowed in PSR-6's keys
$key = rawurlencode('getType.'.$serializedArguments);
if (\array_key_exists($key, $this->arrayCache)) {
return $this->arrayCache[$key];
}
$item = $this->cacheItemPool->getItem($key);
if ($item->isHit()) {
return $this->arrayCache[$key] = $item->get();
}
if (method_exists($this->propertyInfoExtractor, 'getType')) {
$value = $this->propertyInfoExtractor->getType($class, $property, $context);
} else {
$value = LegacyTypeConverter::toTypeInfoType($this->propertyInfoExtractor->getTypes($class, $property, $context));
}
$item->set($value);
$this->cacheItemPool->save($item);
return $this->arrayCache[$key] = $value;
}
public function getTypes(string $class, string $property, array $context = []): ?array
{
return $this->extract('getTypes', [$class, $property, $context]);
}
public function isInitializable(string $class, string $property, array $context = []): ?bool
{
return $this->extract('isInitializable', [$class, $property, $context]);
}
/**
* Retrieves the cached data if applicable or delegates to the decorated extractor.
*/
private function extract(string $method, array $arguments): mixed
{
try {
$serializedArguments = serialize($arguments);
} catch (\Exception) {
// If arguments are not serializable, skip the cache
return $this->propertyInfoExtractor->{$method}(...$arguments);
}
// Calling rawurlencode escapes special characters not allowed in PSR-6's keys
$key = rawurlencode($method.'.'.$serializedArguments);
if (\array_key_exists($key, $this->arrayCache)) {
return $this->arrayCache[$key];
}
$item = $this->cacheItemPool->getItem($key);
if ($item->isHit()) {
return $this->arrayCache[$key] = $item->get();
}
$value = $this->propertyInfoExtractor->{$method}(...$arguments);
$item->set($value);
$this->cacheItemPool->save($item);
return $this->arrayCache[$key] = $value;
}
}