|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mnabialek\LaravelModular\Providers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
use Illuminate\Support\ServiceProvider; |
|
7
|
|
|
use Mnabialek\LaravelModular\Models\Module; |
|
8
|
|
|
use Mnabialek\LaravelModular\Services\Config; |
|
9
|
|
|
use Mnabialek\LaravelModular\Services\Modular; |
|
10
|
|
|
use Mnabialek\LaravelModular\Console\Commands\ModuleMake; |
|
11
|
|
|
use Mnabialek\LaravelModular\Console\Commands\ModuleSeed; |
|
12
|
|
|
use Mnabialek\LaravelModular\Console\Commands\ModuleFiles; |
|
13
|
|
|
use Mnabialek\LaravelModular\Console\Commands\ModuleMakeMigration; |
|
14
|
|
|
|
|
15
|
|
|
class ModularServiceProvider extends ServiceProvider |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var Collection|array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $filesToPublish = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Register the service provider. |
|
24
|
|
|
* |
|
25
|
|
|
* @return void |
|
26
|
|
|
*/ |
|
27
|
|
|
public function register() |
|
28
|
|
|
{ |
|
29
|
|
|
// register module binding |
|
30
|
|
|
$this->app->singleton('modular.config', function ($app) { |
|
31
|
|
|
return new Config($app); |
|
32
|
|
|
}); |
|
33
|
|
|
|
|
34
|
|
|
$this->app->singleton('modular', function ($app) { |
|
35
|
|
|
return new Modular($app, $app['modular.config']); |
|
36
|
|
|
}); |
|
37
|
|
|
|
|
38
|
|
|
// merge module default configuration in case of not created yet or used |
|
39
|
|
|
// only some parts of it |
|
40
|
|
|
$configName = $this->app['modular.config']->configName(); |
|
41
|
|
|
$this->mergeConfigFrom( |
|
42
|
|
|
$this->getDefaultConfigFilePath($configName), $configName |
|
43
|
|
|
); |
|
44
|
|
|
|
|
45
|
|
|
// register new Artisan commands |
|
46
|
|
|
$this->commands([ |
|
47
|
|
|
ModuleMake::class, |
|
48
|
|
|
ModuleSeed::class, |
|
49
|
|
|
ModuleMakeMigration::class, |
|
50
|
|
|
ModuleFiles::class, |
|
51
|
|
|
]); |
|
52
|
|
|
|
|
53
|
|
|
// register files to be published |
|
54
|
|
|
$this->publishes($this->getFilesToPublish()->all()); |
|
55
|
|
|
|
|
56
|
|
|
// set migrations paths |
|
57
|
|
|
$this->setModulesMigrationPaths(); |
|
58
|
|
|
|
|
59
|
|
|
// register modules providers |
|
60
|
|
|
$this->app['modular']->loadServiceProviders(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
*/ |
|
66
|
|
|
public function provides() |
|
67
|
|
|
{ |
|
68
|
|
|
return ['modular', 'modular.config']; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get files that should be published |
|
73
|
|
|
* |
|
74
|
|
|
* @return Collection |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function getFilesToPublish() |
|
77
|
|
|
{ |
|
78
|
|
|
$this->filesToPublish = collect(); |
|
79
|
|
|
|
|
80
|
|
|
$this->addConfigurationToPublished() |
|
81
|
|
|
->addStubsTemplatesToPublished() |
|
82
|
|
|
->addAppFilesToPublished(); |
|
83
|
|
|
|
|
84
|
|
|
return $this->filesToPublish; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Add configuration file to published files |
|
89
|
|
|
* |
|
90
|
|
|
* @return $this |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function addConfigurationToPublished() |
|
93
|
|
|
{ |
|
94
|
|
|
$configName = $this->app['modular.config']->configName(); |
|
95
|
|
|
$this->filesToPublish->put($this->getDefaultConfigFilePath($configName), |
|
96
|
|
|
$this->app['modular.config']->configPath()); |
|
97
|
|
|
|
|
98
|
|
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Add stubs templates to published files |
|
103
|
|
|
* |
|
104
|
|
|
* @return $this |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function addStubsTemplatesToPublished() |
|
107
|
|
|
{ |
|
108
|
|
|
$templatesPath = $this->getTemplatesStubsPath(); |
|
109
|
|
|
$pathLength = mb_strlen($templatesPath); |
|
110
|
|
|
|
|
111
|
|
|
// here we get all stubs files from stubs templates directory |
|
112
|
|
|
$publishedStubsPath = $this->app['modular.config']->stubsPath(); |
|
113
|
|
|
|
|
114
|
|
|
collect(glob($templatesPath . '/*/{,.}*.stub', GLOB_BRACE)) |
|
115
|
|
|
->each(function ($file) use ($publishedStubsPath, $pathLength) { |
|
116
|
|
|
$this->filesToPublish->put($file, |
|
117
|
|
|
$publishedStubsPath . DIRECTORY_SEPARATOR . |
|
118
|
|
|
mb_substr($file, $pathLength + 1)); |
|
119
|
|
|
}); |
|
120
|
|
|
|
|
121
|
|
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Add app files to published files |
|
126
|
|
|
* |
|
127
|
|
|
* @return $this |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function addAppFilesToPublished() |
|
130
|
|
|
{ |
|
131
|
|
|
$appPath = $this->getAppSamplePath(); |
|
132
|
|
|
collect(glob($appPath . '/*/*'))->each(function ($file) use ($appPath) { |
|
133
|
|
|
$this->filesToPublish->put($file, |
|
134
|
|
|
$this->app['path'] . DIRECTORY_SEPARATOR . |
|
135
|
|
|
mb_substr($file, mb_strlen($appPath) + 1)); |
|
136
|
|
|
}); |
|
137
|
|
|
|
|
138
|
|
|
return $this; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Get stub templates directory |
|
143
|
|
|
* |
|
144
|
|
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function getTemplatesStubsPath() |
|
147
|
|
|
{ |
|
148
|
|
|
return realpath(__DIR__ . '/../../stubs/templates/'); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Get default configuration file path |
|
153
|
|
|
* |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getDefaultConfigFilePath($configName) |
|
157
|
|
|
{ |
|
158
|
|
|
return realpath(__DIR__ . "/../../config/{$configName}.php"); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Get sample app path |
|
163
|
|
|
* |
|
164
|
|
|
* @return string |
|
165
|
|
|
*/ |
|
166
|
|
|
protected function getAppSamplePath() |
|
167
|
|
|
{ |
|
168
|
|
|
return realpath(__DIR__ . '/../../stubs/app'); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Set migrations paths for all active modules |
|
173
|
|
|
*/ |
|
174
|
|
|
protected function setModulesMigrationPaths() |
|
175
|
|
|
{ |
|
176
|
|
|
$paths = collect(); |
|
177
|
|
|
|
|
178
|
|
|
// add to paths all migration directories from modules |
|
179
|
|
|
collect($this->app['modular']->active()) |
|
180
|
|
|
->each(function ($module) use ($paths) { |
|
181
|
|
|
/* @var Module $module */ |
|
182
|
|
|
$paths->push($module->migrationsPath()); |
|
183
|
|
|
}); |
|
184
|
|
|
|
|
185
|
|
|
$this->loadMigrationsFrom($paths->all()); |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|