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