Total Complexity | 47 |
Total Lines | 327 |
Duplicated Lines | 0 % |
Changes | 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 |
||
12 | class ThemeResourceLoader |
||
13 | { |
||
14 | |||
15 | /** |
||
16 | * @var ThemeResourceLoader |
||
17 | */ |
||
18 | private static $instance; |
||
19 | |||
20 | /** |
||
21 | * The base path of the application |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $base; |
||
26 | |||
27 | /** |
||
28 | * List of template "sets" that contain a test manifest, and have an alias. |
||
29 | * E.g. '$default' |
||
30 | * |
||
31 | * @var ThemeList[] |
||
32 | */ |
||
33 | protected $sets = []; |
||
34 | |||
35 | /** |
||
36 | * @return ThemeResourceLoader |
||
37 | */ |
||
38 | public static function inst() |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Set instance |
||
45 | * |
||
46 | * @param ThemeResourceLoader $instance |
||
47 | */ |
||
48 | public static function set_instance(ThemeResourceLoader $instance) |
||
49 | { |
||
50 | self::$instance = $instance; |
||
51 | } |
||
52 | |||
53 | public function __construct($base = null) |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Add a new theme manifest for a given identifier. E.g. '$default' |
||
60 | * |
||
61 | * @param string $set |
||
62 | * @param ThemeList $manifest |
||
63 | */ |
||
64 | public function addSet($set, ThemeList $manifest) |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Get a named theme set |
||
71 | * |
||
72 | * @param string $set |
||
73 | * @return ThemeList |
||
74 | */ |
||
75 | public function getSet($set) |
||
76 | { |
||
77 | if (isset($this->sets[$set])) { |
||
78 | return $this->sets[$set]; |
||
79 | } |
||
80 | return null; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Given a theme identifier, determine the path from the root directory |
||
85 | * |
||
86 | * The mapping from $identifier to path follows these rules: |
||
87 | * - A simple theme name ('mytheme') which maps to the standard themes dir (/themes/mytheme) |
||
88 | * - A theme path with a leading slash ('/mymodule/themes/mytheme') which maps directly to that path. |
||
89 | * - or a vendored theme path. (vendor/mymodule:mytheme) which maps to the nested 'theme' within |
||
90 | * that module. ('/mymodule/themes/mytheme'). |
||
91 | * - A vendored module with no nested theme (vendor/mymodule) which maps to the root directory |
||
92 | * of that module. ('/mymodule'). |
||
93 | * |
||
94 | * @param string $identifier Theme identifier. |
||
95 | * @return string Path from root, not including leading or trailing forward slash. E.g. themes/mytheme |
||
96 | */ |
||
97 | public function getPath($identifier) |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Attempts to find possible candidate templates from a set of template |
||
157 | * names from modules, current theme directory and finally the application |
||
158 | * folder. |
||
159 | * |
||
160 | * The template names can be passed in as plain strings, or be in the |
||
161 | * format "type/name", where type is the type of template to search for |
||
162 | * (e.g. Includes, Layout). |
||
163 | * |
||
164 | * @param string|array $template Template name, or template spec in array format with the keys |
||
165 | * 'type' (type string) and 'templates' (template hierarchy in order of precedence). |
||
166 | * If 'templates' is ommitted then any other item in the array will be treated as the template |
||
167 | * list, or list of templates each in the array spec given. |
||
168 | * Templates with an .ss extension will be treated as file paths, and will bypass |
||
169 | * theme-coupled resolution. |
||
170 | * @param array $themes List of themes to use to resolve themes. Defaults to {@see SSViewer::get_themes()} |
||
171 | * @return string Absolute path to resolved template file, or null if not resolved. |
||
172 | * File location will be in the format themes/<theme>/templates/<directories>/<type>/<basename>.ss |
||
173 | * Note that type (e.g. 'Layout') is not the root level directory under 'templates'. |
||
174 | */ |
||
175 | public function findTemplate($template, $themes = null) |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Resolve themed CSS path |
||
234 | * |
||
235 | * @param string $name Name of CSS file without extension |
||
236 | * @param array $themes List of themes, Defaults to {@see SSViewer::get_themes()} |
||
237 | * @return string Path to resolved CSS file (relative to base dir) |
||
238 | */ |
||
239 | public function findThemedCSS($name, $themes = null) |
||
240 | { |
||
241 | if ($themes === null) { |
||
242 | $themes = SSViewer::get_themes(); |
||
243 | } |
||
244 | |||
245 | if (substr($name, -4) !== '.css') { |
||
246 | $name .= '.css'; |
||
247 | } |
||
248 | |||
249 | $filename = $this->findThemedResource("css/$name", $themes); |
||
250 | if ($filename === null) { |
||
251 | $filename = $this->findThemedResource($name, $themes); |
||
252 | } |
||
253 | |||
254 | return $filename; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Resolve themed javascript path |
||
259 | * |
||
260 | * A javascript file in the current theme path name 'themename/javascript/$name.js' is first searched for, |
||
261 | * and it that doesn't exist and the module parameter is set then a javascript file with that name in |
||
262 | * the module is used. |
||
263 | * |
||
264 | * @param string $name The name of the file - eg '/js/File.js' would have the name 'File' |
||
265 | * @param array $themes List of themes, Defaults to {@see SSViewer::get_themes()} |
||
266 | * @return string Path to resolved javascript file (relative to base dir) |
||
267 | */ |
||
268 | public function findThemedJavascript($name, $themes = null) |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * Resolve a themed resource |
||
288 | * |
||
289 | * A themed resource and be any file that resides in a theme folder. |
||
290 | * |
||
291 | * @param string $resource A file path relative to the root folder of a theme |
||
292 | * @param array $themes An order listed of themes to search, Defaults to {@see SSViewer::get_themes()} |
||
293 | * @return string |
||
294 | */ |
||
295 | public function findThemedResource($resource, $themes = null) |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * Resolve all themes to the list of root folders relative to site root |
||
317 | * |
||
318 | * @param array $themes List of themes to resolve. Supports named theme sets. Defaults to {@see SSViewer::get_themes()}. |
||
319 | * @return array List of root-relative folders in order of precendence. |
||
320 | */ |
||
321 | public function getThemePaths($themes = null) |
||
341 |