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