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