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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param $module |
203
|
|
|
*/ |
204
|
|
|
protected function loadModuleMigrations($module) |
205
|
|
|
{ |
206
|
|
View Code Duplication |
if (is_dir(base_path('app/Modules/' . $module . '/Database/Migrations'))) { |
|
|
|
|
207
|
|
|
/** @scrutinizer ignore-call */ |
208
|
|
|
$this->loadMigrationsFrom(base_path('app/Modules/' . $module . '/Database/Migrations'), $module); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param $module |
214
|
|
|
*/ |
215
|
|
View Code Duplication |
protected function loadModuleProviders($module) |
|
|
|
|
216
|
|
|
{ |
217
|
|
|
if (is_dir(base_path('app/Modules/' . $module . '/Providers'))) { |
218
|
|
|
$serviceProviderStartPos = strlen(base_path('app/Modules/' . $module . '/Providers/')); |
219
|
|
|
$files = glob(base_path('app/Modules/' . $module . '/Providers/*.php')); |
220
|
|
|
foreach ($files as $file) { |
221
|
|
|
$this->app->register($this->app->getNamespace() . "Modules\\$module\Providers\\" . substr($file, $serviceProviderStartPos, -4)); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param $module |
228
|
|
|
*/ |
229
|
|
View Code Duplication |
protected function loadModuleCommands($module) |
|
|
|
|
230
|
|
|
{ |
231
|
|
|
if (is_dir(base_path('app/Modules/' . $module . '/Console/Commands'))) { |
232
|
|
|
$startPos = strlen(base_path('app/Modules/' . $module . '/Console/Commands/')); |
233
|
|
|
$files = glob(base_path('app/Modules/' . $module . '/Console/Commands/*.php')); |
234
|
|
|
foreach ($files as $file) { |
235
|
|
|
$this->commands($this->app->getNamespace() . "Modules\\$module\Console\Commands\\" . substr($file, $startPos, -4)); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param $module |
242
|
|
|
*/ |
243
|
|
|
protected function loadModuleTranslations($module) |
244
|
|
|
{ |
245
|
|
View Code Duplication |
if (is_dir(base_path('app/Modules/' . $module . '/Lang'))) { |
|
|
|
|
246
|
|
|
$this->loadTranslationsFrom(base_path('app/Modules/' . $module . '/Lang'), $module); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* |
252
|
|
|
*/ |
253
|
|
|
protected function bindGetModuleNameClosureToIOC() |
254
|
|
|
{ |
255
|
|
|
$this->app->bind('Module::getNameLowerCase', function($app, $parameters) { |
256
|
|
|
return strtolower(substr($parameters['path'], strrpos($parameters['path'], "/") + 1)); |
257
|
|
|
}); |
258
|
|
|
|
259
|
|
|
$this->app->bind('Module::getName', function($app, $parameters) { |
260
|
|
|
return substr($parameters['path'], strrpos($parameters['path'], "/") + 1); |
261
|
|
|
}); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* |
266
|
|
|
*/ |
267
|
|
|
protected function bindGetControllerPathClosureToIOC() |
268
|
|
|
{ |
269
|
|
|
$this->app->bind('Module::getControllerPath', function($app, $parameters) { |
270
|
|
|
return $this->app->getNamespace() . 'Modules\\' . substr($parameters['path'], strrpos($parameters['path'], "/") + 1) . '\Controllers'; |
271
|
|
|
}); |
272
|
|
|
} |
273
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.