We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 51 |
Total Lines | 279 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like ThemeServiceProvider often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ThemeServiceProvider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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 |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Perform post-registration booting of services. |
||
37 | * |
||
38 | * @return void |
||
39 | */ |
||
40 | public function autoboot(): void |
||
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() |
||
204 | } |
||
205 | |||
206 | protected function packageAssetsPath() |
||
207 | { |
||
208 | return $this->getPath().'/resources/assets'; |
||
209 | } |
||
210 | |||
211 | protected function packageMigrationsPath() |
||
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() |
||
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() |
||
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) |
||
274 | } |
||
275 | |||
276 | public function packageIsActiveTheme() |
||
277 | { |
||
278 | $viewNamespace = $this->vendorNameDotPackageName().'::'; |
||
279 | |||
282 | } |
||
283 | |||
284 | public function registerPackageBladeComponents() |
||
289 | }); |
||
290 | } |
||
291 | } |
||
292 | } |
||
293 |