1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Cache; |
6
|
|
|
|
7
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
|
|
|
8
|
|
|
use Traversable; |
9
|
|
|
use Yiisoft\Cache\Exception\InvalidArgumentException; |
10
|
|
|
|
11
|
|
|
use function array_fill_keys; |
12
|
|
|
use function array_keys; |
13
|
|
|
use function array_map; |
14
|
|
|
use function is_string; |
15
|
|
|
use function iterator_to_array; |
16
|
|
|
use function strpbrk; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* NullCache does not cache anything reporting success for all methods calls. |
20
|
|
|
* |
21
|
|
|
* By replacing it with some other cache component, one can quickly switch from non-caching mode to caching mode. |
22
|
|
|
* |
23
|
|
|
* See {@see \Psr\SimpleCache\CacheInterface} for common cache operations that NullCache supports. |
24
|
|
|
*/ |
25
|
|
|
final class NullCache implements CacheInterface |
26
|
|
|
{ |
27
|
2 |
|
public function get($key, $default = null) |
28
|
|
|
{ |
29
|
2 |
|
$this->validateKey($key); |
30
|
2 |
|
return $default; |
31
|
|
|
} |
32
|
|
|
|
33
|
2 |
|
public function set($key, $value, $ttl = null): bool |
34
|
|
|
{ |
35
|
2 |
|
$this->validateKey($key); |
36
|
2 |
|
return true; |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
public function delete($key): bool |
40
|
|
|
{ |
41
|
1 |
|
$this->validateKey($key); |
42
|
1 |
|
return true; |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
public function clear(): bool |
46
|
|
|
{ |
47
|
1 |
|
return true; |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
public function getMultiple($keys, $default = null): iterable |
51
|
|
|
{ |
52
|
1 |
|
$keys = $this->iterableToArray($keys); |
53
|
1 |
|
$this->validateKeys($keys); |
54
|
1 |
|
return array_fill_keys($keys, $default); |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
public function setMultiple($values, $ttl = null): bool |
58
|
|
|
{ |
59
|
1 |
|
$values = $this->iterableToArray($values); |
60
|
1 |
|
$this->validateKeysOfValues($values); |
61
|
1 |
|
return true; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
public function deleteMultiple($keys): bool |
65
|
|
|
{ |
66
|
1 |
|
$keys = $this->iterableToArray($keys); |
67
|
1 |
|
$this->validateKeys($keys); |
68
|
1 |
|
return true; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
public function has($key): bool |
72
|
|
|
{ |
73
|
1 |
|
$this->validateKey($key); |
74
|
1 |
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param mixed $iterable |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
3 |
|
private function iterableToArray($iterable): array |
82
|
|
|
{ |
83
|
3 |
|
return $iterable instanceof Traversable ? iterator_to_array($iterable) : (array) $iterable; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param mixed $key |
88
|
|
|
*/ |
89
|
8 |
|
private function validateKey($key): void |
90
|
|
|
{ |
91
|
8 |
|
if (!is_string($key) || strpbrk($key, '{}()/\@:')) { |
92
|
|
|
throw new InvalidArgumentException('Invalid key value.'); |
93
|
|
|
} |
94
|
8 |
|
} |
95
|
|
|
|
96
|
3 |
|
private function validateKeys(array $keys): void |
97
|
|
|
{ |
98
|
3 |
|
foreach ($keys as $key) { |
99
|
3 |
|
$this->validateKey($key); |
100
|
|
|
} |
101
|
3 |
|
} |
102
|
|
|
|
103
|
1 |
|
private function validateKeysOfValues(array $values): void |
104
|
|
|
{ |
105
|
1 |
|
$keys = array_map('strval', array_keys($values)); |
106
|
1 |
|
$this->validateKeys($keys); |
107
|
1 |
|
} |
108
|
|
|
} |
109
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: