1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mnabialek\LaravelModular\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Foundation\Application; |
6
|
|
|
use Mnabialek\LaravelModular\Services\Config; |
7
|
|
|
use Mnabialek\LaravelModular\Traits\Normalizer; |
8
|
|
|
use Mnabialek\LaravelModular\Traits\Replacer; |
9
|
|
|
|
10
|
|
|
class Module |
11
|
|
|
{ |
12
|
|
|
use Normalizer, Replacer; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var |
16
|
|
|
*/ |
17
|
|
|
protected $name; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $options; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Config |
26
|
|
|
*/ |
27
|
|
|
protected $config; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Application |
31
|
|
|
*/ |
32
|
|
|
protected $laravel; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Module constructor. |
36
|
|
|
* |
37
|
|
|
* @param string $name |
38
|
|
|
* @param Application $application |
39
|
|
|
* @param array $options |
40
|
|
|
*/ |
41
|
|
|
public function __construct( |
42
|
|
|
$name, |
43
|
|
|
Application $application, |
44
|
|
|
array $options = [] |
45
|
|
|
) { |
46
|
|
|
$this->name = $name; |
47
|
|
|
$this->options = collect($options); |
|
|
|
|
48
|
|
|
$this->laravel = $application; |
49
|
|
|
$this->config = $application['modular.config']; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get module name |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function name() |
58
|
|
|
{ |
59
|
|
|
return $this->name; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get module seeder class name (with namespace) |
64
|
|
|
* |
65
|
|
|
* @param string|null $class |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function seederClass($class = null) |
70
|
|
|
{ |
71
|
|
|
return $this->fileClass('seeder', $class); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get module service provider class |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function serviceProviderClass() |
80
|
|
|
{ |
81
|
|
|
return $this->fileClass('serviceProvider'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get file class |
86
|
|
|
* |
87
|
|
|
* @param string $type |
88
|
|
|
* @param string|null $class |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
protected function fileClass($type, $class = null) |
93
|
|
|
{ |
94
|
|
|
$filename = $type . 'File'; |
95
|
|
|
$namespace = $type . 'Namespace'; |
96
|
|
|
|
97
|
|
|
$class = $class ?: basename($this->config->$filename(), '.php'); |
98
|
|
|
|
99
|
|
|
return $this->replace($this->config->modulesNamespace() . '\\' . |
100
|
|
|
$this->name() . '\\' . $this->config->$namespace() . '\\' . |
101
|
|
|
$class, $this); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get module directory |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function directory() |
110
|
|
|
{ |
111
|
|
|
return $this->normalizePath($this->config->directory()) . |
112
|
|
|
DIRECTORY_SEPARATOR . $this->name(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get module migrations path |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function migrationsPath() |
121
|
|
|
{ |
122
|
|
|
return $this->directory() . DIRECTORY_SEPARATOR . |
123
|
|
|
$this->normalizePath($this->config->migrationsPath()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Verify whether module has service provider |
128
|
|
|
* |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
|
|
public function hasServiceProvider() |
132
|
|
|
{ |
133
|
|
|
return $this->hasFile('provider', 'serviceProviderFilePath'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Verifies whether module has factory |
138
|
|
|
* |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
public function hasFactory() |
142
|
|
|
{ |
143
|
|
|
return $this->hasFile('factory', 'factoryFilePath'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Verifies whether module has routes file |
148
|
|
|
* |
149
|
|
|
* @param array $data |
150
|
|
|
* |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
|
|
public function hasRoutes(array $data = []) |
154
|
|
|
{ |
155
|
|
|
$prefix = $this->routePrefix($data); |
156
|
|
|
|
157
|
|
|
return $this->hasFile('routes', 'routesFilePath', $prefix); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Verifies whether module has seeder file |
162
|
|
|
* |
163
|
|
|
* @return bool |
164
|
|
|
*/ |
165
|
|
|
public function hasSeeder() |
166
|
|
|
{ |
167
|
|
|
return $this->hasFile('seeder', 'seederFilePath'); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Verifies whether module has file of given type either checking config |
172
|
|
|
* and if it's not exist by checking whether file exists |
173
|
|
|
* |
174
|
|
|
* @param string $option |
175
|
|
|
* @param string $pathFunction |
176
|
|
|
* @param string $prefix |
177
|
|
|
* |
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
|
|
protected function hasFile($option, $pathFunction, $prefix = '') |
181
|
|
|
{ |
182
|
|
|
return (bool)($this->options->has($prefix . $option) ? |
|
|
|
|
183
|
|
|
$this->options->get($prefix . $option) : |
|
|
|
|
184
|
|
|
$this->laravel['files']->exists($this->$pathFunction($prefix))); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get controller namespace for routing |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
public function routingControllerNamespace() |
193
|
|
|
{ |
194
|
|
|
return $this->config->modulesNamespace() . '\\' . $this->name() . '\\' . |
195
|
|
|
$this->config->routingControllerNamespace(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Get module routes file (with path) |
200
|
|
|
* |
201
|
|
|
* @param string $prefix |
202
|
|
|
* |
203
|
|
|
* @return string |
204
|
|
|
*/ |
205
|
|
|
public function routesFilePath($prefix) |
206
|
|
|
{ |
207
|
|
|
return $this->getPath('routingFile', $prefix); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Get route prefix |
212
|
|
|
* |
213
|
|
|
* @param array $data |
214
|
|
|
* |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
|
|
public function routePrefix(array $data) |
218
|
|
|
{ |
219
|
|
|
return $data ? $data['type'] . '_' : ''; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Get module factory file path |
224
|
|
|
* |
225
|
|
|
* @return string |
226
|
|
|
*/ |
227
|
|
|
public function factoryFilePath() |
228
|
|
|
{ |
229
|
|
|
return $this->getPath('factoryFile'); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Get module factory file path |
234
|
|
|
* |
235
|
|
|
* @return string |
236
|
|
|
*/ |
237
|
|
|
public function seederFilePath() |
238
|
|
|
{ |
239
|
|
|
return $this->getPath('seederFile'); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Get module service provider file path |
244
|
|
|
* |
245
|
|
|
* @return string |
246
|
|
|
*/ |
247
|
|
|
public function serviceProviderFilePath() |
248
|
|
|
{ |
249
|
|
|
return $this->getPath('serviceProviderFile'); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Get path |
254
|
|
|
* |
255
|
|
|
* @param string $configMethod |
256
|
|
|
* @param string $prefix |
257
|
|
|
* |
258
|
|
|
* @return string |
259
|
|
|
*/ |
260
|
|
|
protected function getPath($configMethod, $prefix = '') |
261
|
|
|
{ |
262
|
|
|
return $this->directory() . DIRECTORY_SEPARATOR . |
263
|
|
|
$this->replace($this->config->$configMethod($prefix), $this); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Verifies whether given module is active |
268
|
|
|
* |
269
|
|
|
* @return bool |
270
|
|
|
*/ |
271
|
|
|
public function active() |
272
|
|
|
{ |
273
|
|
|
return $this->options->get('active', true); |
|
|
|
|
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
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..