1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Caffeinated\Modules\Repositories; |
4
|
|
|
|
5
|
|
|
class LocalRepository extends Repository |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Update cached repository of module information. |
9
|
|
|
* |
10
|
|
|
* @return bool |
11
|
|
|
*/ |
12
|
|
|
public function optimize() |
13
|
|
|
{ |
14
|
|
|
$cachePath = $this->getCachePath(); |
15
|
|
|
$cache = $this->getCache(); |
16
|
|
|
$basenames = $this->getAllBasenames(); |
17
|
|
|
$modules = collect(); |
18
|
|
|
|
19
|
|
|
$basenames->each(function ($module, $key) use ($modules, $cache) { |
|
|
|
|
20
|
|
|
$temp = collect($cache->get($module)); |
21
|
|
|
$manifest = collect($this->getManifest($module)); |
22
|
|
|
|
23
|
|
|
$modules->put($module, $temp->merge($manifest)); |
24
|
|
|
}); |
25
|
|
|
|
26
|
|
|
$modules->each(function ($module) { |
27
|
|
|
if (!$module->has('enabled')) { |
28
|
|
|
$module->put('enabled', config('modules.enabled', true)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
if (!$module->has('order')) { |
32
|
|
|
$module->put('order', 9001); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return $module; |
36
|
|
|
}); |
37
|
|
|
|
38
|
|
|
$content = json_encode($modules->all(), JSON_PRETTY_PRINT); |
39
|
|
|
|
40
|
|
|
return $this->files->put($cachePath, $content); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get all modules. |
45
|
|
|
* |
46
|
|
|
* @return Collection |
47
|
|
|
*/ |
48
|
|
|
public function all() |
49
|
|
|
{ |
50
|
|
|
return $this->getCache()->sortBy('order'); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get all module slugs. |
55
|
|
|
* |
56
|
|
|
* @return Collection |
57
|
|
|
*/ |
58
|
|
|
public function slugs() |
59
|
|
|
{ |
60
|
|
|
$slugs = collect(); |
61
|
|
|
|
62
|
|
|
$this->all()->each(function ($item, $key) use ($slugs) { |
|
|
|
|
63
|
|
|
$slugs->push($item['slug']); |
64
|
|
|
}); |
65
|
|
|
|
66
|
|
|
return $slugs; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get modules based on where clause. |
71
|
|
|
* |
72
|
|
|
* @param string $key |
73
|
|
|
* @param mixed $value |
74
|
|
|
* |
75
|
|
|
* @return Collection |
76
|
|
|
*/ |
77
|
|
|
public function where($key, $value) |
78
|
|
|
{ |
79
|
|
|
return collect($this->all()->where($key, $value)->first()); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Sort modules by given key in ascending order. |
84
|
|
|
* |
85
|
|
|
* @param string $key |
86
|
|
|
* |
87
|
|
|
* @return Collection |
88
|
|
|
*/ |
89
|
|
|
public function sortBy($key) |
90
|
|
|
{ |
91
|
|
|
$collection = $this->all(); |
92
|
|
|
|
93
|
|
|
return $collection->sortBy($key); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Sort modules by given key in ascending order. |
98
|
|
|
* |
99
|
|
|
* @param string $key |
100
|
|
|
* |
101
|
|
|
* @return Collection |
102
|
|
|
*/ |
103
|
|
|
public function sortByDesc($key) |
104
|
|
|
{ |
105
|
|
|
$collection = $this->all(); |
106
|
|
|
|
107
|
|
|
return $collection->sortByDesc($key); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Determines if the given module exists. |
112
|
|
|
* |
113
|
|
|
* @param string $slug |
114
|
|
|
* |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
|
|
public function exists($slug) |
118
|
|
|
{ |
119
|
|
|
return $this->slugs()->contains(str_slug($slug)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns count of all modules. |
124
|
|
|
* |
125
|
|
|
* @return int |
126
|
|
|
*/ |
127
|
|
|
public function count() |
128
|
|
|
{ |
129
|
|
|
return $this->all()->count(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get a module property value. |
134
|
|
|
* |
135
|
|
|
* @param string $property |
136
|
|
|
* @param mixed $default |
137
|
|
|
* |
138
|
|
|
* @return mixed |
139
|
|
|
*/ |
140
|
|
|
public function get($property, $default = null) |
141
|
|
|
{ |
142
|
|
|
list($slug, $key) = explode('::', $property); |
143
|
|
|
|
144
|
|
|
$module = $this->where('slug', $slug); |
145
|
|
|
|
146
|
|
|
return $module->get($key, $default); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Set the given module property value. |
151
|
|
|
* |
152
|
|
|
* @param string $property |
153
|
|
|
* @param mixed $value |
154
|
|
|
* |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
|
|
public function set($property, $value) |
158
|
|
|
{ |
159
|
|
|
list($slug, $key) = explode('::', $property); |
160
|
|
|
|
161
|
|
|
$cachePath = $this->getCachePath(); |
162
|
|
|
$cache = $this->getCache(); |
163
|
|
|
$module = $this->where('slug', $slug); |
164
|
|
|
$moduleKey = $module->keys()->first(); |
165
|
|
|
$values = $module->first(); |
166
|
|
|
|
167
|
|
|
if (isset($values[$key])) { |
168
|
|
|
unset($values[$key]); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$values[$key] = $value; |
172
|
|
|
|
173
|
|
|
$module = collect([$moduleKey => $values]); |
174
|
|
|
|
175
|
|
|
$merged = $cache->merge($module); |
176
|
|
|
$content = json_encode($merged->all(), JSON_PRETTY_PRINT); |
177
|
|
|
|
178
|
|
|
return $this->files->put($cachePath, $content); |
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get all enabled modules. |
183
|
|
|
* |
184
|
|
|
* @return Collection |
185
|
|
|
*/ |
186
|
|
|
public function enabled() |
187
|
|
|
{ |
188
|
|
|
return $this->all()->where('enabled', true); |
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Get all disabled modules. |
193
|
|
|
* |
194
|
|
|
* @return Collection |
195
|
|
|
*/ |
196
|
|
|
public function disabled() |
197
|
|
|
{ |
198
|
|
|
return $this->all()->where('enabled', false); |
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Check if specified module is enabled. |
203
|
|
|
* |
204
|
|
|
* @param string $slug |
205
|
|
|
* |
206
|
|
|
* @return bool |
207
|
|
|
*/ |
208
|
|
|
public function isEnabled($slug) |
209
|
|
|
{ |
210
|
|
|
$module = $this->where('slug', $slug) |
211
|
|
|
->first(); |
212
|
|
|
|
213
|
|
|
return $module['enabled'] === true; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Check if specified module is disabled. |
218
|
|
|
* |
219
|
|
|
* @param string $slug |
220
|
|
|
* |
221
|
|
|
* @return bool |
222
|
|
|
*/ |
223
|
|
|
public function isDisabled($slug) |
224
|
|
|
{ |
225
|
|
|
$module = $this->where('slug', $slug) |
226
|
|
|
->first(); |
227
|
|
|
|
228
|
|
|
return $module['enabled'] === false; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Enables the specified module. |
233
|
|
|
* |
234
|
|
|
* @param string $slug |
235
|
|
|
* |
236
|
|
|
* @return bool |
237
|
|
|
*/ |
238
|
|
|
public function enable($slug) |
239
|
|
|
{ |
240
|
|
|
return $this->set($slug.'::enabled', true); |
|
|
|
|
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Disables the specified module. |
245
|
|
|
* |
246
|
|
|
* @param string $slug |
247
|
|
|
* |
248
|
|
|
* @return bool |
249
|
|
|
*/ |
250
|
|
|
public function disable($slug) |
251
|
|
|
{ |
252
|
|
|
return $this->set($slug.'::enabled', false); |
|
|
|
|
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Get the contents of the cache file. |
257
|
|
|
* |
258
|
|
|
* The cache file lists all module slugs and their |
259
|
|
|
* enabled or disabled status. This can be used to |
260
|
|
|
* filter out modules depending on their status. |
261
|
|
|
* |
262
|
|
|
* @return Collection |
263
|
|
|
*/ |
264
|
|
|
public function getCache() |
265
|
|
|
{ |
266
|
|
|
$cachePath = $this->getCachePath(); |
267
|
|
|
|
268
|
|
|
if (!$this->files->exists($cachePath)) { |
269
|
|
|
$content = json_encode(array(), JSON_PRETTY_PRINT); |
270
|
|
|
|
271
|
|
|
$this->files->put($cachePath, $content); |
272
|
|
|
|
273
|
|
|
$this->optimize(); |
274
|
|
|
|
275
|
|
|
return collect(json_decode($content, true)); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
return collect(json_decode($this->files->get($cachePath), true)); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Get the path to the cache file. |
283
|
|
|
* |
284
|
|
|
* @return string |
285
|
|
|
*/ |
286
|
|
|
protected function getCachePath() |
287
|
|
|
{ |
288
|
|
|
return storage_path('app/modules.json'); |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.