|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Nwidart\Modules\Laravel; |
|
5
|
|
|
|
|
6
|
|
|
use Illuminate\Cache\CacheManager; |
|
7
|
|
|
use Illuminate\Container\Container; |
|
8
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
9
|
|
|
use Illuminate\Foundation\AliasLoader; |
|
10
|
|
|
use Illuminate\Foundation\ProviderRepository; |
|
11
|
|
|
use Illuminate\Support\Str; |
|
12
|
|
|
use Illuminate\Translation\Translator; |
|
13
|
|
|
use Illuminate\Support\Arr; |
|
14
|
|
|
use Nwidart\Modules\Contracts\ActivatorInterface; |
|
15
|
|
|
use Nwidart\Modules\Entities\ModuleEntity; |
|
16
|
3 |
|
use Nwidart\Modules\Json; |
|
17
|
|
|
use Nwidart\Modules\Module as BaseModule; |
|
18
|
|
|
|
|
19
|
|
|
class Module extends BaseModule |
|
20
|
3 |
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The laravel|lumen application instance. |
|
24
|
3 |
|
* |
|
25
|
|
|
* @var \Illuminate\Contracts\Foundation\Application|\Laravel\Lumen\Application |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $app; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
2 |
|
* The module name. |
|
31
|
|
|
* |
|
32
|
2 |
|
* @var |
|
33
|
2 |
|
*/ |
|
34
|
2 |
|
public $name; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The module path. |
|
38
|
|
|
* |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $path; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var array of cached Json objects, keyed by filename |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $moduleJson = []; |
|
47
|
|
|
/** |
|
48
|
|
|
* @var CacheManager |
|
49
|
|
|
*/ |
|
50
|
|
|
private $cache; |
|
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var Filesystem |
|
53
|
|
|
*/ |
|
54
|
|
|
private $files; |
|
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var Translator |
|
57
|
|
|
*/ |
|
58
|
|
|
private $translator; |
|
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var ActivatorInterface |
|
61
|
|
|
*/ |
|
62
|
|
|
private $activator; |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
private $id; |
|
65
|
|
|
|
|
66
|
|
|
private $attributes; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* The constructor. |
|
70
|
|
|
* @param Container $app |
|
71
|
|
|
* @param string $name |
|
72
|
|
|
* @param $path |
|
73
|
|
|
*/ |
|
74
|
|
|
public function __construct(Container $app, string $name, $path, $attributes = []) |
|
75
|
|
|
{ |
|
76
|
|
|
parent::__construct($app, $name, $path); |
|
77
|
|
|
$this->attributes = $attributes; |
|
78
|
|
|
$this->id = isset($this->attributes['id']) ? $this->attributes['id'] : false; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getCachedServicesPath(): string |
|
85
|
|
|
{ |
|
86
|
|
|
return Str::replaceLast('services.php', $this->getSnakeName() . '_module.php', $this->app->getCachedServicesPath()); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* {@inheritdoc} |
|
91
|
|
|
*/ |
|
92
|
|
|
public function registerProviders(): void |
|
93
|
|
|
{ |
|
94
|
|
|
(new ProviderRepository($this->app, new Filesystem(), $this->getCachedServicesPath())) |
|
95
|
|
|
->load($this->get('providers', [])); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritdoc} |
|
100
|
|
|
*/ |
|
101
|
|
|
public function registerAliases(): void |
|
102
|
|
|
{ |
|
103
|
|
|
$loader = AliasLoader::getInstance(); |
|
104
|
|
|
foreach ($this->get('aliases', []) as $aliasName => $aliasClass) { |
|
105
|
|
|
$loader->alias($aliasName, $aliasClass); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function getLaravel() |
|
110
|
|
|
{ |
|
111
|
|
|
// TODO: Implement getLaravel() method. |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function enabled(): bool |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->attributes['is_active']; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function disabled(): bool |
|
120
|
|
|
{ |
|
121
|
|
|
return !$this->attributes['is_active']; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function getId() { |
|
125
|
|
|
return $this->id; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function getAttributes() |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->attributes; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Get a specific data from json file by given the key. |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $key |
|
137
|
|
|
* @param null $default |
|
138
|
|
|
* |
|
139
|
|
|
* @return mixed |
|
140
|
|
|
*/ |
|
141
|
|
|
public function get(string $key, $default = null) |
|
142
|
|
|
{ |
|
143
|
|
|
return isset($this->attributes[$key]) ? $this->attributes[$key] : $default; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function getAlias(): string |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->attributes['alias']; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|