1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace micmania1\config; |
4
|
|
|
|
5
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
6
|
|
|
|
7
|
|
|
class CachedConfigCollection implements ConfigCollectionInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @const string |
11
|
|
|
*/ |
12
|
|
|
const METADATA_KEY = '__METADATA__'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @const string |
16
|
|
|
*/ |
17
|
|
|
const HISTORY_KEY = '__HISTORY__'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var CacheItemPoolInterface |
21
|
|
|
*/ |
22
|
|
|
protected $pool; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var boolean |
26
|
|
|
*/ |
27
|
|
|
protected $trackMetadata = false; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param boolean $trackMetadata |
31
|
|
|
* @param CacheItemPoolInterface $pool |
32
|
|
|
*/ |
33
|
4 |
|
public function __construct(CacheItemPoolInterface $pool, $trackMetadata = false) |
34
|
|
|
{ |
35
|
4 |
|
$this->pool = $pool; |
36
|
4 |
|
$this->trackMetadata = (bool) $trackMetadata; |
37
|
4 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
3 |
|
public function set($key, $value, $metadata = []) |
43
|
|
|
{ |
44
|
3 |
|
$key = strtolower($key); |
45
|
|
|
|
46
|
|
|
// We use null as the key to return an empty cache item |
47
|
3 |
|
$cacheItem = $this->pool->getItem($key); |
48
|
|
|
|
49
|
3 |
|
if($this->trackMetadata) { |
50
|
1 |
|
$cachedMetadata = $this->getMetadata(); |
51
|
1 |
|
$cachedHistory = $this->getHistory(); |
52
|
|
|
|
53
|
1 |
|
if($this->exists($key) && isset($cachedMetadata[$key])) { |
54
|
|
|
if(!isset($cachedHistory[$key])) { |
55
|
|
|
$cachedHistory[$key] = []; |
56
|
|
|
} |
57
|
|
|
array_unshift($cachedHistory[$key], [ |
58
|
|
|
'value' => $value, |
59
|
|
|
'metadata' => $metadata, |
60
|
|
|
]); |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
$cachedMetadata[$key] = $metadata; |
64
|
|
|
|
65
|
1 |
|
$this->saveMetadata($cachedMetadata); |
66
|
1 |
|
$this->saveHistory($cachedHistory); |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
// Save our new value |
70
|
3 |
|
$cacheItem->set($value); |
71
|
3 |
|
$this->pool->saveDeferred($cacheItem); |
72
|
3 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
1 |
|
public function get($key) |
78
|
|
|
{ |
79
|
1 |
|
$key = strtolower($key); |
80
|
1 |
|
if(!$this->exists($key)) { |
81
|
1 |
|
return null; |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
$cacheItem = $this->pool->getItem($key); |
85
|
|
|
|
86
|
1 |
|
return $cacheItem->get() ?: null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
2 |
|
public function exists($key) |
93
|
|
|
{ |
94
|
2 |
|
$key = strtolower($key); |
95
|
2 |
|
return $this->pool->hasItem($key); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
1 |
|
public function delete($key) |
102
|
|
|
{ |
103
|
1 |
|
$key = strtolower($key); |
104
|
1 |
|
$this->pool->deleteItem($key); |
105
|
1 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
1 |
|
public function deleteAll() |
111
|
|
|
{ |
112
|
1 |
|
$this->pool->clear(); |
113
|
1 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
2 |
|
public function getMetadata() |
119
|
|
|
{ |
120
|
2 |
|
return $this->getTrackingData(self::METADATA_KEY); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
2 |
|
public function getHistory() |
127
|
|
|
{ |
128
|
2 |
|
return $this->getTrackingData(self::HISTORY_KEY); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* A shortcut for tracking data (metadata and history). This will |
133
|
|
|
* always return an array, even if we're not tracking. |
134
|
|
|
* |
135
|
|
|
* @param string $key |
136
|
|
|
* |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
2 |
|
private function getTrackingData($key) |
140
|
|
|
{ |
141
|
2 |
|
if (!$this->trackMetadata) { |
142
|
1 |
|
return []; |
143
|
|
|
} |
144
|
|
|
|
145
|
1 |
|
$ket = strtolower($key); |
|
|
|
|
146
|
1 |
|
$value = $this->pool->getItem($key)->get(); |
147
|
|
|
|
148
|
1 |
|
return is_array($value) ? $value : []; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Saves the metadata to cache |
153
|
|
|
* |
154
|
|
|
* @param array $metadata |
155
|
|
|
*/ |
156
|
1 |
|
protected function saveMetadata($metadata) |
157
|
|
|
{ |
158
|
1 |
|
$cached = $this->pool->getItem(self::METADATA_KEY); |
159
|
1 |
|
$cached->set($metadata); |
160
|
|
|
|
161
|
1 |
|
$this->pool->saveDeferred($cached); |
162
|
1 |
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Saves the history to the cache |
166
|
|
|
* |
167
|
|
|
* @param array $history |
168
|
|
|
*/ |
169
|
1 |
|
protected function saveHistory($history) |
170
|
|
|
{ |
171
|
1 |
|
$cached = $this->pool->getItem(self::HISTORY_KEY); |
172
|
1 |
|
$cached->set($history); |
173
|
|
|
|
174
|
1 |
|
$this->pool->saveDeferred($cached); |
175
|
1 |
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Commits the cache |
179
|
|
|
*/ |
180
|
4 |
|
public function __destruct() |
181
|
|
|
{ |
182
|
4 |
|
$this->pool->commit(); |
183
|
4 |
|
} |
184
|
|
|
} |
185
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.