|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Contains the BaseServiceProvider class. |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2016 Attila Fulop |
|
6
|
|
|
* @author Attila Fulop |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
* @since 2016-12-29 |
|
9
|
|
|
* |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Konekt\Concord; |
|
13
|
|
|
|
|
14
|
|
|
use Illuminate\Support\ServiceProvider; |
|
15
|
|
|
use Konekt\Concord\Contracts\Concord as ConcordContract; |
|
16
|
|
|
use Konekt\Concord\Contracts\Convention; |
|
17
|
|
|
use Konekt\Concord\Contracts\Module; |
|
18
|
|
|
use Konekt\Concord\Module\Manifest; |
|
19
|
|
|
use Konekt\Concord\Module\Kind; |
|
20
|
|
|
use Konekt\Concord\Routing\RouteRegistrar; |
|
21
|
|
|
use ReflectionClass; |
|
22
|
|
|
|
|
23
|
|
|
abstract class BaseServiceProvider extends ServiceProvider implements Module |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var string */ |
|
26
|
|
|
protected $basePath; |
|
27
|
|
|
|
|
28
|
|
|
/** @var Manifest */ |
|
29
|
|
|
protected $manifest; |
|
30
|
|
|
|
|
31
|
|
|
/** @var string */ |
|
32
|
|
|
protected $namespaceRoot; |
|
33
|
|
|
|
|
34
|
|
|
/** @var string */ |
|
35
|
|
|
protected $id; |
|
36
|
|
|
|
|
37
|
|
|
/** @var array */ |
|
38
|
|
|
protected $models = []; |
|
39
|
|
|
|
|
40
|
|
|
/** @var array */ |
|
41
|
|
|
protected $enums = []; |
|
42
|
|
|
|
|
43
|
|
|
/** @var array */ |
|
44
|
|
|
protected $requests = []; |
|
45
|
|
|
|
|
46
|
|
|
/** @var ConcordContract */ |
|
47
|
|
|
protected $concord; |
|
48
|
|
|
|
|
49
|
|
|
/** @var Convention */ |
|
50
|
|
|
protected $convention; |
|
51
|
|
|
|
|
52
|
|
|
/** @var Kind */ |
|
53
|
|
|
protected $kind; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* ModuleServiceProvider class constructor |
|
57
|
|
|
* |
|
58
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct($app) |
|
61
|
|
|
{ |
|
62
|
|
|
parent::__construct($app); |
|
63
|
|
|
|
|
64
|
|
|
$this->concord = $app->make(ConcordContract::class); // retrieve the concord singleton |
|
65
|
|
|
$this->convention = $this->concord->getConvention(); // storing to get rid of train wrecks |
|
66
|
|
|
$this->kind = Kind::create(static::$_kind); |
|
67
|
|
|
$this->basePath = dirname(dirname((new ReflectionClass(static::class))->getFileName())); |
|
68
|
|
|
$this->namespaceRoot = str_replace( |
|
69
|
|
|
sprintf('\\%s\\ModuleServiceProvider', |
|
70
|
|
|
str_replace('/', '\\', $this->convention->providersFolder()) |
|
71
|
|
|
), |
|
72
|
|
|
'', static::class |
|
73
|
|
|
); |
|
74
|
|
|
$this->id = $this->getModuleId(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function register() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->loadConfiguration(); |
|
80
|
|
|
|
|
81
|
|
|
if (true === $this->config('event_listeners')) { |
|
82
|
|
|
$this->registerEventServiceProvider(); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @inheritdoc |
|
88
|
|
|
*/ |
|
89
|
|
|
public function boot() |
|
90
|
|
|
{ |
|
91
|
|
|
if ($this->areMigrationsEnabled()) { |
|
92
|
|
|
$this->registerMigrations(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if ($this->areModelsEnabled()) { |
|
96
|
|
|
$this->registerModels(); |
|
97
|
|
|
$this->registerEnums(); |
|
98
|
|
|
$this->registerRequestTypes(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if ($this->areViewsEnabled()) { |
|
102
|
|
|
$this->registerViews(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
if ($routes = $this->config('routes', true)) { |
|
106
|
|
|
$routeRegistrar = new RouteRegistrar($this, $this->convention); |
|
107
|
|
|
if (true === $routes) { |
|
108
|
|
|
$routeRegistrar->registerAllRoutes(); |
|
109
|
|
|
} elseif (isset($routes['files'])) { |
|
110
|
|
|
$routeRegistrar->registerRoutes($this->config('routes.files'), $this->config('routes')); |
|
111
|
|
|
} elseif (isset($routes[0]) && is_array($routes[0])) { |
|
112
|
|
|
foreach ($routes as $route) { |
|
113
|
|
|
$routeRegistrar->registerRoutes($route['files'], $route); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function areMigrationsEnabled(): bool |
|
120
|
|
|
{ |
|
121
|
|
|
return (bool) $this->config('migrations', true); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
public function areModelsEnabled(): bool |
|
126
|
|
|
{ |
|
127
|
|
|
return (bool) $this->config('models', true); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function areViewsEnabled(): bool |
|
131
|
|
|
{ |
|
132
|
|
|
return (bool) $this->config('views', true); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
public function areRoutesEnabled(): bool |
|
137
|
|
|
{ |
|
138
|
|
|
return (bool) $this->config('routes', true); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @inheritDoc |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getId(): string |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->id; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Returns module configuration value(s) |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $key If left empty, the entire module configuration gets retrieved |
|
153
|
|
|
* @param mixed $default |
|
154
|
|
|
* |
|
155
|
|
|
* @return mixed |
|
156
|
|
|
*/ |
|
157
|
|
|
public function config(string $key = null, $default = null) |
|
158
|
|
|
{ |
|
159
|
|
|
$key = $key ? sprintf('%s.%s', $this->getId(), $key) : $this->getId(); |
|
160
|
|
|
|
|
161
|
|
|
return config($key, $default); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return Manifest |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getManifest(): Manifest |
|
168
|
|
|
{ |
|
169
|
|
|
if (!$this->manifest) { |
|
170
|
|
|
$data = include($this->basePath . '/' . $this->convention->manifestFile()); |
|
171
|
|
|
|
|
172
|
|
|
$name = $data['name'] ?? 'N/A'; |
|
173
|
|
|
$version = $data['version'] ?? 'n/a'; |
|
174
|
|
|
|
|
175
|
|
|
$this->manifest = new Manifest($name, $version); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return $this->manifest; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Returns the root folder on the filesystem containing the module |
|
183
|
|
|
* |
|
184
|
|
|
* @return string |
|
185
|
|
|
*/ |
|
186
|
|
|
public function getBasePath(): string |
|
187
|
|
|
{ |
|
188
|
|
|
return $this->basePath; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @inheritdoc |
|
193
|
|
|
*/ |
|
194
|
|
|
public function getKind(): Kind |
|
195
|
|
|
{ |
|
196
|
|
|
return $this->kind; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Returns the folder where the module/box configuration files are |
|
201
|
|
|
* |
|
202
|
|
|
* @return string |
|
203
|
|
|
*/ |
|
204
|
|
|
public function getConfigPath(): string |
|
205
|
|
|
{ |
|
206
|
|
|
return $this->getBasePath() . '/' . $this->convention->configFolder(); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Returns the module's root (topmost) namespace |
|
211
|
|
|
* |
|
212
|
|
|
* @return string |
|
213
|
|
|
*/ |
|
214
|
|
|
public function getNamespaceRoot(): string |
|
215
|
|
|
{ |
|
216
|
|
|
return $this->namespaceRoot; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Returns the short (abbreviated) name of the module |
|
221
|
|
|
* E.g. Konekt\AppShell => app_shell |
|
222
|
|
|
*/ |
|
223
|
|
|
public function shortName() |
|
224
|
|
|
{ |
|
225
|
|
|
$id = $this->getModuleId(); |
|
226
|
|
|
$p = strrpos($id, '.'); |
|
227
|
|
|
|
|
228
|
|
|
return $p ? substr($id, $p + 1) : $id; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Returns a standard module name based on the module provider's classname |
|
233
|
|
|
* |
|
234
|
|
|
* Eg.: '\Vendor\Module\Services\ModuleServiceProvider' -> 'vendor.module' |
|
235
|
|
|
* |
|
236
|
|
|
* @param string $classname |
|
237
|
|
|
* |
|
238
|
|
|
* @see concord_module_id |
|
239
|
|
|
* |
|
240
|
|
|
* @return string |
|
241
|
|
|
*/ |
|
242
|
|
|
protected function getModuleId($classname = null) |
|
243
|
|
|
{ |
|
244
|
|
|
return concord_module_id($classname ?: static::class); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Register the module's migrations |
|
249
|
|
|
*/ |
|
250
|
|
|
protected function registerMigrations() |
|
251
|
|
|
{ |
|
252
|
|
|
$path = $this->getBasePath() . '/' . $this->convention->migrationsFolder(); |
|
253
|
|
|
|
|
254
|
|
|
if ($this->app->runningInConsole() && is_dir($path)) { |
|
255
|
|
|
$this->loadMigrationsFrom($path); |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Register models in a box/module |
|
261
|
|
|
*/ |
|
262
|
|
|
protected function registerModels() |
|
263
|
|
|
{ |
|
264
|
|
|
foreach ($this->models as $key => $model) { |
|
265
|
|
|
$contract = is_string($key) ? $key : $this->convention->contractForModel($model); |
|
266
|
|
|
$this->concord->registerModel($contract, $model); |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Register enums in a box/module |
|
272
|
|
|
*/ |
|
273
|
|
|
protected function registerEnums() |
|
274
|
|
|
{ |
|
275
|
|
|
foreach ($this->enums as $key => $enum) { |
|
276
|
|
|
$contract = is_string($key) ? $key : $this->convention->contractForEnum($enum); |
|
277
|
|
|
$this->concord->registerEnum($contract, $enum); |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* Register request types in a box/module |
|
283
|
|
|
*/ |
|
284
|
|
|
protected function registerRequestTypes() |
|
285
|
|
|
{ |
|
286
|
|
|
foreach ($this->requests as $key => $requestType) { |
|
287
|
|
|
$contract = is_string($key) ? $key : $this->convention->contractForRequest($requestType); |
|
288
|
|
|
$this->concord->registerRequest($contract, $requestType); |
|
289
|
|
|
} |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* Register the views folder, in a separate namespace |
|
294
|
|
|
*/ |
|
295
|
|
|
protected function registerViews() |
|
296
|
|
|
{ |
|
297
|
|
|
$path = $this->getBasePath() . '/' . $this->convention->viewsFolder(); |
|
298
|
|
|
$namespace = $this->config('views.namespace', $this->shortName()); |
|
299
|
|
|
|
|
300
|
|
|
if (is_dir($path)) { |
|
301
|
|
|
$this->loadViewsFrom($path, $namespace); |
|
302
|
|
|
} |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* Registers the event service provider of the module/config (ie. event-listener bindings) |
|
307
|
|
|
*/ |
|
308
|
|
|
protected function registerEventServiceProvider() |
|
309
|
|
|
{ |
|
310
|
|
|
$eventServiceProviderClass = sprintf('%s\\%s\\EventServiceProvider', |
|
311
|
|
|
$this->namespaceRoot, |
|
312
|
|
|
str_replace('/', '\\', $this->convention->providersFolder()) |
|
313
|
|
|
); |
|
314
|
|
|
|
|
315
|
|
|
if (class_exists($eventServiceProviderClass)) { |
|
316
|
|
|
$this->app->register($eventServiceProviderClass); |
|
317
|
|
|
} |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
protected function loadConfiguration() |
|
321
|
|
|
{ |
|
322
|
|
|
$cfgFile = sprintf('%s/%s', $this->getConfigPath(), $this->configFileName); |
|
323
|
|
|
|
|
324
|
|
|
if (file_exists($cfgFile)) { |
|
325
|
|
|
$this->mergeConfigFrom($cfgFile, $this->getId()); |
|
326
|
|
|
} |
|
327
|
|
|
} |
|
328
|
|
|
} |
|
329
|
|
|
|