|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Metadata\Cache; |
|
6
|
|
|
|
|
7
|
|
|
use Metadata\ClassMetadata; |
|
8
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
9
|
|
|
|
|
10
|
|
|
class PsrCacheAdapter implements CacheInterface, ClearableCacheInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
private $prefix; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var CacheItemPoolInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $pool; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var CacheItemPoolInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $lastItem; |
|
26
|
|
|
|
|
27
|
2 |
|
public function __construct(string $prefix, CacheItemPoolInterface $pool) |
|
28
|
|
|
{ |
|
29
|
2 |
|
$this->prefix = $prefix; |
|
30
|
2 |
|
$this->pool = $pool; |
|
31
|
2 |
|
} |
|
32
|
|
|
|
|
33
|
2 |
|
public function load(string $class): ?ClassMetadata |
|
34
|
|
|
{ |
|
35
|
2 |
|
$this->lastItem = $this->pool->getItem($this->sanitizeCacheKey($this->prefix . $class)); |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
2 |
|
return $this->lastItem->get(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
2 |
|
public function put(ClassMetadata $metadata): void |
|
41
|
|
|
{ |
|
42
|
2 |
|
$key = $this->sanitizeCacheKey($this->prefix . $metadata->name); |
|
43
|
|
|
|
|
44
|
2 |
|
if (null === $this->lastItem || $this->lastItem->getKey() !== $key) { |
|
|
|
|
|
|
45
|
|
|
$this->lastItem = $this->pool->getItem($key); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
2 |
|
$this->pool->save($this->lastItem->set($metadata)); |
|
|
|
|
|
|
49
|
2 |
|
} |
|
50
|
|
|
|
|
51
|
2 |
|
public function evict(string $class): void |
|
52
|
|
|
{ |
|
53
|
2 |
|
$this->pool->deleteItem($this->sanitizeCacheKey($this->prefix . $class)); |
|
54
|
2 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function clear(): bool |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->pool->clear(); |
|
59
|
|
|
} |
|
60
|
2 |
|
|
|
61
|
|
|
/** |
|
62
|
2 |
|
* If anonymous class is to be cached, it contains invalid path characters that need to be removed/replaced |
|
63
|
|
|
* Example of anonymous class name: class@anonymous\x00/app/src/Controller/DefaultController.php0x7f82a7e026ec |
|
64
|
|
|
*/ |
|
65
|
|
|
private function sanitizeCacheKey(string $key): string |
|
66
|
|
|
{ |
|
67
|
|
|
return str_replace(['\\', "\0", '@', '/', '$', '{', '}', ':'], '-', $key); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..