Total Complexity | 53 |
Total Lines | 377 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 0 |
Complex classes like ThemeResourceLoader 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 ThemeResourceLoader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class ThemeResourceLoader implements Flushable |
||
16 | { |
||
17 | |||
18 | /** |
||
19 | * @var ThemeResourceLoader |
||
20 | */ |
||
21 | private static $instance; |
||
22 | |||
23 | /** |
||
24 | * The base path of the application |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $base; |
||
29 | |||
30 | /** |
||
31 | * List of template "sets" that contain a test manifest, and have an alias. |
||
32 | * E.g. '$default' |
||
33 | * |
||
34 | * @var ThemeList[] |
||
35 | */ |
||
36 | protected $sets = []; |
||
37 | |||
38 | /** |
||
39 | * @var CacheInterface |
||
40 | */ |
||
41 | protected $cache; |
||
42 | |||
43 | /** |
||
44 | * @return ThemeResourceLoader |
||
45 | */ |
||
46 | public static function inst() |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Set instance |
||
53 | * |
||
54 | * @param ThemeResourceLoader $instance |
||
55 | */ |
||
56 | public static function set_instance(ThemeResourceLoader $instance) |
||
57 | { |
||
58 | self::$instance = $instance; |
||
59 | } |
||
60 | |||
61 | public function __construct($base = null) |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Add a new theme manifest for a given identifier. E.g. '$default' |
||
68 | * |
||
69 | * @param string $set |
||
70 | * @param ThemeList $manifest |
||
71 | */ |
||
72 | public function addSet($set, ThemeList $manifest) |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Get a named theme set |
||
79 | * |
||
80 | * @param string $set |
||
81 | * @return ThemeList |
||
82 | */ |
||
83 | public function getSet($set) |
||
84 | { |
||
85 | if (isset($this->sets[$set])) { |
||
86 | return $this->sets[$set]; |
||
87 | } |
||
88 | return null; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Given a theme identifier, determine the path from the root directory |
||
93 | * |
||
94 | * The mapping from $identifier to path follows these rules: |
||
95 | * - A simple theme name ('mytheme') which maps to the standard themes dir (/themes/mytheme) |
||
96 | * - A theme path with a leading slash ('/mymodule/themes/mytheme') which maps directly to that path. |
||
97 | * - or a vendored theme path. (vendor/mymodule:mytheme) which maps to the nested 'theme' within |
||
98 | * that module. ('/mymodule/themes/mytheme'). |
||
99 | * - A vendored module with no nested theme (vendor/mymodule) which maps to the root directory |
||
100 | * of that module. ('/mymodule'). |
||
101 | * |
||
102 | * @param string $identifier Theme identifier. |
||
103 | * @return string Path from root, not including leading or trailing forward slash. E.g. themes/mytheme |
||
104 | */ |
||
105 | public function getPath($identifier) |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Attempts to find possible candidate templates from a set of template |
||
165 | * names from modules, current theme directory and finally the application |
||
166 | * folder. |
||
167 | * |
||
168 | * The template names can be passed in as plain strings, or be in the |
||
169 | * format "type/name", where type is the type of template to search for |
||
170 | * (e.g. Includes, Layout). |
||
171 | * |
||
172 | * The results of this method will be cached for future use. |
||
173 | * |
||
174 | * @param string|array $template Template name, or template spec in array format with the keys |
||
175 | * 'type' (type string) and 'templates' (template hierarchy in order of precedence). |
||
176 | * If 'templates' is ommitted then any other item in the array will be treated as the template |
||
177 | * list, or list of templates each in the array spec given. |
||
178 | * Templates with an .ss extension will be treated as file paths, and will bypass |
||
179 | * theme-coupled resolution. |
||
180 | * @param array $themes List of themes to use to resolve themes. Defaults to {@see SSViewer::get_themes()} |
||
181 | * @return string Absolute path to resolved template file, or null if not resolved. |
||
182 | * File location will be in the format themes/<theme>/templates/<directories>/<type>/<basename>.ss |
||
183 | * Note that type (e.g. 'Layout') is not the root level directory under 'templates'. |
||
184 | */ |
||
185 | public function findTemplate($template, $themes = null) |
||
186 | { |
||
187 | if ($themes === null) { |
||
188 | $themes = SSViewer::get_themes(); |
||
189 | } |
||
190 | |||
191 | // Look for a cached result for this data set |
||
192 | $cacheKey = md5(json_encode($template) . json_encode($themes)); |
||
193 | if ($this->getCache()->has($cacheKey)) { |
||
194 | return $this->getCache()->get($cacheKey); |
||
195 | } |
||
196 | |||
197 | $type = ''; |
||
198 | if (is_array($template)) { |
||
199 | // Check if templates has type specified |
||
200 | if (array_key_exists('type', $template)) { |
||
201 | $type = $template['type']; |
||
202 | unset($template['type']); |
||
203 | } |
||
204 | // Templates are either nested in 'templates' or just the rest of the list |
||
205 | $templateList = array_key_exists('templates', $template) ? $template['templates'] : $template; |
||
206 | } else { |
||
207 | $templateList = array($template); |
||
208 | } |
||
209 | |||
210 | foreach ($templateList as $i => $template) { |
||
211 | // Check if passed list of templates in array format |
||
212 | if (is_array($template)) { |
||
213 | $path = $this->findTemplate($template, $themes); |
||
214 | if ($path) { |
||
215 | $this->getCache()->set($cacheKey, $path); |
||
216 | return $path; |
||
217 | } |
||
218 | continue; |
||
219 | } |
||
220 | |||
221 | // If we have an .ss extension, this is a path, not a template name. We should |
||
222 | // pass in templates without extensions in order for template manifest to find |
||
223 | // files dynamically. |
||
224 | if (substr($template, -3) == '.ss' && file_exists($template)) { |
||
225 | $this->getCache()->set($cacheKey, $template); |
||
226 | return $template; |
||
227 | } |
||
228 | |||
229 | // Check string template identifier |
||
230 | $template = str_replace('\\', '/', $template); |
||
231 | $parts = explode('/', $template); |
||
232 | |||
233 | $tail = array_pop($parts); |
||
234 | $head = implode('/', $parts); |
||
235 | $themePaths = $this->getThemePaths($themes); |
||
236 | foreach ($themePaths as $themePath) { |
||
237 | // Join path |
||
238 | $pathParts = [ $this->base, $themePath, 'templates', $head, $type, $tail ]; |
||
239 | try { |
||
240 | $path = Path::join($pathParts) . '.ss'; |
||
241 | if (file_exists($path)) { |
||
242 | $this->getCache()->set($cacheKey, $path); |
||
243 | return $path; |
||
244 | } |
||
245 | } catch (InvalidArgumentException $e) { |
||
246 | // No-op |
||
247 | } |
||
248 | } |
||
249 | } |
||
250 | |||
251 | // No template found |
||
252 | $this->getCache()->set($cacheKey, null); |
||
253 | return null; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Resolve themed CSS path |
||
258 | * |
||
259 | * @param string $name Name of CSS file without extension |
||
260 | * @param array $themes List of themes, Defaults to {@see SSViewer::get_themes()} |
||
261 | * @return string Path to resolved CSS file (relative to base dir) |
||
262 | */ |
||
263 | public function findThemedCSS($name, $themes = null) |
||
264 | { |
||
265 | if ($themes === null) { |
||
266 | $themes = SSViewer::get_themes(); |
||
267 | } |
||
268 | |||
269 | if (substr($name, -4) !== '.css') { |
||
270 | $name .= '.css'; |
||
271 | } |
||
272 | |||
273 | $filename = $this->findThemedResource("css/$name", $themes); |
||
274 | if ($filename === null) { |
||
275 | $filename = $this->findThemedResource($name, $themes); |
||
276 | } |
||
277 | |||
278 | return $filename; |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Resolve themed javascript path |
||
283 | * |
||
284 | * A javascript file in the current theme path name 'themename/javascript/$name.js' is first searched for, |
||
285 | * and it that doesn't exist and the module parameter is set then a javascript file with that name in |
||
286 | * the module is used. |
||
287 | * |
||
288 | * @param string $name The name of the file - eg '/js/File.js' would have the name 'File' |
||
289 | * @param array $themes List of themes, Defaults to {@see SSViewer::get_themes()} |
||
290 | * @return string Path to resolved javascript file (relative to base dir) |
||
291 | */ |
||
292 | public function findThemedJavascript($name, $themes = null) |
||
293 | { |
||
294 | if ($themes === null) { |
||
295 | $themes = SSViewer::get_themes(); |
||
296 | } |
||
297 | |||
298 | if (substr($name, -3) !== '.js') { |
||
299 | $name .= '.js'; |
||
300 | } |
||
301 | |||
302 | $filename = $this->findThemedResource("javascript/$name", $themes); |
||
303 | if ($filename === null) { |
||
304 | $filename = $this->findThemedResource($name, $themes); |
||
305 | } |
||
306 | |||
307 | return $filename; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Resolve a themed resource |
||
312 | * |
||
313 | * A themed resource and be any file that resides in a theme folder. |
||
314 | * |
||
315 | * @param string $resource A file path relative to the root folder of a theme |
||
316 | * @param array $themes An order listed of themes to search, Defaults to {@see SSViewer::get_themes()} |
||
317 | * @return string |
||
318 | */ |
||
319 | public function findThemedResource($resource, $themes = null) |
||
320 | { |
||
321 | if ($themes === null) { |
||
322 | $themes = SSViewer::get_themes(); |
||
323 | } |
||
324 | |||
325 | $paths = $this->getThemePaths($themes); |
||
326 | |||
327 | foreach ($paths as $themePath) { |
||
328 | $relativePath = Path::join($themePath, $resource); |
||
329 | $absolutePath = Path::join($this->base, $relativePath); |
||
330 | if (file_exists($absolutePath)) { |
||
331 | return $relativePath; |
||
332 | } |
||
333 | } |
||
334 | |||
335 | // Resource exists in no context |
||
336 | return null; |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * Resolve all themes to the list of root folders relative to site root |
||
341 | * |
||
342 | * @param array $themes List of themes to resolve. Supports named theme sets. Defaults to {@see SSViewer::get_themes()}. |
||
343 | * @return array List of root-relative folders in order of precendence. |
||
344 | */ |
||
345 | public function getThemePaths($themes = null) |
||
346 | { |
||
347 | if ($themes === null) { |
||
348 | $themes = SSViewer::get_themes(); |
||
349 | } |
||
350 | |||
351 | $paths = []; |
||
352 | foreach ($themes as $themename) { |
||
353 | // Expand theme sets |
||
354 | $set = $this->getSet($themename); |
||
355 | $subthemes = $set ? $set->getThemes() : [$themename]; |
||
356 | |||
357 | // Resolve paths |
||
358 | foreach ($subthemes as $theme) { |
||
359 | $paths[] = $this->getPath($theme); |
||
360 | } |
||
361 | } |
||
362 | return $paths; |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * Flush any cached data |
||
367 | */ |
||
368 | public static function flush() |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * @return CacheInterface |
||
375 | */ |
||
376 | public function getCache() |
||
377 | { |
||
378 | if (!$this->cache) { |
||
379 | $this->setCache(Injector::inst()->get(CacheInterface::class . '.ThemeResourceLoader')); |
||
380 | } |
||
381 | return $this->cache; |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * @param CacheInterface $cache |
||
386 | * @return ThemeResourceLoader |
||
387 | */ |
||
388 | public function setCache(CacheInterface $cache) |
||
392 | } |
||
393 | } |
||
394 |