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