1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace W2w\Lib\ApieObjectAccessNormalizer\ObjectAccess; |
5
|
|
|
|
6
|
|
|
use Closure; |
7
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
8
|
|
|
use ReflectionClass; |
9
|
|
|
use ReflectionMethod; |
10
|
|
|
use Symfony\Component\PropertyInfo\Type; |
11
|
|
|
|
12
|
|
|
class CachedObjectAccess implements ObjectAccessSupportedInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var ObjectAccessInterface |
16
|
|
|
*/ |
17
|
|
|
private $internal; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var CacheItemPoolInterface |
21
|
|
|
*/ |
22
|
|
|
private $cacheItemPool; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param ObjectAccessInterface $internal |
26
|
|
|
* @param CacheItemPoolInterface $cacheItemPool |
27
|
|
|
*/ |
28
|
|
|
public function __construct(ObjectAccessInterface $internal, CacheItemPoolInterface $cacheItemPool) |
29
|
|
|
{ |
30
|
|
|
$this->internal = $internal; |
31
|
|
|
$this->cacheItemPool = $cacheItemPool; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritDoc} |
36
|
|
|
*/ |
37
|
|
|
public function getGetterFields(ReflectionClass $reflectionClass): array |
38
|
|
|
{ |
39
|
|
|
return $this->cacheCheck( |
40
|
|
|
__FUNCTION__ . ',' . $reflectionClass->name, |
41
|
|
|
function (ReflectionClass $reflectionClass) { |
42
|
|
|
return $this->internal->getGetterFields($reflectionClass); |
43
|
|
|
}, |
44
|
|
|
$reflectionClass |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritDoc} |
50
|
|
|
*/ |
51
|
|
|
public function getSetterFields(ReflectionClass $reflectionClass): array |
52
|
|
|
{ |
53
|
|
|
return $this->cacheCheck( |
54
|
|
|
__FUNCTION__ . ',' . $reflectionClass->name, |
55
|
|
|
function (ReflectionClass $reflectionClass) { |
56
|
|
|
return $this->internal->getSetterFields($reflectionClass); |
57
|
|
|
}, |
58
|
|
|
$reflectionClass |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritDoc} |
64
|
|
|
*/ |
65
|
|
|
public function getGetterTypes(ReflectionClass $reflectionClass, string $fieldName): array |
66
|
|
|
{ |
67
|
|
|
return $this->cacheCheck( |
68
|
|
|
__FUNCTION__ . ',' . $reflectionClass->name . ',' . $fieldName, |
69
|
|
|
function (ReflectionClass $reflectionClass, string $fieldName) { |
70
|
|
|
return $this->internal->getGetterTypes($reflectionClass, $fieldName); |
71
|
|
|
}, |
72
|
|
|
$reflectionClass, |
73
|
|
|
$fieldName |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritDoc} |
79
|
|
|
*/ |
80
|
|
|
public function getSetterTypes(ReflectionClass $reflectionClass, string $fieldName): array |
81
|
|
|
{ |
82
|
|
|
return $this->cacheCheck( |
83
|
|
|
__FUNCTION__ . ',' . $reflectionClass->name . ',' . $fieldName, |
84
|
|
|
function (ReflectionClass $reflectionClass, string $fieldName) { |
85
|
|
|
return $this->internal->getSetterTypes($reflectionClass, $fieldName); |
86
|
|
|
}, |
87
|
|
|
$reflectionClass, |
88
|
|
|
$fieldName |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritDoc} |
94
|
|
|
*/ |
95
|
|
|
public function getConstructorArguments(ReflectionClass $reflectionClass): array |
96
|
|
|
{ |
97
|
|
|
return $this->cacheCheck( |
98
|
|
|
__FUNCTION__ . ',' . $reflectionClass->name, |
99
|
|
|
function (ReflectionClass $reflectionClass) { |
100
|
|
|
return $this->internal->getConstructorArguments($reflectionClass); |
101
|
|
|
}, |
102
|
|
|
$reflectionClass |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritDoc} |
108
|
|
|
*/ |
109
|
|
|
public function getValue(object $instance, string $fieldName) |
110
|
|
|
{ |
111
|
|
|
return $this->internal->getValue($instance, $fieldName); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritDoc} |
116
|
|
|
*/ |
117
|
|
|
public function setValue(object $instance, string $fieldName, $value) |
118
|
|
|
{ |
119
|
|
|
return $this->internal->setValue($instance, $fieldName, $value); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritDoc} |
124
|
|
|
*/ |
125
|
|
|
public function instantiate(ReflectionClass $reflectionClass, array $constructorArgs): object |
126
|
|
|
{ |
127
|
|
|
return $this->internal->instantiate($reflectionClass, $constructorArgs); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritDoc} |
132
|
|
|
*/ |
133
|
|
|
public function getDescription(ReflectionClass $reflectionClass, string $fieldName, bool $preferGetters): ?string |
134
|
|
|
{ |
135
|
|
|
return $this->cacheCheck( |
136
|
|
|
__FUNCTION__ . ',' . $reflectionClass->name . ',' . $fieldName . ',' . json_encode($preferGetters), |
137
|
|
|
function (ReflectionClass $reflectionClass, string $fieldName, bool $preferGetters) { |
138
|
|
|
return $this->internal->getDescription($reflectionClass, $fieldName, $preferGetters); |
139
|
|
|
}, |
140
|
|
|
$reflectionClass, |
141
|
|
|
$fieldName, |
142
|
|
|
$preferGetters |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritDoc} |
148
|
|
|
*/ |
149
|
|
|
public function isSupported(ReflectionClass $reflectionClass): bool |
150
|
|
|
{ |
151
|
|
|
return $this->cacheCheck( |
152
|
|
|
__FUNCTION__ . ',' . $reflectionClass->name, |
153
|
|
|
function (ReflectionClass $reflectionClass) { |
154
|
|
|
if ($this->internal instanceof ObjectAccessSupportedInterface) { |
155
|
|
|
return $this->internal->isSupported($reflectionClass); |
156
|
|
|
} |
157
|
|
|
return true; |
158
|
|
|
}, |
159
|
|
|
$reflectionClass |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* {@inheritDoc} |
166
|
|
|
*/ |
167
|
|
|
public function getMethodArguments(ReflectionMethod $method, ?ReflectionClass $reflectionClass = null): array |
168
|
|
|
{ |
169
|
|
|
return $this->cacheCheck( |
170
|
|
|
__FUNCTION__ . ',' . $method->name . ',' . ($reflectionClass ? $reflectionClass->name : '(null)'), |
171
|
|
|
function (ReflectionMethod $method, ?ReflectionClass $reflectionClass = null) { |
172
|
|
|
return $this->internal->getMethodArguments($method, $reflectionClass); |
173
|
|
|
}, |
174
|
|
|
$method, |
175
|
|
|
$reflectionClass |
176
|
|
|
); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Cache helper method. |
181
|
|
|
* |
182
|
|
|
* @param string $cacheKey |
183
|
|
|
* @param Closure $callback |
184
|
|
|
* @param mixed ...$args |
185
|
|
|
* @return mixed |
186
|
|
|
*/ |
187
|
|
|
private function cacheCheck(string $cacheKey, Closure $callback, ...$args) |
188
|
|
|
{ |
189
|
|
|
$cacheKey = str_replace('\\', '|', $cacheKey); |
190
|
|
|
$cacheItem = $this->cacheItemPool->getItem($cacheKey); |
191
|
|
|
if ($cacheItem->isHit()) { |
192
|
|
|
return $cacheItem->get(); |
193
|
|
|
} |
194
|
|
|
$returnValue = $callback(...$args); |
195
|
|
|
$cacheItem->set($returnValue); |
196
|
|
|
$this->cacheItemPool->save($cacheItem); |
197
|
|
|
return $returnValue; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|