/
CachedConfigCollection.php
259 lines (223 loc) · 5.56 KB
/
CachedConfigCollection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
namespace SilverStripe\Config\Collections;
use BadMethodCallException;
use Psr\SimpleCache\CacheInterface;
use SilverStripe\Config\Middleware\MiddlewareAware;
class CachedConfigCollection implements ConfigCollectionInterface
{
use MiddlewareAware;
/**
* @const string
*/
const CACHE_KEY = '__CACHE__';
/**
* @var CacheInterface
*/
protected $cache;
/**
* Nested config to delegate to
*
* @var ConfigCollectionInterface
*/
protected $collection;
/**
* Stores a hash to allow comparison
*/
private ?string $collectionHash = null;
/**
* @var callable
*/
protected $collectionCreator;
/**
* @var bool
*/
protected $flush = false;
/**
* Set to true while building config.
* Used to protect against infinite loops.
*
* @var bool
*/
protected $building = false;
/**
* Injectable factory for nesting config.
* This callback will be passed the inner ConfigCollection
*
* @var callable
*/
protected $nestFactory = null;
/**
* @return static
*/
public static function create()
{
return new static();
}
/**
* Get callback for nesting the inner collection
*
* @return callable
*/
public function getNestFactory()
{
return $this->nestFactory;
}
/**
* Set callback for nesting the inner collection
*
* @param callable $factory
* @return $this
*/
public function setNestFactory(callable $factory)
{
$this->nestFactory = $factory;
return $this;
}
public function get($class, $name = null, $excludeMiddleware = 0)
{
return $this->getCollection()->get($class, $name, $excludeMiddleware);
}
public function getAll()
{
return $this->getCollection()->getAll();
}
public function exists($class, $name = null, $excludeMiddleware = 0)
{
return $this->getCollection()->exists($class, $name, $excludeMiddleware);
}
public function getMetadata()
{
return $this->getCollection()->getMetadata();
}
public function getHistory()
{
return $this->getCollection()->getHistory();
}
private function getHash(): string
{
return md5(serialize($this->collection));
}
/**
* Get or build collection
*
* @return ConfigCollectionInterface
*/
public function getCollection()
{
// Get current collection
if ($this->collection) {
return $this->collection;
}
// Load from cache (unless flushing)
if (!$this->flush) {
$this->collection = $this->cache->get(self::CACHE_KEY);
if ($this->collection) {
$this->collectionHash = $this->getHash();
return $this->collection;
}
}
// Protect against infinity loop
if ($this->building) {
throw new BadMethodCallException("Infinite loop detected. Config could not be bootstrapped.");
}
$this->building = true;
// Cache missed
try {
$this->collection = call_user_func($this->collectionCreator);
} finally {
$this->building = false;
}
// Save immediately.
// Note additional deferred save can occur in _destruct()
$this->cache->set(self::CACHE_KEY, $this->collection);
$this->collectionHash = $this->getHash();
return $this->collection;
}
/**
* Commits the cache
*/
public function __destruct()
{
// Ensure back-end cache is updated
if ($this->collection && $this->collectionHash) {
if ($this->getHash() !== $this->collectionHash) {
$this->cache->set(self::CACHE_KEY, $this->collection);
}
// Prevent double-destruct
$this->collection = null;
$this->collectionHash = null;
}
}
public function nest()
{
$collection = $this->getCollection();
$factory = $this->getNestFactory();
if ($factory) {
return $factory($collection);
}
// Fall back to regular nest
return $collection->nest();
}
/**
* Set a PSR-16 cache
*
* @param CacheInterface $cache
* @return $this
*/
public function setCache(CacheInterface $cache)
{
$this->cache = $cache;
if ($this->flush) {
$cache->clear();
}
return $this;
}
/**
* @param callable $collectionCreator
* @return $this
*/
public function setCollectionCreator($collectionCreator)
{
$this->collectionCreator = $collectionCreator;
return $this;
}
/**
* @return callable
*/
public function getCollectionCreator()
{
return $this->collectionCreator;
}
/**
* @return CacheInterface
*/
public function getCache()
{
return $this->cache;
}
/**
* @param bool $flush
* @return $this
*/
public function setFlush($flush)
{
$this->flush = $flush;
if ($flush && $this->cache) {
$this->cache->clear();
}
return $this;
}
/**
* @return bool
*/
public function getFlush()
{
return $this->flush;
}
public function setMiddlewares($middlewares)
{
throw new BadMethodCallException(
"Please apply middleware to collection factory via setCollectionCreator()"
);
}
}