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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
11 | class ThemeResourceLoader |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * @var ThemeResourceLoader |
||
16 | */ |
||
17 | private static $instance; |
||
18 | |||
19 | protected $base; |
||
20 | |||
21 | /** |
||
22 | * List of template "sets" that contain a test manifest, and have an alias. |
||
23 | * E.g. '$default' |
||
24 | * |
||
25 | * @var ThemeList[] |
||
26 | */ |
||
27 | protected $sets = []; |
||
28 | |||
29 | /** |
||
30 | * @return ThemeResourceLoader |
||
31 | */ |
||
32 | public static function inst() |
||
33 | { |
||
34 | return self::$instance ? self::$instance : self::$instance = new self(); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Set instance |
||
39 | * |
||
40 | * @param ThemeResourceLoader $instance |
||
41 | */ |
||
42 | public static function set_instance(ThemeResourceLoader $instance) |
||
46 | |||
47 | public function __construct($base = null) |
||
51 | |||
52 | /** |
||
53 | * Add a new theme manifest for a given identifier. E.g. '$default' |
||
54 | * |
||
55 | * @param string $set |
||
56 | * @param ThemeList $manifest |
||
57 | */ |
||
58 | public function addSet($set, ThemeList $manifest) |
||
62 | |||
63 | /** |
||
64 | * Get a named theme set |
||
65 | * |
||
66 | * @param string $set |
||
67 | * @return ThemeList |
||
68 | */ |
||
69 | public function getSet($set) |
||
76 | |||
77 | /** |
||
78 | * Given a theme identifier, determine the path from the root directory |
||
79 | * |
||
80 | * The mapping from $identifier to path follows these rules: |
||
81 | * - A simple theme name ('mytheme') which maps to the standard themes dir (/themes/mytheme) |
||
82 | * - A theme path with a leading slash ('/mymodule/themes/mytheme') which maps directly to that path. |
||
83 | * - or a vendored theme path. (vendor/mymodule:mytheme) which maps to the nested 'theme' within |
||
84 | * that module. ('/mymodule/themes/mytheme'). |
||
85 | * - A vendored module with no nested theme (vendor/mymodule) which maps to the root directory |
||
86 | * of that module. ('/mymodule'). |
||
87 | * |
||
88 | * @param string $identifier Theme identifier. |
||
89 | * @return string Path from root, not including leading or trailing forward slash. E.g. themes/mytheme |
||
90 | */ |
||
91 | public function getPath($identifier) |
||
145 | |||
146 | /** |
||
147 | * Attempts to find possible candidate templates from a set of template |
||
148 | * names from modules, current theme directory and finally the application |
||
149 | * folder. |
||
150 | * |
||
151 | * The template names can be passed in as plain strings, or be in the |
||
152 | * format "type/name", where type is the type of template to search for |
||
153 | * (e.g. Includes, Layout). |
||
154 | * |
||
155 | * @param string|array $template Template name, or template spec in array format with the keys |
||
156 | * 'type' (type string) and 'templates' (template hierarchy in order of precedence). |
||
157 | * If 'templates' is ommitted then any other item in the array will be treated as the template |
||
158 | * list, or list of templates each in the array spec given. |
||
159 | * Templates with an .ss extension will be treated as file paths, and will bypass |
||
160 | * theme-coupled resolution. |
||
161 | * @param array $themes List of themes to use to resolve themes. In most cases |
||
162 | * you should pass in {@see SSViewer::get_themes()} |
||
163 | * @return string Absolute path to resolved template file, or null if not resolved. |
||
164 | * File location will be in the format themes/<theme>/templates/<directories>/<type>/<basename>.ss |
||
165 | * Note that type (e.g. 'Layout') is not the root level directory under 'templates'. |
||
166 | */ |
||
167 | public function findTemplate($template, $themes) |
||
220 | |||
221 | /** |
||
222 | * Resolve themed CSS path |
||
223 | * |
||
224 | * @param string $name Name of CSS file without extension |
||
225 | * @param array $themes List of themes |
||
226 | * @return string Path to resolved CSS file (relative to base dir) |
||
227 | */ |
||
228 | public function findThemedCSS($name, $themes) |
||
241 | |||
242 | /** |
||
243 | * Resolve themed javascript path |
||
244 | * |
||
245 | * A javascript file in the current theme path name 'themename/javascript/$name.js' is first searched for, |
||
246 | * and it that doesn't exist and the module parameter is set then a javascript file with that name in |
||
247 | * the module is used. |
||
248 | * |
||
249 | * @param string $name The name of the file - eg '/js/File.js' would have the name 'File' |
||
250 | * @param array $themes List of themes |
||
251 | * @return string Path to resolved javascript file (relative to base dir) |
||
252 | */ |
||
253 | public function findThemedJavascript($name, $themes) |
||
266 | |||
267 | /** |
||
268 | * Resolve a themed resource |
||
269 | * |
||
270 | * A themed resource and be any file that resides in a theme folder. |
||
271 | * |
||
272 | * @param string $resource A file path relative to the root folder of a theme |
||
273 | * @param array $themes An order listed of themes to search |
||
274 | */ |
||
275 | public function findThemedResource($resource, $themes) |
||
293 | |||
294 | /** |
||
295 | * Resolve all themes to the list of root folders relative to site root |
||
296 | * |
||
297 | * @param array $themes List of themes to resolve. Supports named theme sets. |
||
298 | * @return array List of root-relative folders in order of precendence. |
||
299 | */ |
||
300 | public function getThemePaths($themes) |
||
315 | } |
||
316 |