1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace neilherbertuk\modules; |
4
|
|
|
|
5
|
|
|
use Illuminate\Foundation\Application as LaravelApplication; |
6
|
|
|
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; |
7
|
|
|
use neilherbertuk\modules\Console\MakeModuleCommand; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class ModuleServiceProvider |
11
|
|
|
* @package neilherbertuk\modules |
12
|
|
|
*/ |
13
|
|
|
class ModuleServiceProvider extends ServiceProvider |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
public function register() |
19
|
|
|
{ |
20
|
|
|
// Merge Configuration |
21
|
|
|
$this->mergeConfigFrom( |
22
|
|
|
__DIR__ . '/Config/modules.php', |
23
|
|
|
'modules' |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* |
29
|
|
|
*/ |
30
|
|
|
public function boot() |
31
|
|
|
{ |
32
|
|
|
$this->bootInConsole(); |
33
|
|
|
|
34
|
|
|
$this->bindClosuresToIOC(); |
35
|
|
|
|
36
|
|
|
$this->loadModules(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* |
41
|
|
|
*/ |
42
|
|
|
protected function bootInConsole() |
43
|
|
|
{ |
44
|
|
|
if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) { |
45
|
|
|
|
46
|
|
|
// Publish configuration file |
47
|
|
|
$this->publishes([ |
48
|
|
|
__DIR__ . '/Config/modules.php' => config_path('modules.php'), |
49
|
|
|
], "config"); |
50
|
|
|
|
51
|
|
|
// Create app/Modules directory |
52
|
|
|
if (!file_exists(base_path() . "/app/Modules/")) { |
53
|
|
|
mkdir(base_path() . "/app/Modules/", 0755, true); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->registerCommands(); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Register the commands. |
62
|
|
|
* |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
|
|
protected function registerCommands() |
66
|
|
|
{ |
67
|
|
|
$this->registerModuleMakeCommand(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Register the command. |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
protected function registerModuleMakeCommand() |
76
|
|
|
{ |
77
|
|
|
$this->commands([ |
78
|
|
|
MakeModuleCommand::class |
79
|
|
|
]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* |
84
|
|
|
*/ |
85
|
|
|
protected function bindClosuresToIOC() |
86
|
|
|
{ |
87
|
|
|
// Bind a closure to the IOC container which gets called from module's routes files and returns the module name |
|
|
|
|
88
|
|
|
$this->bindGetModuleNameClosureToIOC(); |
89
|
|
|
|
90
|
|
|
// Bind a closure to the IOC container which gets called from module's routes files and returns the module controller path |
|
|
|
|
91
|
|
|
$this->bindGetControllerPathClosureToIOC(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return \Illuminate\Support\Collection |
96
|
|
|
*/ |
97
|
|
|
protected function getEnabledModules() |
98
|
|
|
{ |
99
|
|
|
// Get what modules to load from config or directory |
100
|
|
|
if (config("modules.autoload")) { |
101
|
|
|
return $this->getDirectories(base_path() . "/app/Modules/"); |
102
|
|
|
} |
103
|
|
|
return collect(config("modules.enabled")); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param $directory |
108
|
|
|
* @return \Illuminate\Support\Collection |
109
|
|
|
*/ |
110
|
|
|
protected function getDirectories($directory) |
111
|
|
|
{ |
112
|
|
|
$disabledModules = collect(config('modules.disabled')); |
113
|
|
|
$directories = collect(scandir($directory)) |
114
|
|
|
->reject(function($folder) use ($directory, $disabledModules) { |
|
|
|
|
115
|
|
|
return !is_dir($directory . DIRECTORY_SEPARATOR . $folder) |
116
|
|
|
|| $folder == "." |
117
|
|
|
|| $folder == ".." |
118
|
|
|
|| $this->isModuleDisabled($folder, $disabledModules); |
119
|
|
|
}); |
120
|
|
|
|
121
|
|
|
return $directories; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param $moduleToLoad |
126
|
|
|
* @param $disabledModulesList |
127
|
|
|
* @return mixed |
128
|
|
|
*/ |
129
|
|
|
protected function isModuleDisabled($moduleToLoad, $disabledModulesList) |
130
|
|
|
{ |
131
|
|
|
return $disabledModulesList->contains($moduleToLoad); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* |
136
|
|
|
*/ |
137
|
|
|
protected function loadModules() |
138
|
|
|
{ |
139
|
|
|
$modules = $this->getEnabledModules(); |
140
|
|
|
|
141
|
|
|
// Load each module |
142
|
|
|
$modules->each(function($module) { |
|
|
|
|
143
|
|
|
|
144
|
|
|
$this->loadModuleProviders($module); |
145
|
|
|
|
146
|
|
|
$this->loadModuleRoutes($module); |
147
|
|
|
|
148
|
|
|
$this->loadModuleViews($module); |
149
|
|
|
|
150
|
|
|
$this->loadModuleTranslations($module); |
151
|
|
|
|
152
|
|
|
if ($this->app->runningInConsole()) { |
153
|
|
|
$this->loadModuleMigrations($module); |
154
|
|
|
$this->loadModuleCommands($module); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
}); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param $module |
162
|
|
|
*/ |
163
|
|
|
protected function loadModuleRoutes($module) |
164
|
|
|
{ |
165
|
|
|
$this->loadModuleWebRoutes($module); |
166
|
|
|
|
167
|
|
|
$this->loadModuleAPIRoutes($module); |
168
|
|
|
|
169
|
|
|
} |
|
|
|
|
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param $module |
173
|
|
|
*/ |
174
|
|
|
protected function loadModuleWebRoutes($module) |
175
|
|
|
{ |
176
|
|
|
if (file_exists(base_path() .'/app/Modules/' . $module . '/web.php')) { |
177
|
|
|
$this->loadRoutesFrom(base_path('app/Modules/' . $module . '/web.php')); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param $module |
183
|
|
|
*/ |
184
|
|
|
protected function loadModuleAPIRoutes($module) |
185
|
|
|
{ |
186
|
|
|
if (file_exists(base_path() .'/app/Modules/' . $module . '/api.php')) { |
187
|
|
|
$this->loadRoutesFrom(base_path('app/Modules/' . $module . '/api.php')); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param $module |
193
|
|
|
*/ |
194
|
|
|
protected function loadModuleViews($module) |
195
|
|
|
{ |
196
|
|
|
if (is_dir(base_path('app/Modules/' . $module . '/Views'))) { |
197
|
|
|
$this->loadViewsFrom(base_path('app/Modules/' . $module . '/Views'), strtolower($module)); |
|
|
|
|
198
|
|
|
} |
199
|
|
|
if (is_dir(base_path('app/Modules/' . $module . '/Resources/Views'))) { |
200
|
|
|
$this->loadViewsFrom(base_path('app/Modules/' . $module . '/Resources/Views'), strtolower($module)); |
|
|
|
|
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param $module |
206
|
|
|
*/ |
207
|
|
|
protected function loadModuleMigrations($module) |
208
|
|
|
{ |
209
|
|
|
if (is_dir(base_path('app/Modules/' . $module . '/Database/Migrations'))) { |
210
|
|
|
/** @scrutinizer ignore-call */ |
211
|
|
|
$this->loadMigrationsFrom(base_path('app/Modules/' . $module . '/Database/Migrations'), $module); |
|
|
|
|
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @param $module |
217
|
|
|
*/ |
218
|
|
|
protected function loadModuleProviders($module) |
219
|
|
|
{ |
220
|
|
|
if (is_dir(base_path('app/Modules/' . $module . '/Providers'))) { |
221
|
|
|
$serviceProviderStartPos = strlen(base_path('app/Modules/' . $module . '/Providers/')); |
222
|
|
|
$files = glob(base_path('app/Modules/' . $module . '/Providers/*.php')); |
223
|
|
|
foreach ($files as $file) { |
224
|
|
|
$this->app->register($this->app->getNamespace() . "Modules\\$module\Providers\\" . substr($file, $serviceProviderStartPos, -4)); |
|
|
|
|
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param $module |
231
|
|
|
*/ |
232
|
|
|
protected function loadModuleCommands($module) |
233
|
|
|
{ |
234
|
|
|
if (is_dir(base_path('app/Modules/' . $module . '/Console/Commands'))) { |
235
|
|
|
$startPos = strlen(base_path('app/Modules/' . $module . '/Console/Commands/')); |
236
|
|
|
$files = glob(base_path('app/Modules/' . $module . '/Console/Commands/*.php')); |
237
|
|
|
foreach ($files as $file) { |
238
|
|
|
$this->commands($this->app->getNamespace() . "Modules\\$module\Console\Commands\\" . substr($file, $startPos, -4)); |
|
|
|
|
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @param $module |
245
|
|
|
*/ |
246
|
|
|
protected function loadModuleTranslations($module) |
247
|
|
|
{ |
248
|
|
|
if (is_dir(base_path('app/Modules/' . $module . '/Lang'))) { |
249
|
|
|
$this->loadTranslationsFrom(base_path('app/Modules/' . $module . '/Lang'), $module); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* |
255
|
|
|
*/ |
256
|
|
|
protected function bindGetModuleNameClosureToIOC() |
257
|
|
|
{ |
258
|
|
|
$this->app->bind('Module::getNameLowerCase', function($app, $parameters) { |
|
|
|
|
259
|
|
|
return strtolower(substr($parameters['path'], strrpos($parameters['path'], "/") + 1)); |
260
|
|
|
}); |
261
|
|
|
|
262
|
|
|
$this->app->bind('Module::getName', function($app, $parameters) { |
|
|
|
|
263
|
|
|
return substr($parameters['path'], strrpos($parameters['path'], "/") + 1); |
264
|
|
|
}); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* |
269
|
|
|
*/ |
270
|
|
|
protected function bindGetControllerPathClosureToIOC() |
271
|
|
|
{ |
272
|
|
|
$this->app->bind('Module::getControllerPath', function($app, $parameters) { |
|
|
|
|
273
|
|
|
return $this->app->getNamespace() . 'Modules\\' . substr($parameters['path'], strrpos($parameters['path'], "/") + 1) . '\Controllers'; |
|
|
|
|
274
|
|
|
}); |
275
|
|
|
} |
276
|
|
|
} |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.