Complex classes like Bootstrap 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 Bootstrap, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Bootstrap |
||
17 | { |
||
18 | private $manifests = array(); |
||
19 | private $extensions = array(); |
||
20 | private $extensionsDone = array(); |
||
21 | private $dependencies = array(); |
||
22 | private static $includePaths = array(); |
||
23 | private static $autoloader = false; |
||
24 | |||
25 | |||
26 | /** |
||
27 | * Initialises the object. |
||
28 | * |
||
29 | * @param array $extdirs List of directories to look for manifest files (or sub-directories thereof) |
||
30 | * @param boolean $defaultdir If default extension directory should be included automatically |
||
31 | * @param string|null $basedir Aimeos core path (optional, __DIR__ if null) |
||
32 | */ |
||
33 | public function __construct( array $extdirs = array(), $defaultdir = true, $basedir = null ) |
||
34 | { |
||
35 | if( $basedir === null ) { |
||
36 | $basedir = __DIR__; |
||
37 | } |
||
38 | |||
39 | if( $defaultdir === true && is_dir( $basedir . DIRECTORY_SEPARATOR . 'ext' ) === true ) { |
||
40 | $extdirs[] = realpath( $basedir . DIRECTORY_SEPARATOR . 'ext' ); |
||
41 | } |
||
42 | |||
43 | $this->manifests[$basedir] = $this->getManifestFile( $basedir ); |
||
44 | |||
45 | self::$includePaths = $this->getIncludePaths(); |
||
46 | $this->registerAutoloader(); |
||
47 | $this->addDependencies( $extdirs ); |
||
48 | $this->addManifests( $this->dependencies ); |
||
49 | self::$includePaths = $this->getIncludePaths(); |
||
50 | } |
||
51 | |||
52 | |||
53 | /** |
||
54 | * Loads the class files for a given class name. |
||
55 | * |
||
56 | * @param string $className Name of the class |
||
57 | * @return boolean True if file was found, false if not |
||
58 | */ |
||
59 | public static function autoload( $className ) |
||
60 | { |
||
61 | $fileName = strtr( ltrim( $className, '\\' ), '\\_', DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR ) . '.php'; |
||
62 | |||
63 | if( strncmp( $fileName, 'Aimeos' . DIRECTORY_SEPARATOR, 7 ) === 0 ) { |
||
64 | $fileName = substr( $fileName, 7 ); |
||
65 | } |
||
66 | |||
67 | foreach( self::$includePaths as $path ) |
||
68 | { |
||
69 | $file = $path . DIRECTORY_SEPARATOR . $fileName; |
||
70 | |||
71 | if( file_exists( $file ) === true && ( include_once $file ) !== false ) { |
||
72 | return true; |
||
73 | } |
||
74 | } |
||
75 | |||
76 | foreach( explode( PATH_SEPARATOR, get_include_path() ) as $path ) |
||
77 | { |
||
78 | $file = $path . DIRECTORY_SEPARATOR . $fileName; |
||
79 | |||
80 | if( file_exists( $file ) === true && ( include_once $file ) !== false ) { |
||
81 | return true; |
||
82 | } |
||
83 | } |
||
84 | |||
85 | return false; |
||
86 | } |
||
87 | |||
88 | |||
89 | /** |
||
90 | * Returns the list of paths for each domain where the translation files are located. |
||
91 | * |
||
92 | * @return array Associative list of i18n domains and lists of absolute paths to the translation directories |
||
93 | */ |
||
94 | public function getI18nPaths() |
||
111 | |||
112 | |||
113 | /** |
||
114 | * Returns the include paths containing the required class files. |
||
115 | * |
||
116 | * @return array List of include paths |
||
117 | */ |
||
118 | public function getIncludePaths() |
||
135 | |||
136 | |||
137 | /** |
||
138 | * Returns the paths containing the required configuration files. |
||
139 | * |
||
140 | * @return string[] List of configuration paths |
||
141 | */ |
||
142 | public function getConfigPaths() |
||
159 | |||
160 | |||
161 | /** |
||
162 | * Returns the paths stored in the manifest file for the given custom section. |
||
163 | * |
||
164 | * @param string $section Name of the section like in the manifest file |
||
165 | * @return array List of paths |
||
166 | */ |
||
167 | public function getCustomPaths( $section ) |
||
180 | |||
181 | |||
182 | /** |
||
183 | * Returns the available extensions |
||
184 | * |
||
185 | * @return array List of available extension names |
||
186 | */ |
||
187 | public function getExtensions() |
||
202 | |||
203 | |||
204 | /** |
||
205 | * Returns the list of paths where setup tasks are stored. |
||
206 | * |
||
207 | * @param string $site Name of the site like "default", "unitperf" and "unittest" |
||
208 | * @return array List of setup paths |
||
209 | */ |
||
210 | public function getSetupPaths( $site ) |
||
234 | |||
235 | |||
236 | /** |
||
237 | * Returns the language IDs for the available translations |
||
238 | * |
||
239 | * @param string $section Section name in the i18n paths |
||
240 | * @return array List of ISO language codes |
||
241 | */ |
||
242 | public function getI18nList( $section ) |
||
265 | |||
266 | |||
267 | /** |
||
268 | * Returns the configurations of the manifest files in the given directories. |
||
269 | * |
||
270 | * @param array $directories List of directories where the manifest files are stored |
||
271 | * @return array Associative list of directory / configuration array pairs |
||
272 | */ |
||
273 | protected function getManifests( array $directories ) |
||
303 | |||
304 | |||
305 | /** |
||
306 | * Loads the manifest file from the given directory. |
||
307 | * |
||
308 | * @param string $dir Directory that includes the manifest file |
||
309 | * @return array|false Associative list of configurations or false if the file doesn't exist |
||
310 | */ |
||
311 | protected function getManifestFile( $dir ) |
||
321 | |||
322 | |||
323 | /** |
||
324 | * Registers the Aimeos autoloader. |
||
325 | */ |
||
326 | protected function registerAutoloader() |
||
340 | |||
341 | |||
342 | /** |
||
343 | * Adds the dependencies from the extensions |
||
344 | * |
||
345 | * @param array $extdirs List of extension directories |
||
346 | * @throws \Exception If dependencies are incorrectly configured |
||
347 | */ |
||
348 | private function addDependencies( array $extdirs ) |
||
371 | |||
372 | |||
373 | /** |
||
374 | * Re-order the given dependencies of each manifest configuration. |
||
375 | * |
||
376 | * @param array $deps List of dependencies |
||
377 | * @param array $stack List of task names that are scheduled after this task |
||
378 | * @todo version checks |
||
379 | */ |
||
380 | private function addManifests( array $deps, array $stack = array( ) ) |
||
405 | } |
||
406 |