We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
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 ?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()) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
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 | } |
||
96 | |||
97 | /** |
||
98 | * Register any package services. |
||
99 | * |
||
100 | * @return void |
||
101 | */ |
||
102 | public function register(): void |
||
103 | { |
||
104 | if ($this->packageDirectoryExistsAndIsNotEmpty('config')) { |
||
105 | $this->mergeConfigFrom($this->packageConfigFile(), $this->vendorNameDotPackageName()); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Console-specific booting. |
||
111 | * |
||
112 | * @return void |
||
113 | */ |
||
114 | protected function bootForConsole(): void |
||
115 | { |
||
116 | // Publishing the configuration file. |
||
117 | if ($this->packageDirectoryExistsAndIsNotEmpty('config')) { |
||
118 | $this->publishes([ |
||
119 | $this->packageConfigFile() => $this->publishedConfigFile(), |
||
120 | ], $this->packageName.'-config'); |
||
121 | } |
||
122 | |||
123 | // Publishing the views. |
||
124 | if ($this->packageDirectoryExistsAndIsNotEmpty('resources/views')) { |
||
125 | $this->publishes([ |
||
126 | $this->packageViewsPath() => $this->publishedViewsPath(), |
||
127 | ], 'views'); |
||
128 | |||
129 | // Add basset view path |
||
130 | Basset::addViewPath($this->packageViewsPath()); |
||
131 | } |
||
132 | |||
133 | // Publishing assets. |
||
134 | if ($this->packageDirectoryExistsAndIsNotEmpty('resources/assets')) { |
||
135 | $this->publishes([ |
||
136 | $this->packageAssetsPath() => $this->publishedAssetsPath(), |
||
137 | ], 'assets'); |
||
138 | } |
||
139 | |||
140 | // Publishing the translation files. |
||
141 | if ($this->packageDirectoryExistsAndIsNotEmpty('resources/lang')) { |
||
142 | $this->publishes([ |
||
143 | $this->packageLangsPath() => $this->publishedLangsPath(), |
||
144 | ], 'lang'); |
||
145 | } |
||
146 | |||
147 | // Registering package commands. |
||
148 | if (! empty($this->commands)) { |
||
149 | $this->commands($this->commands); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * ------------------- |
||
155 | * CONVENIENCE METHODS |
||
156 | * -------------------. |
||
157 | */ |
||
158 | protected function vendorNameDotPackageName() |
||
159 | { |
||
160 | return $this->vendorName.'.'.$this->packageName; |
||
161 | } |
||
162 | |||
163 | protected function vendorNameSlashPackageName() |
||
164 | { |
||
165 | return $this->vendorName.'/'.$this->packageName; |
||
166 | } |
||
167 | |||
168 | // ------------- |
||
169 | // Package paths |
||
170 | // ------------- |
||
171 | |||
172 | protected function getPath() |
||
173 | { |
||
174 | return $this->path ?? base_path('vendor/'.$this->vendorName.'/'.$this->packageName); |
||
175 | } |
||
176 | |||
177 | protected function packageViewsPath() |
||
178 | { |
||
179 | return $this->getPath().'/resources/views'; |
||
180 | } |
||
181 | |||
182 | protected function packageLangsPath() |
||
183 | { |
||
184 | return $this->getPath().'/resources/lang'; |
||
185 | } |
||
186 | |||
187 | protected function packageAssetsPath() |
||
188 | { |
||
189 | return $this->getPath().'/resources/assets'; |
||
190 | } |
||
191 | |||
192 | protected function packageMigrationsPath() |
||
193 | { |
||
194 | return $this->getPath().'/database/migrations'; |
||
195 | } |
||
196 | |||
197 | protected function packageConfigFile() |
||
198 | { |
||
199 | return $this->getPath().'/config/'.$this->packageName.'.php'; |
||
200 | } |
||
201 | |||
202 | protected function packageRoutesFile() |
||
203 | { |
||
204 | return $this->getPath().'/routes/'.$this->packageName.'.php'; |
||
205 | } |
||
206 | |||
207 | protected function packageHelpersFile() |
||
208 | { |
||
209 | return $this->getPath().'/bootstrap/helpers.php'; |
||
210 | } |
||
211 | |||
212 | // --------------- |
||
213 | // Published paths |
||
214 | // --------------- |
||
215 | |||
216 | protected function publishedViewsPath() |
||
217 | { |
||
218 | return base_path('resources/views/vendor/'.$this->vendorName.'/'.$this->packageName); |
||
219 | } |
||
220 | |||
221 | protected function publishedConfigFile() |
||
222 | { |
||
223 | return config_path($this->vendorNameSlashPackageName().'.php'); |
||
224 | } |
||
225 | |||
226 | protected function publishedAssetsPath() |
||
227 | { |
||
228 | return public_path('vendor/'.$this->vendorNameSlashPackageName()); |
||
229 | } |
||
230 | |||
231 | protected function publishedLangsPath() |
||
232 | { |
||
233 | return resource_path('lang/vendor/'.$this->vendorName); |
||
234 | } |
||
235 | |||
236 | // ------------- |
||
237 | // Miscellaneous |
||
238 | // ------------- |
||
239 | |||
240 | protected function packageDirectoryExistsAndIsNotEmpty($name) |
||
241 | { |
||
242 | // check if directory exists |
||
243 | if (! is_dir($this->getPath().'/'.$name)) { |
||
244 | return false; |
||
245 | } |
||
246 | |||
247 | // check if directory has files |
||
248 | foreach (scandir($this->getPath().'/'.$name) as $file) { |
||
249 | if ($file != '.' && $file != '..' && $file != '.gitkeep') { |
||
250 | return true; |
||
251 | } |
||
252 | } |
||
253 | |||
254 | return false; |
||
255 | } |
||
256 | |||
257 | public function packageIsActiveTheme() |
||
258 | { |
||
259 | $viewNamespace = $this->vendorNameDotPackageName().'::'; |
||
260 | |||
261 | return config('backpack.ui.view_namespace') === $viewNamespace || |
||
262 | config('backpack.ui.view_namespace_fallback') === $viewNamespace; |
||
263 | } |
||
264 | |||
265 | public function registerPackageBladeComponents() |
||
266 | { |
||
267 | if ($this->componentsNamespace) { |
||
268 | $this->app->afterResolving(BladeCompiler::class, function () { |
||
269 | Blade::componentNamespace($this->componentsNamespace, $this->packageName); |
||
270 | }); |
||
271 | } |
||
272 | } |
||
273 | } |
||
274 |