1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Foundation\Providers; |
4
|
|
|
|
5
|
|
|
use Foundation\Console\SeedCommand; |
6
|
|
|
use Foundation\Services\BootstrapRegistrarService; |
7
|
|
|
use Illuminate\Database\Eloquent\Factory; |
8
|
|
|
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; |
9
|
|
|
use Illuminate\Routing\Router; |
10
|
|
|
use Route; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class BootstrapServiceProvider. |
14
|
|
|
*/ |
15
|
|
|
class BootstrapServiceProvider extends ServiceProvider |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var BootstrapRegistrarService |
19
|
|
|
*/ |
20
|
|
|
protected $bootstrapService; |
21
|
|
|
|
22
|
4 |
|
public function register() |
23
|
|
|
{ |
24
|
4 |
|
$this->loadBootstrapService(); |
25
|
4 |
|
$this->loadCommands(); |
26
|
4 |
|
$this->loadPolicies(); |
27
|
4 |
|
$this->loadV1Routes(); |
28
|
4 |
|
$this->loadConfigs(); |
29
|
4 |
|
$this->loadFactories(); |
30
|
4 |
|
$this->loadMigrations(); |
31
|
|
|
|
32
|
4 |
|
$this->overrideSeedCommand(); |
33
|
4 |
|
} |
34
|
|
|
|
35
|
4 |
|
public function loadBootstrapService() |
36
|
|
|
{ |
37
|
4 |
|
$this->bootstrapService = new BootstrapRegistrarService(); |
38
|
|
|
|
39
|
4 |
|
if (!$this->app->environment('production')) { |
40
|
4 |
|
$this->bootstrapService->cache(); |
41
|
|
|
} |
42
|
4 |
|
} |
43
|
|
|
|
44
|
4 |
|
private function loadCommands() |
45
|
|
|
{ |
46
|
4 |
|
$this->commands($this->bootstrapService->getCommands()); |
47
|
4 |
|
} |
48
|
|
|
|
49
|
4 |
|
private function loadV1Routes() |
50
|
|
|
{ |
51
|
4 |
|
foreach ($this->bootstrapService->getRoutes() as $route) { |
52
|
4 |
|
$apiDomain = strtolower(env('API_URL')); |
53
|
4 |
|
$apiDomain = str_replace('http://', '', $apiDomain); |
54
|
4 |
|
$apiDomain = str_replace('https://', '', $apiDomain); |
55
|
4 |
|
$moduleNamespace = $route[1]; |
56
|
4 |
|
$moduleName = explode('\\', $moduleNamespace)[1]; |
57
|
4 |
|
$controllerNamespace = $moduleNamespace . '\\' . 'Http\\Controllers'; |
58
|
4 |
|
$modelNameSpace = $moduleNamespace . '\\' . 'Entities\\' . $moduleName; |
59
|
4 |
|
$filepath = $route[0]; |
60
|
4 |
|
Route::group([ |
61
|
4 |
|
'prefix' => 'v1', |
62
|
4 |
|
'namespace' => $controllerNamespace, |
63
|
4 |
|
'domain' => $apiDomain, |
64
|
|
|
'middleware' => ['api'], |
65
|
|
|
], function (Router $router) use ($filepath) { |
66
|
4 |
|
require $filepath; |
67
|
4 |
|
}); |
68
|
4 |
|
Route::model(strtolower($moduleName), $modelNameSpace); |
69
|
|
|
} |
70
|
4 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Register config. |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
4 |
|
protected function loadConfigs() |
78
|
|
|
{ |
79
|
4 |
|
foreach ($this->bootstrapService->getConfigs() as $route) { |
80
|
4 |
|
$this->publishes([ |
81
|
4 |
|
$route[0] => config_path($route[1]), |
82
|
4 |
|
], 'config'); |
83
|
4 |
|
$this->mergeConfigFrom( |
84
|
4 |
|
$route[0], basename($route[1], '.php') |
85
|
|
|
); |
86
|
|
|
} |
87
|
4 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Register additional directories of factories. |
91
|
|
|
* |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
4 |
|
public function loadFactories() |
95
|
|
|
{ |
96
|
4 |
|
foreach ($this->bootstrapService->getFactories() as $factoryPath) { |
97
|
4 |
|
if (!$this->app->environment('production')) { |
98
|
4 |
|
app(Factory::class)->load($factoryPath); |
99
|
|
|
} |
100
|
|
|
} |
101
|
4 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Register additional directories of migrations. |
105
|
|
|
* |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
4 |
|
public function loadMigrations() |
109
|
|
|
{ |
110
|
4 |
|
foreach ($this->bootstrapService->getMigrations() as $migrationPath) { |
111
|
4 |
|
$this->loadMigrationsFrom($migrationPath); |
112
|
|
|
} |
113
|
4 |
|
} |
114
|
|
|
|
115
|
4 |
|
private function loadPolicies() |
116
|
|
|
{ |
117
|
|
|
//TODO |
118
|
4 |
|
} |
119
|
|
|
|
120
|
4 |
|
private function overrideSeedCommand() |
121
|
|
|
{ |
122
|
4 |
|
$app = $this->app; |
123
|
4 |
|
$service = $this->bootstrapService; |
124
|
|
|
$this->app->extend('command.seed', function () use ($app, $service) { |
125
|
|
|
return new SeedCommand($app['db'], $service); |
126
|
4 |
|
}); |
127
|
4 |
|
} |
128
|
|
|
} |
129
|
|
|
|