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