1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mnabialek\LaravelModular\Services; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Foundation\Application; |
6
|
|
|
use Illuminate\Contracts\Routing\Registrar; |
7
|
|
|
use Illuminate\Database\Seeder; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use Mnabialek\LaravelModular\Models\Module; |
10
|
|
|
use Mnabialek\LaravelModular\Traits\Normalizer; |
11
|
|
|
use Mnabialek\LaravelModular\Traits\Replacer; |
12
|
|
|
|
13
|
|
|
class Modular |
14
|
|
|
{ |
15
|
|
|
use Replacer; |
16
|
|
|
use Normalizer; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Application |
20
|
|
|
*/ |
21
|
|
|
protected $app; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Collection|null |
25
|
|
|
*/ |
26
|
|
|
protected $modules = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Config |
30
|
|
|
*/ |
31
|
|
|
protected $config; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Modular constructor. |
35
|
|
|
* |
36
|
|
|
* @param Application $app |
37
|
|
|
* @param Config $config |
38
|
|
|
*/ |
39
|
|
|
public function __construct(Application $app, Config $config) |
40
|
|
|
{ |
41
|
|
|
$this->app = $app; |
42
|
|
|
$this->config = $config; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Runs main seeders for all active modules |
47
|
|
|
* |
48
|
|
|
* @param Seeder $seeder |
49
|
|
|
*/ |
50
|
|
|
public function seed(Seeder $seeder) |
51
|
|
|
{ |
52
|
|
|
$this->withSeeders()->each(function ($module) use ($seeder) { |
53
|
|
|
/* @var Module $module */ |
54
|
|
|
$seeder->call($module->seederClass()); |
55
|
|
|
}); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Load routes for active modules |
60
|
|
|
* |
61
|
|
|
* @param Registrar $router |
62
|
|
|
* @param string|null $type |
63
|
|
|
*/ |
64
|
|
|
public function loadRoutes(Registrar $router, $type = null) |
65
|
|
|
{ |
66
|
|
|
$this->withRoutes($type)->each(function ($module) use ($router, $type) { |
67
|
|
|
/* @var Module $module */ |
68
|
|
|
$router->group(['namespace' => $module->routingControllerNamespace()], |
69
|
|
|
function ($router) use ($module, $type) { |
|
|
|
|
70
|
|
|
$this->app['files']->getRequire($this->app->basePath() . |
71
|
|
|
DIRECTORY_SEPARATOR . |
72
|
|
|
$module->routesFilePath($module->routePrefix(compact('type')))); |
73
|
|
|
}); |
74
|
|
|
}); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Load factories for active modules |
79
|
|
|
* |
80
|
|
|
* @param $factory |
81
|
|
|
*/ |
82
|
|
|
public function loadFactories($factory) |
83
|
|
|
{ |
84
|
|
|
$this->withFactories()->each(function ($module) use ($factory) { |
85
|
|
|
/* @var Module $module */ |
86
|
|
|
$this->loadFactoryFile($module->factoryFilePath(), $factory); |
87
|
|
|
}); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Load file |
92
|
|
|
* |
93
|
|
|
* @param string $file |
94
|
|
|
* @param $factory |
95
|
|
|
*/ |
96
|
|
|
protected function loadFactoryFile($file, $factory) |
97
|
|
|
{ |
98
|
|
|
require $file; // @codeCoverageIgnore |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Load service providers for active modules |
103
|
|
|
*/ |
104
|
|
|
public function loadServiceProviders() |
105
|
|
|
{ |
106
|
|
|
$this->withServiceProviders()->each(function ($module) { |
107
|
|
|
/* @var Module $module */ |
108
|
|
|
$this->app->register($module->serviceProviderClass()); |
109
|
|
|
}); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get all routable modules (active and having routes file) |
114
|
|
|
* |
115
|
|
|
* @param string $type |
116
|
|
|
* |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
public function withRoutes($type) |
120
|
|
|
{ |
121
|
|
|
return $this->filterActiveByMethod('hasRoutes', compact('type')); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get all routable modules (active and having routes file) |
126
|
|
|
* |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
public function withFactories() |
130
|
|
|
{ |
131
|
|
|
return $this->filterActiveByMethod('hasFactory'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get all modules that have service providers (active and having service |
136
|
|
|
* provider file) |
137
|
|
|
* |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
public function withServiceProviders() |
141
|
|
|
{ |
142
|
|
|
return $this->filterActiveByMethod('hasServiceProvider'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get all modules that have seeders (active and having seeder file) |
147
|
|
|
* |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
|
|
public function withSeeders() |
151
|
|
|
{ |
152
|
|
|
return $this->filterActiveByMethod('hasSeeder'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Get active modules that also pass given requirement |
157
|
|
|
* |
158
|
|
|
* @param string $requirement |
159
|
|
|
* |
160
|
|
|
* @param array $data |
161
|
|
|
* |
162
|
|
|
* @return Collection |
163
|
|
|
*/ |
164
|
|
|
protected function filterActiveByMethod($requirement, array $data = []) |
165
|
|
|
{ |
166
|
|
|
return $this->modules() |
167
|
|
|
->filter(function ($module) use ($requirement, $data) { |
168
|
|
|
return $module->active() && $module->$requirement($data); |
169
|
|
|
})->values(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get all modules |
174
|
|
|
* |
175
|
|
|
* @return Collection |
176
|
|
|
*/ |
177
|
|
|
public function all() |
178
|
|
|
{ |
179
|
|
|
return $this->modules(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get active modules |
184
|
|
|
* |
185
|
|
|
* @return Collection |
186
|
|
|
*/ |
187
|
|
|
public function active() |
188
|
|
|
{ |
189
|
|
|
return $this->modules()->filter(function ($module) { |
190
|
|
|
return $module->active(); |
191
|
|
|
})->values(); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Load modules (if not loaded) and get modules |
196
|
|
|
* |
197
|
|
|
* @return Collection |
198
|
|
|
*/ |
199
|
|
|
protected function modules() |
200
|
|
|
{ |
201
|
|
|
if ($this->modules === null) { |
202
|
|
|
$this->loadModules(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return $this->modules; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Load modules from config |
210
|
|
|
*/ |
211
|
|
|
protected function loadModules() |
212
|
|
|
{ |
213
|
|
|
$this->modules = collect(); |
214
|
|
|
|
215
|
|
|
collect($this->config->modules())->each(function ($options, $name) { |
216
|
|
|
$this->modules->push(new Module($name, $this->app, $options)); |
217
|
|
|
}); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Find given module by name |
222
|
|
|
* |
223
|
|
|
* @param string $name |
224
|
|
|
* |
225
|
|
|
* @return Module |
226
|
|
|
*/ |
227
|
|
|
public function find($name) |
228
|
|
|
{ |
229
|
|
|
return $this->modules()->first(function ($module) use ($name) { |
230
|
|
|
/* @var Module $module */ |
231
|
|
|
return $module->name() == $name; |
232
|
|
|
}); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Verify whether module with given name already exists |
237
|
|
|
* |
238
|
|
|
* @param $name |
239
|
|
|
* |
240
|
|
|
* @return bool |
241
|
|
|
*/ |
242
|
|
|
public function exists($name) |
243
|
|
|
{ |
244
|
|
|
return $this->find($name) !== null; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.