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