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