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