|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace WyriHaximus\React\Cache; |
|
6
|
|
|
|
|
7
|
|
|
use React\Cache\CacheInterface; |
|
8
|
|
|
|
|
9
|
|
|
use function serialize; |
|
|
|
|
|
|
10
|
|
|
use function unserialize; |
|
11
|
|
|
|
|
12
|
|
|
final class Serialize implements CacheInterface |
|
13
|
|
|
{ |
|
14
|
|
|
public function __construct(private readonly CacheInterface $cache) |
|
15
|
|
|
{ |
|
16
|
10 |
|
} |
|
17
|
|
|
|
|
18
|
10 |
|
/** |
|
19
|
10 |
|
* @inheritDoc |
|
20
|
|
|
* @phpstan-ignore-next-line |
|
21
|
|
|
*/ |
|
22
|
|
|
public function get($key, $default = null) |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @psalm-suppress TooManyTemplateParams |
|
26
|
2 |
|
* @psalm-suppress MissingClosureParamType |
|
27
|
|
|
*/ |
|
28
|
|
|
return $this->cache->get($key, $default)->then(static function ($result) use ($default): mixed { |
|
29
|
2 |
|
if ($result === null || $result === $default) { |
|
30
|
1 |
|
return $default; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
1 |
|
/** |
|
34
|
2 |
|
* @psalm-suppress MixedArgument |
|
35
|
|
|
*/ |
|
36
|
|
|
return unserialize($result); |
|
37
|
|
|
}); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @inheritDoc |
|
42
|
|
|
* @phpstan-ignore-next-line |
|
43
|
1 |
|
*/ |
|
44
|
|
|
public function set($key, $value, $ttl = null) |
|
45
|
1 |
|
{ |
|
46
|
|
|
/** |
|
47
|
|
|
* @psalm-suppress TooManyTemplateParams |
|
48
|
|
|
*/ |
|
49
|
|
|
return $this->cache->set($key, serialize($value), $ttl); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
/** |
|
53
|
|
|
* @inheritDoc |
|
54
|
1 |
|
*/ |
|
55
|
|
|
public function delete($key) |
|
56
|
|
|
{ |
|
57
|
2 |
|
/** |
|
58
|
|
|
* @psalm-suppress TooManyTemplateParams |
|
59
|
|
|
*/ |
|
60
|
2 |
|
return $this->cache->delete($key); |
|
61
|
2 |
|
} |
|
62
|
1 |
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @inheritDoc |
|
65
|
1 |
|
* @phpstan-ignore-next-line |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getMultiple(array $keys, $default = null) |
|
68
|
2 |
|
{ |
|
69
|
2 |
|
/** |
|
70
|
|
|
* @psalm-suppress TooManyTemplateParams |
|
71
|
|
|
* @psalm-suppress MissingClosureParamType |
|
72
|
1 |
|
*/ |
|
73
|
|
|
return $this->cache->getMultiple($keys, $default)->then(static function ($results) use ($default) { |
|
74
|
1 |
|
/** |
|
75
|
1 |
|
* @psalm-suppress MixedAssignment |
|
76
|
|
|
*/ |
|
77
|
|
|
foreach ($results as $key => $result) { |
|
78
|
1 |
|
if ($result === null || $result === $default) { |
|
79
|
|
|
continue; |
|
80
|
|
|
} |
|
81
|
1 |
|
|
|
82
|
|
|
/** |
|
83
|
1 |
|
* @psalm-suppress MixedAssignment |
|
84
|
|
|
* @psalm-suppress MixedArrayAssignment |
|
85
|
|
|
* @psalm-suppress MixedArrayOffset |
|
86
|
1 |
|
* @psalm-suppress MixedArgument |
|
87
|
|
|
*/ |
|
88
|
1 |
|
$results[$key] = unserialize($result); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
return $results; |
|
92
|
|
|
}); |
|
93
|
1 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @inheritDoc |
|
97
|
|
|
* @phpstan-ignore-next-line |
|
98
|
|
|
*/ |
|
99
|
|
|
public function setMultiple(array $values, $ttl = null) |
|
100
|
|
|
{ |
|
101
|
|
|
/** |
|
102
|
|
|
* @psalm-suppress MixedAssignment |
|
103
|
|
|
*/ |
|
104
|
|
|
foreach ($values as $key => $value) { |
|
105
|
|
|
$values[$key] = serialize($value); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @psalm-suppress TooManyTemplateParams |
|
110
|
|
|
*/ |
|
111
|
|
|
return $this->cache->setMultiple($values, $ttl); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @inheritDoc |
|
116
|
|
|
*/ |
|
117
|
|
|
public function deleteMultiple(array $keys) |
|
118
|
|
|
{ |
|
119
|
|
|
/** |
|
120
|
|
|
* @psalm-suppress TooManyTemplateParams |
|
121
|
|
|
*/ |
|
122
|
|
|
return $this->cache->deleteMultiple($keys); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @inheritDoc |
|
127
|
|
|
*/ |
|
128
|
|
|
public function clear() |
|
129
|
|
|
{ |
|
130
|
|
|
/** |
|
131
|
|
|
* @psalm-suppress TooManyTemplateParams |
|
132
|
|
|
*/ |
|
133
|
|
|
return $this->cache->clear(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @inheritDoc |
|
138
|
|
|
*/ |
|
139
|
|
|
public function has($key) |
|
140
|
|
|
{ |
|
141
|
|
|
/** |
|
142
|
|
|
* @psalm-suppress TooManyTemplateParams |
|
143
|
|
|
*/ |
|
144
|
|
|
return $this->cache->has($key); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: