1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nwidart\Modules; |
4
|
|
|
|
5
|
|
|
use Illuminate\Container\Container; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Illuminate\Support\ServiceProvider; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
use Illuminate\Support\Traits\Macroable; |
10
|
|
|
|
11
|
|
|
abstract class Module extends ServiceProvider |
12
|
|
|
{ |
13
|
|
|
use Macroable; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The laravel|lumen application instance. |
17
|
|
|
* |
18
|
|
|
* @var \Illuminate\Contracts\Foundation\Application|\Laravel\Lumen\Application |
19
|
|
|
*/ |
20
|
|
|
protected $app; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The module name. |
24
|
|
|
* |
25
|
|
|
* @var |
26
|
|
|
*/ |
27
|
|
|
protected $name; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The module path. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $path; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array of cached Json objects, keyed by filename |
38
|
|
|
*/ |
39
|
|
|
protected $moduleJson = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The constructor. |
43
|
|
|
* |
44
|
|
|
* @param Container $app |
45
|
|
|
* @param $name |
46
|
|
|
* @param $path |
47
|
|
|
*/ |
48
|
153 |
|
public function __construct(Container $app, $name, $path) |
49
|
|
|
{ |
50
|
153 |
|
parent::__construct($app); |
51
|
153 |
|
$this->name = $name; |
52
|
153 |
|
$this->path = $path; |
53
|
153 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get laravel instance. |
57
|
|
|
* |
58
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Laravel\Lumen\Application |
59
|
|
|
*/ |
60
|
|
|
public function getLaravel() |
61
|
|
|
{ |
62
|
|
|
return $this->app; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get name. |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
2 |
|
public function getName() |
71
|
|
|
{ |
72
|
2 |
|
return $this->name; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get name in lower case. |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
115 |
|
public function getLowerName() |
81
|
|
|
{ |
82
|
115 |
|
return strtolower($this->name); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get name in studly case. |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
101 |
|
public function getStudlyName() |
91
|
|
|
{ |
92
|
101 |
|
return Str::studly($this->name); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get name in snake case. |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
6 |
|
public function getSnakeName() |
101
|
|
|
{ |
102
|
6 |
|
return Str::snake($this->name); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get description. |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
2 |
|
public function getDescription() |
111
|
|
|
{ |
112
|
2 |
|
return $this->get('description'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get alias. |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
4 |
|
public function getAlias() |
121
|
|
|
{ |
122
|
4 |
|
return $this->get('alias'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get priority. |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
public function getPriority() |
131
|
|
|
{ |
132
|
|
|
return $this->get('priority'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get module requirements. |
137
|
|
|
* |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
3 |
|
public function getRequires() |
141
|
|
|
{ |
142
|
3 |
|
return $this->get('requires'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get path. |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
136 |
|
public function getPath() |
151
|
|
|
{ |
152
|
136 |
|
return $this->path; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Set path. |
157
|
|
|
* |
158
|
|
|
* @param string $path |
159
|
|
|
* |
160
|
|
|
* @return $this |
161
|
|
|
*/ |
162
|
|
|
public function setPath($path) |
163
|
|
|
{ |
164
|
|
|
$this->path = $path; |
165
|
|
|
|
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Bootstrap the application events. |
171
|
|
|
*/ |
172
|
2 |
|
public function boot() |
173
|
|
|
{ |
174
|
2 |
|
if (config('modules.register.translations', true) === true) { |
175
|
2 |
|
$this->registerTranslation(); |
176
|
|
|
} |
177
|
|
|
|
178
|
2 |
|
if ($this->isLoadFilesOnBoot()) { |
179
|
|
|
$this->registerFiles(); |
180
|
|
|
} |
181
|
|
|
|
182
|
2 |
|
$this->fireEvent('boot'); |
183
|
2 |
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Register module's translation. |
187
|
|
|
* |
188
|
|
|
* @return void |
189
|
|
|
*/ |
190
|
2 |
|
protected function registerTranslation() |
191
|
|
|
{ |
192
|
2 |
|
$lowerName = $this->getLowerName(); |
193
|
|
|
|
194
|
2 |
|
$langPath = $this->getPath() . '/Resources/lang'; |
195
|
|
|
|
196
|
2 |
|
if (is_dir($langPath)) { |
197
|
2 |
|
$this->loadTranslationsFrom($langPath, $lowerName); |
198
|
|
|
} |
199
|
2 |
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Get json contents from the cache, setting as needed. |
203
|
|
|
* |
204
|
|
|
* @param string $file |
205
|
|
|
* |
206
|
|
|
* @return Json |
207
|
|
|
*/ |
208
|
35 |
|
public function json($file = null) : Json |
209
|
|
|
{ |
210
|
35 |
|
if ($file === null) { |
211
|
33 |
|
$file = 'module.json'; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return Arr::get($this->moduleJson, $file, function () use ($file) { |
215
|
35 |
|
return $this->moduleJson[$file] = new Json($this->getPath() . '/' . $file, $this->app['files']); |
216
|
35 |
|
}); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Get a specific data from json file by given the key. |
221
|
|
|
* |
222
|
|
|
* @param string $key |
223
|
|
|
* @param null $default |
224
|
|
|
* |
225
|
|
|
* @return mixed |
226
|
|
|
*/ |
227
|
24 |
|
public function get(string $key, $default = null) |
228
|
|
|
{ |
229
|
24 |
|
return $this->json()->get($key, $default); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Get a specific data from composer.json file by given the key. |
234
|
|
|
* |
235
|
|
|
* @param $key |
236
|
|
|
* @param null $default |
237
|
|
|
* |
238
|
|
|
* @return mixed |
239
|
|
|
*/ |
240
|
2 |
|
public function getComposerAttr($key, $default = null) |
241
|
|
|
{ |
242
|
2 |
|
return $this->json('composer.json')->get($key, $default); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Register the module. |
247
|
|
|
*/ |
248
|
|
|
public function register() |
249
|
|
|
{ |
250
|
|
|
$this->registerAliases(); |
251
|
|
|
|
252
|
|
|
$this->registerProviders(); |
253
|
|
|
|
254
|
|
|
if ($this->isLoadFilesOnBoot() === false) { |
255
|
|
|
$this->registerFiles(); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
$this->fireEvent('register'); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Register the module event. |
263
|
|
|
* |
264
|
|
|
* @param string $event |
265
|
|
|
*/ |
266
|
8 |
|
protected function fireEvent($event) |
267
|
|
|
{ |
268
|
8 |
|
$this->app['events']->dispatch(sprintf('modules.%s.' . $event, $this->getLowerName()), [$this]); |
269
|
8 |
|
} |
270
|
|
|
/** |
271
|
|
|
* Register the aliases from this module. |
272
|
|
|
*/ |
273
|
|
|
abstract public function registerAliases(); |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Register the service providers from this module. |
277
|
|
|
*/ |
278
|
|
|
abstract public function registerProviders(); |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Get the path to the cached *_module.php file. |
282
|
|
|
* |
283
|
|
|
* @return string |
284
|
|
|
*/ |
285
|
|
|
abstract public function getCachedServicesPath(); |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Register the files from this module. |
289
|
|
|
*/ |
290
|
|
|
protected function registerFiles() |
291
|
|
|
{ |
292
|
|
|
foreach ($this->get('files', []) as $file) { |
293
|
|
|
include $this->path . '/' . $file; |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Handle call __toString. |
299
|
|
|
* |
300
|
|
|
* @return string |
301
|
|
|
*/ |
302
|
3 |
|
public function __toString() |
303
|
|
|
{ |
304
|
3 |
|
return $this->getStudlyName(); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Determine whether the given status same with the current module status. |
309
|
|
|
* |
310
|
|
|
* @param $status |
311
|
|
|
* |
312
|
|
|
* @return bool |
313
|
|
|
*/ |
314
|
11 |
|
public function isStatus($status) : bool |
315
|
|
|
{ |
316
|
11 |
|
return $this->get('active', 0) === $status; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Determine whether the current module activated. |
321
|
|
|
* |
322
|
|
|
* @return bool |
323
|
|
|
*/ |
324
|
6 |
|
public function enabled() : bool |
325
|
|
|
{ |
326
|
6 |
|
return $this->isStatus(1); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Determine whether the current module not disabled. |
331
|
|
|
* |
332
|
|
|
* @return bool |
333
|
|
|
*/ |
334
|
2 |
|
public function disabled() : bool |
335
|
|
|
{ |
336
|
2 |
|
return !$this->enabled(); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Set active state for current module. |
341
|
|
|
* |
342
|
|
|
* @param $active |
343
|
|
|
* |
344
|
|
|
* @return bool |
345
|
|
|
*/ |
346
|
6 |
|
public function setActive($active) |
347
|
|
|
{ |
348
|
6 |
|
return $this->json()->set('active', $active)->save(); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Disable the current module. |
353
|
|
|
*/ |
354
|
3 |
|
public function disable() |
355
|
|
|
{ |
356
|
3 |
|
$this->fireEvent('disabling'); |
357
|
|
|
|
358
|
3 |
|
$this->setActive(0); |
359
|
3 |
|
$this->flushCache(); |
360
|
|
|
|
361
|
3 |
|
$this->fireEvent('disabled'); |
362
|
3 |
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Enable the current module. |
366
|
|
|
*/ |
367
|
3 |
|
public function enable() |
368
|
|
|
{ |
369
|
3 |
|
$this->fireEvent('enabling'); |
370
|
|
|
|
371
|
3 |
|
$this->setActive(1); |
372
|
3 |
|
$this->flushCache(); |
373
|
|
|
|
374
|
3 |
|
$this->fireEvent('enabled'); |
375
|
3 |
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Delete the current module. |
379
|
|
|
* |
380
|
|
|
* @return bool |
381
|
|
|
*/ |
382
|
2 |
|
public function delete() |
383
|
|
|
{ |
384
|
2 |
|
return $this->json()->getFilesystem()->deleteDirectory($this->getPath()); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Get extra path. |
389
|
|
|
* |
390
|
|
|
* @param string $path |
391
|
|
|
* |
392
|
|
|
* @return string |
393
|
|
|
*/ |
394
|
3 |
|
public function getExtraPath(string $path) : string |
395
|
|
|
{ |
396
|
3 |
|
return $this->getPath() . '/' . $path; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* Handle call to __get method. |
401
|
|
|
* |
402
|
|
|
* @param $key |
403
|
|
|
* |
404
|
|
|
* @return mixed |
405
|
|
|
*/ |
406
|
|
|
public function __get($key) |
407
|
|
|
{ |
408
|
|
|
return $this->get($key); |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* Check if can load files of module on boot method. |
413
|
|
|
* |
414
|
|
|
* @return bool |
415
|
|
|
*/ |
416
|
2 |
|
protected function isLoadFilesOnBoot() |
417
|
|
|
{ |
418
|
2 |
|
return config('modules.register.files', 'register') === 'boot' && |
419
|
|
|
// force register method if option == boot && app is AsgardCms |
420
|
2 |
|
!class_exists('\Modules\Core\Foundation\AsgardCms'); |
421
|
|
|
} |
422
|
|
|
|
423
|
6 |
|
private function flushCache(): void |
424
|
|
|
{ |
425
|
6 |
|
if (config('modules.cache.enabled')) { |
426
|
|
|
$this->app['cache']->store()->flush(); |
427
|
|
|
} |
428
|
6 |
|
} |
429
|
|
|
} |
430
|
|
|
|