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