forked from statamic/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndex.php
More file actions
176 lines (133 loc) · 3.52 KB
/
Index.php
File metadata and controls
176 lines (133 loc) · 3.52 KB
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
<?php
namespace Statamic\Stache\Indexes;
use Statamic\Facades\Stache;
use Statamic\Statamic;
abstract class Index
{
protected $store;
protected $name;
protected $items = [];
protected $loaded = false;
private static array $loadingStack = [];
public function __construct($store, $name)
{
$this->store = $store;
$this->name = $name;
}
public function name()
{
return $this->name;
}
public function items()
{
return collect($this->items);
}
public function values()
{
return array_values($this->items);
}
public function keys()
{
return array_keys($this->items);
}
public function get($key)
{
return $this->items[$key] ?? null;
}
public function has($key)
{
return array_key_exists($key, $this->items);
}
public function put($key, $value)
{
$this->items[$key] = $value;
}
public function push($value)
{
$this->items[] = $value;
}
public function load()
{
if ($this->loaded) {
return $this;
}
$loadingKey = $this->store->key().'/'.$this->name;
$currentlyLoadingThis = in_array($loadingKey, static::$loadingStack);
static::$loadingStack[] = $loadingKey;
try {
$this->loaded = true;
if (Statamic::isWorker() && ! $currentlyLoadingThis) {
$this->loaded = false;
}
debugbar()->addMessage("Loading index: {$loadingKey}", 'stache');
$this->items = Stache::cacheStore()->get($this->cacheKey());
if ($this->items === null) {
$this->update();
}
$this->store->cacheIndexUsage($this);
} finally {
array_pop(static::$loadingStack);
}
return $this;
}
public function update()
{
if (! Stache::shouldUpdateIndexes()) {
return $this;
}
debugbar()->addMessage("Updating index: {$this->store->key()}/{$this->name}", 'stache');
$this->items = $this->getItems();
$this->cache();
return $this;
}
public function isCached()
{
return Stache::cacheStore()->has($this->cacheKey());
}
public function cache()
{
Stache::cacheStore()->forever($this->cacheKey(), $this->items);
}
public function updateItem($item)
{
$this->load();
$this->put($this->store->getItemKey($item), $this->getItemValue($item));
$this->cache();
}
public function forgetItem($key)
{
$this->load();
unset($this->items[$key]);
$this->cache();
}
abstract public function getItems();
public function cacheKey()
{
$searches = ['.', '/'];
$replacements = ['::', '->'];
if (windows_os()) {
$replacements[1] = '-]';
$searches[] = '->';
$replacements[] = '-]';
}
return vsprintf('stache::indexes::%s::%s', [
$this->store->key(),
str_replace($searches, $replacements, $this->name),
]);
}
public function clear()
{
$this->loaded = false;
$this->items = null;
Stache::cacheStore()->forget($this->cacheKey());
}
/** @deprecated */
public static function currentlyLoading()
{
return end(static::$loadingStack) ?: null;
}
public static function isLoading(): bool
{
return ! empty(static::$loadingStack);
}
}