1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\Support\Providers; |
6
|
|
|
|
7
|
|
|
use Arcanedev\Support\Exceptions\PackageException; |
8
|
|
|
use Arcanedev\Support\Providers\Concerns\{ |
9
|
|
|
HasAssets, HasConfig, HasFactories, HasMigrations, HasTranslations, HasViews |
10
|
|
|
}; |
11
|
|
|
use Illuminate\Contracts\Foundation\Application; |
12
|
|
|
use Illuminate\Support\Str; |
13
|
|
|
use ReflectionClass; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class PackageServiceProvider |
17
|
|
|
* |
18
|
|
|
* @package Arcanedev\Support\Providers |
19
|
|
|
* @author ARCANEDEV <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
abstract class PackageServiceProvider extends ServiceProvider |
22
|
|
|
{ |
23
|
|
|
/* ----------------------------------------------------------------- |
24
|
|
|
| Traits |
25
|
|
|
| ----------------------------------------------------------------- |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
use HasAssets, |
29
|
|
|
HasConfig, |
30
|
|
|
HasFactories, |
31
|
|
|
HasMigrations, |
32
|
|
|
HasTranslations, |
33
|
|
|
HasViews; |
34
|
|
|
|
35
|
|
|
/* ----------------------------------------------------------------- |
36
|
|
|
| Properties |
37
|
|
|
| ----------------------------------------------------------------- |
38
|
|
|
*/ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Vendor name. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $vendor = 'arcanedev'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Package name. |
49
|
|
|
* |
50
|
|
|
* @var string|null |
51
|
|
|
*/ |
52
|
|
|
protected $package; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Package base path. |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
protected $basePath; |
60
|
|
|
|
61
|
|
|
/* ----------------------------------------------------------------- |
62
|
|
|
| Constructor |
63
|
|
|
| ----------------------------------------------------------------- |
64
|
|
|
*/ |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Create a new service provider instance. |
68
|
|
|
* |
69
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
70
|
|
|
*/ |
71
|
18 |
|
public function __construct(Application $app) |
72
|
|
|
{ |
73
|
18 |
|
parent::__construct($app); |
74
|
|
|
|
75
|
18 |
|
$this->basePath = $this->resolveBasePath(); |
76
|
18 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Resolve the base path of the package. |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
18 |
|
protected function resolveBasePath() |
84
|
|
|
{ |
85
|
18 |
|
return dirname( |
86
|
18 |
|
(new ReflectionClass($this))->getFileName(), 2 |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/* ----------------------------------------------------------------- |
91
|
|
|
| Getters & Setters |
92
|
|
|
| ----------------------------------------------------------------- |
93
|
|
|
*/ |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get the base path of the package. |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
18 |
|
public function getBasePath() |
101
|
|
|
{ |
102
|
18 |
|
return $this->basePath; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the vendor name. |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
18 |
|
protected function getVendorName(): string |
111
|
|
|
{ |
112
|
18 |
|
return $this->vendor; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get the package name. |
117
|
|
|
* |
118
|
|
|
* @return string|null |
119
|
|
|
*/ |
120
|
18 |
|
protected function getPackageName(): ?string |
121
|
|
|
{ |
122
|
18 |
|
return $this->package; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/* ----------------------------------------------------------------- |
126
|
|
|
| Main Methods |
127
|
|
|
| ----------------------------------------------------------------- |
128
|
|
|
*/ |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Register the service provider. |
132
|
|
|
*/ |
133
|
18 |
|
public function register() |
134
|
|
|
{ |
135
|
18 |
|
parent::register(); |
136
|
|
|
|
137
|
18 |
|
$this->checkPackageName(); |
138
|
18 |
|
} |
139
|
|
|
|
140
|
|
|
/* ----------------------------------------------------------------- |
141
|
|
|
| Package Methods |
142
|
|
|
| ----------------------------------------------------------------- |
143
|
|
|
*/ |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Publish all the package files. |
147
|
|
|
*/ |
148
|
|
|
protected function publishAll(): void |
149
|
|
|
{ |
150
|
|
|
$this->publishAssets(); |
151
|
|
|
$this->publishConfig(); |
152
|
|
|
$this->publishFactories(); |
153
|
|
|
$this->publishMigrations(); |
154
|
|
|
$this->publishTranslations(); |
155
|
|
|
$this->publishViews(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/* ----------------------------------------------------------------- |
159
|
|
|
| Check Methods |
160
|
|
|
| ----------------------------------------------------------------- |
161
|
|
|
*/ |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Check package name. |
165
|
|
|
* |
166
|
|
|
* @throws \Arcanedev\Support\Exceptions\PackageException |
167
|
|
|
*/ |
168
|
18 |
|
protected function checkPackageName(): void |
169
|
|
|
{ |
170
|
18 |
|
if (empty($this->getVendorName()) || empty($this->getPackageName())) { |
171
|
6 |
|
throw PackageException::unspecifiedName(); |
172
|
|
|
} |
173
|
18 |
|
} |
174
|
|
|
|
175
|
|
|
/* ----------------------------------------------------------------- |
176
|
|
|
| Other Methods |
177
|
|
|
| ----------------------------------------------------------------- |
178
|
|
|
*/ |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Get the published tags. |
182
|
|
|
* |
183
|
|
|
* @param string $tag |
184
|
|
|
* |
185
|
|
|
* @return array |
186
|
|
|
*/ |
187
|
|
|
protected function getPublishedTags(string $tag): array |
188
|
|
|
{ |
189
|
|
|
$package = $this->getPackageName(); |
190
|
|
|
|
191
|
|
|
return array_map(function ($name) { |
192
|
|
|
return Str::slug($name); |
193
|
|
|
}, [$this->getVendorName(), $package, $tag, $package.'-'.$tag]); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|