1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD; |
4
|
|
|
|
5
|
|
|
use Backpack\Basset\Facades\Basset; |
6
|
|
|
use Illuminate\Support\Facades\Blade; |
7
|
|
|
use Illuminate\Support\ServiceProvider; |
8
|
|
|
use Illuminate\View\Compilers\BladeCompiler; |
9
|
|
|
|
10
|
|
|
class ThemeServiceProvider extends ServiceProvider |
11
|
|
|
{ |
12
|
|
|
protected string $path; // the root directory of the theme |
13
|
|
|
protected string $vendorName = 'backpack'; |
14
|
|
|
protected string $packageName = 'theme-name'; |
15
|
|
|
protected array $commands = []; |
16
|
|
|
protected bool $theme = true; |
17
|
|
|
protected null|string $componentsNamespace = null; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* ------------------------- |
21
|
|
|
* SERVICE PROVIDER DEFAULTS |
22
|
|
|
* -------------------------. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Boot method may be overridden by AddonServiceProvider. |
27
|
|
|
* |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
public function boot(): void |
31
|
|
|
{ |
32
|
|
|
$this->autoboot(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Perform post-registration booting of services. |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
public function autoboot(): void |
41
|
|
|
{ |
42
|
|
|
if ($this->packageDirectoryExistsAndIsNotEmpty('bootstrap') && |
43
|
|
|
file_exists($helpers = $this->packageHelpersFile())) { |
44
|
|
|
require $helpers; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ($this->packageDirectoryExistsAndIsNotEmpty('resources/lang')) { |
48
|
|
|
$this->loadTranslationsFrom($this->packageLangsPath(), $this->vendorNameDotPackageName()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($this->packageDirectoryExistsAndIsNotEmpty('resources/views')) { |
52
|
|
|
$this->loadViews(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($this->packageDirectoryExistsAndIsNotEmpty('database/migrations')) { |
56
|
|
|
$this->loadMigrationsFrom($this->packageMigrationsPath()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ($this->packageDirectoryExistsAndIsNotEmpty('routes')) { |
60
|
|
|
$this->loadRoutesFrom($this->packageRoutesFile()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->registerPackageBladeComponents(); |
64
|
|
|
|
65
|
|
|
// Publishing is only necessary when using the CLI. |
66
|
|
|
if (app()->runningInConsole()) { |
|
|
|
|
67
|
|
|
$this->bootForConsole(); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function loadViews() |
72
|
|
|
{ |
73
|
|
|
// if this addon is a theme, but isn't active, don't load any views |
74
|
|
|
// if ($this->theme && !$this->packageIsActiveTheme()) { |
75
|
|
|
// return; |
76
|
|
|
// } |
77
|
|
|
|
78
|
|
|
// Load published views |
79
|
|
|
if (is_dir($this->publishedViewsPath())) { |
80
|
|
|
$this->loadViewsFrom($this->publishedViewsPath(), $this->vendorNameDotPackageName()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// Fallback to package views |
84
|
|
|
$this->loadViewsFrom($this->packageViewsPath(), $this->vendorNameDotPackageName()); |
85
|
|
|
|
86
|
|
|
// Add default ViewNamespaces |
87
|
|
|
foreach (['buttons', 'columns', 'fields', 'filters', 'widgets'] as $viewNamespace) { |
88
|
|
|
if ($this->packageDirectoryExistsAndIsNotEmpty("resources/views/$viewNamespace")) { |
89
|
|
|
ViewNamespaces::addFor($viewNamespace, $this->vendorNameDotPackageName()."::{$viewNamespace}"); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// Add basset view path |
94
|
|
|
Basset::addViewPath($this->packageViewsPath()); |
95
|
|
|
foreach (config($this->vendorNameDotPackageName().'.styles', []) as $path) { |
96
|
|
|
if (is_array($path)) { |
97
|
|
|
foreach ($path as $style) { |
98
|
|
|
Basset::map($style, $style); |
99
|
|
|
} |
100
|
|
|
} else { |
101
|
|
|
Basset::map($path, $path); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
foreach (config($this->vendorNameDotPackageName().'.scripts', []) as $path) { |
106
|
|
|
if (is_array($path)) { |
107
|
|
|
foreach ($path as $script) { |
108
|
|
|
Basset::map($script, $script); |
109
|
|
|
} |
110
|
|
|
} else { |
111
|
|
|
Basset::map($path, $path); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Register any package services. |
118
|
|
|
* |
119
|
|
|
* @return void |
120
|
|
|
*/ |
121
|
|
|
public function register(): void |
122
|
|
|
{ |
123
|
|
|
if ($this->packageDirectoryExistsAndIsNotEmpty('config')) { |
124
|
|
|
$this->mergeConfigFrom($this->packageConfigFile(), $this->vendorNameDotPackageName()); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Console-specific booting. |
130
|
|
|
* |
131
|
|
|
* @return void |
132
|
|
|
*/ |
133
|
|
|
protected function bootForConsole(): void |
134
|
|
|
{ |
135
|
|
|
// Publishing the configuration file. |
136
|
|
|
if ($this->packageDirectoryExistsAndIsNotEmpty('config')) { |
137
|
|
|
$this->publishes([ |
138
|
|
|
$this->packageConfigFile() => $this->publishedConfigFile(), |
139
|
|
|
], $this->packageName.'-config'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// Publishing the views. |
143
|
|
|
if ($this->packageDirectoryExistsAndIsNotEmpty('resources/views')) { |
144
|
|
|
$this->publishes([ |
145
|
|
|
$this->packageViewsPath() => $this->publishedViewsPath(), |
146
|
|
|
], 'views'); |
147
|
|
|
|
148
|
|
|
// Add basset view path |
149
|
|
|
Basset::addViewPath($this->packageViewsPath()); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// Publishing assets. |
153
|
|
|
if ($this->packageDirectoryExistsAndIsNotEmpty('resources/assets')) { |
154
|
|
|
$this->publishes([ |
155
|
|
|
$this->packageAssetsPath() => $this->publishedAssetsPath(), |
156
|
|
|
], 'assets'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
// Publishing the translation files. |
160
|
|
|
if ($this->packageDirectoryExistsAndIsNotEmpty('resources/lang')) { |
161
|
|
|
$this->publishes([ |
162
|
|
|
$this->packageLangsPath() => $this->publishedLangsPath(), |
163
|
|
|
], 'lang'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
// Registering package commands. |
167
|
|
|
if (! empty($this->commands)) { |
168
|
|
|
$this->commands($this->commands); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* ------------------- |
174
|
|
|
* CONVENIENCE METHODS |
175
|
|
|
* -------------------. |
176
|
|
|
*/ |
177
|
|
|
protected function vendorNameDotPackageName() |
178
|
|
|
{ |
179
|
|
|
return $this->vendorName.'.'.$this->packageName; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
protected function vendorNameSlashPackageName() |
183
|
|
|
{ |
184
|
|
|
return $this->vendorName.'/'.$this->packageName; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
// ------------- |
188
|
|
|
// Package paths |
189
|
|
|
// ------------- |
190
|
|
|
|
191
|
|
|
protected function getPath() |
192
|
|
|
{ |
193
|
|
|
return $this->path ?? base_path('vendor/'.$this->vendorName.'/'.$this->packageName); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
protected function packageViewsPath() |
197
|
|
|
{ |
198
|
|
|
return $this->getPath().'/resources/views'; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
protected function packageLangsPath() |
202
|
|
|
{ |
203
|
|
|
return $this->getPath().'/resources/lang'; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
protected function packageAssetsPath() |
207
|
|
|
{ |
208
|
|
|
return $this->getPath().'/resources/assets'; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
protected function packageMigrationsPath() |
212
|
|
|
{ |
213
|
|
|
return $this->getPath().'/database/migrations'; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
protected function packageConfigFile() |
217
|
|
|
{ |
218
|
|
|
return $this->getPath().'/config/'.$this->packageName.'.php'; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
protected function packageRoutesFile() |
222
|
|
|
{ |
223
|
|
|
return $this->getPath().'/routes/'.$this->packageName.'.php'; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
protected function packageHelpersFile() |
227
|
|
|
{ |
228
|
|
|
return $this->getPath().'/bootstrap/helpers.php'; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
// --------------- |
232
|
|
|
// Published paths |
233
|
|
|
// --------------- |
234
|
|
|
|
235
|
|
|
protected function publishedViewsPath() |
236
|
|
|
{ |
237
|
|
|
return base_path('resources/views/vendor/'.$this->vendorName.'/'.$this->packageName); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
protected function publishedConfigFile() |
241
|
|
|
{ |
242
|
|
|
return config_path($this->vendorNameSlashPackageName().'.php'); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
protected function publishedAssetsPath() |
246
|
|
|
{ |
247
|
|
|
return public_path('vendor/'.$this->vendorNameSlashPackageName()); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
protected function publishedLangsPath() |
251
|
|
|
{ |
252
|
|
|
return resource_path('lang/vendor/'.$this->vendorName); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
// ------------- |
256
|
|
|
// Miscellaneous |
257
|
|
|
// ------------- |
258
|
|
|
|
259
|
|
|
protected function packageDirectoryExistsAndIsNotEmpty($name) |
260
|
|
|
{ |
261
|
|
|
// check if directory exists |
262
|
|
|
if (! is_dir($this->getPath().'/'.$name)) { |
263
|
|
|
return false; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
// check if directory has files |
267
|
|
|
foreach (scandir($this->getPath().'/'.$name) as $file) { |
268
|
|
|
if ($file != '.' && $file != '..' && $file != '.gitkeep') { |
269
|
|
|
return true; |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
return false; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
public function packageIsActiveTheme() |
277
|
|
|
{ |
278
|
|
|
$viewNamespace = $this->vendorNameDotPackageName().'::'; |
279
|
|
|
|
280
|
|
|
return config('backpack.ui.view_namespace') === $viewNamespace || |
281
|
|
|
config('backpack.ui.view_namespace_fallback') === $viewNamespace; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
public function registerPackageBladeComponents() |
285
|
|
|
{ |
286
|
|
|
if ($this->componentsNamespace) { |
287
|
|
|
$this->app->afterResolving(BladeCompiler::class, function () { |
288
|
|
|
Blade::componentNamespace($this->componentsNamespace, $this->packageName); |
289
|
|
|
}); |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|