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