1
|
|
|
<?php namespace Arcanedev\Support; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Support\Exceptions\PackageException; |
4
|
|
|
use Illuminate\Support\Str; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class PackageServiceProvider |
8
|
|
|
* |
9
|
|
|
* @package Arcanedev\Support\Laravel |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
abstract class PackageServiceProvider extends ServiceProvider |
13
|
|
|
{ |
14
|
|
|
/* ------------------------------------------------------------------------------------------------ |
15
|
|
|
| Properties |
16
|
|
|
| ------------------------------------------------------------------------------------------------ |
17
|
|
|
*/ |
18
|
|
|
/** |
19
|
|
|
* Vendor name. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $vendor = 'arcanedev'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Package name. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $package = ''; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Package base path. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $basePath = ''; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Merge multiple config files into one instance (package name as root key) |
41
|
|
|
* |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
protected $multiConfigs = false; |
45
|
|
|
|
46
|
|
|
/* ------------------------------------------------------------------------------------------------ |
47
|
|
|
| Getters & Setters |
48
|
|
|
| ------------------------------------------------------------------------------------------------ |
49
|
|
|
*/ |
50
|
|
|
/** |
51
|
|
|
* Get the base path of the package. |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
abstract public function getBasePath(); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get config folder. |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
9 |
|
*/ |
62
|
|
|
protected function getConfigFolder() |
63
|
9 |
|
{ |
64
|
3 |
|
return realpath($this->getBasePath().DS.'config'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get config key. |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
9 |
|
*/ |
72
|
|
|
protected function getConfigKey() |
73
|
9 |
|
{ |
74
|
|
|
return Str::slug($this->package); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get config file path. |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
9 |
|
*/ |
82
|
|
|
protected function getConfigFile() |
83
|
9 |
|
{ |
84
|
|
|
return $this->getConfigFolder().DS."{$this->package}.php"; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the migrations path. |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
protected function getMigrationsPath() |
93
|
|
|
{ |
94
|
|
|
return $this->getBasePath().'/database/migrations'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the views path. |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
private function getViewsPath() |
103
|
|
|
{ |
104
|
|
|
return $this->getBasePath().'/resources/views'; |
105
|
|
|
} |
106
|
|
|
|
107
|
9 |
|
/* ------------------------------------------------------------------------------------------------ |
108
|
|
|
| Main Functions |
109
|
9 |
|
| ------------------------------------------------------------------------------------------------ |
110
|
9 |
|
*/ |
111
|
9 |
|
/** |
112
|
|
|
* Boot the service provider. |
113
|
|
|
*/ |
114
|
|
|
public function boot() |
115
|
|
|
{ |
116
|
|
|
parent::boot(); |
117
|
|
|
|
118
|
9 |
|
$this->checkPackageName(); |
119
|
|
|
} |
120
|
9 |
|
|
121
|
9 |
|
/* ------------------------------------------------------------------------------------------------ |
122
|
|
|
| Package Functions |
123
|
9 |
|
| ------------------------------------------------------------------------------------------------ |
124
|
9 |
|
*/ |
125
|
|
|
/** |
126
|
|
|
* Setup package path and stuff. |
127
|
|
|
*/ |
128
|
|
|
protected function setup() |
129
|
|
|
{ |
130
|
|
|
$this->checkPackageName(); |
131
|
|
|
$this->registerConfig(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Register configs. |
136
|
|
|
* |
137
|
|
|
* @param string $separator |
138
|
|
|
*/ |
139
|
|
|
protected function registerConfig($separator = '.') |
140
|
|
|
{ |
141
|
|
|
if ($this->multiConfigs) |
142
|
|
|
$this->registerMultipleConfigs($separator); |
143
|
|
|
else |
144
|
|
|
$this->mergeConfigFrom($this->getConfigFile(), $this->getConfigKey()); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Register all package configs. |
149
|
|
|
* |
150
|
|
|
* @param string $separator |
151
|
|
|
*/ |
152
|
|
|
private function registerMultipleConfigs($separator = '.') |
153
|
|
|
{ |
154
|
|
|
foreach (glob($this->getConfigFolder().'/*.php') as $configPath) { |
155
|
|
|
$this->mergeConfigFrom( |
156
|
|
|
$configPath, |
157
|
|
|
$this->getConfigKey() . $separator . basename($configPath, '.php') |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Register commands service provider. |
164
|
|
|
* |
165
|
|
|
* @param \Illuminate\Support\ServiceProvider|string $provider |
166
|
|
|
*/ |
167
|
|
|
protected function registerCommands($provider) |
168
|
|
|
{ |
169
|
|
|
if ($this->app->runningInConsole()) |
170
|
|
|
$this->app->register($provider); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Publish the config file. |
175
|
|
|
*/ |
176
|
|
|
protected function publishConfig() |
177
|
|
|
{ |
178
|
|
|
$this->publishes([ |
179
|
|
|
$this->getConfigFile() => config_path("{$this->package}.php"), |
180
|
|
|
], 'config'); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Publish the migration files. |
185
|
|
|
*/ |
186
|
|
|
protected function publishMigrations() |
187
|
|
|
{ |
188
|
|
|
if (is_dir($path = $this->getMigrationsPath())) { |
189
|
|
|
$this->publishes([ |
190
|
|
|
$path => database_path('migrations'), |
191
|
|
|
], 'migrations'); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Publish and load the views if $load argument is true. |
197
|
|
|
* |
198
|
|
|
* @param bool $load |
199
|
|
|
*/ |
200
|
|
|
protected function publishViews($load = true) |
201
|
|
|
{ |
202
|
|
|
if (is_dir($path = $this->getViewsPath())) { |
203
|
|
|
$this->publishes([ |
204
|
|
|
$path => base_path("resources/views/vendor/{$this->package}"), |
205
|
|
|
], 'views'); |
206
|
|
|
|
207
|
|
|
if ($load) $this->loadViews(); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Publish and load the translations if $load argument is true. |
213
|
|
|
* |
214
|
|
|
* @param bool $load |
215
|
|
|
*/ |
216
|
|
|
protected function publishTranslations($load = true) |
217
|
|
|
{ |
218
|
|
|
if (is_dir($path = ($this->getBasePath() . '/resources/lang'))) { |
219
|
|
|
$this->publishes([ |
220
|
|
|
$path => base_path("resources/lang/vendor/{$this->package}"), |
221
|
|
|
], 'lang'); |
222
|
|
|
|
223
|
|
|
if ($load) $this->loadTranslations(); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Publish the factories. |
229
|
|
|
*/ |
230
|
|
|
protected function publishFactories() |
231
|
|
|
{ |
232
|
|
|
if (is_dir($path = ($this->getBasePath() . '/database/factories'))) { |
233
|
|
|
$this->publishes([ |
234
|
|
|
$path => database_path('factories'), |
235
|
|
|
], 'factories'); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Publish all the package files. |
241
|
|
|
* |
242
|
|
|
* @param bool $load |
243
|
|
|
*/ |
244
|
|
|
protected function publishAll($load = true) |
245
|
|
|
{ |
246
|
|
|
$this->publishConfig(); |
247
|
|
|
$this->publishMigrations(); |
248
|
|
|
$this->publishViews($load); |
249
|
|
|
$this->publishTranslations($load); |
250
|
|
|
$this->publishFactories(); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Load the views files. |
255
|
|
|
*/ |
256
|
|
|
protected function loadViews() |
257
|
9 |
|
{ |
258
|
|
|
$this->loadViewsFrom($this->getBasePath().'/resources/views', $this->package); |
259
|
9 |
|
} |
260
|
3 |
|
|
261
|
|
|
/** |
262
|
9 |
|
* Load the translations files. |
263
|
|
|
*/ |
264
|
|
|
protected function loadTranslations() |
265
|
|
|
{ |
266
|
|
|
$this->loadTranslationsFrom($this->getBasePath().'/resources/lang', $this->package); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Load the migrations files. |
271
|
|
|
*/ |
272
|
|
|
protected function loadMigrations() |
273
|
|
|
{ |
274
|
|
|
$this->loadMigrationsFrom($this->getBasePath().'/database/migrations/'); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/* ------------------------------------------------------------------------------------------------ |
278
|
|
|
| Check Functions |
279
|
|
|
| ------------------------------------------------------------------------------------------------ |
280
|
|
|
*/ |
281
|
|
|
/** |
282
|
|
|
* Check package name. |
283
|
|
|
* |
284
|
|
|
* @throws PackageException |
285
|
|
|
*/ |
286
|
|
|
private function checkPackageName() |
287
|
|
|
{ |
288
|
|
|
if (empty($this->package) || empty($this->package)) { |
289
|
|
|
throw new PackageException('You must specify the vendor/package name.'); |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Check if has the base config. |
295
|
|
|
* |
296
|
|
|
* @return bool |
297
|
|
|
*/ |
298
|
|
|
protected function hasPackageConfig() |
299
|
|
|
{ |
300
|
|
|
return $this->getConfigFile() !== false; |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|