Total Complexity | 72 |
Total Lines | 548 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Loader 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 Loader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class Loader implements AutoloadInterface |
||
32 | { |
||
33 | /** |
||
34 | * Loader::$publicDirs |
||
35 | * |
||
36 | * Loader Public Directories. |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $publicDirs = [ |
||
41 | PATH_PUBLIC, |
||
42 | ]; |
||
43 | |||
44 | /** |
||
45 | * Loader::$resourcesDirs |
||
46 | * |
||
47 | * Loader Resources Directories. |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $resourcesDirs = [ |
||
52 | PATH_RESOURCES, |
||
53 | ]; |
||
54 | |||
55 | /** |
||
56 | * Loader::$namespaceDirs |
||
57 | * |
||
58 | * Loader Namespaces Directories. |
||
59 | * |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $namespaceDirs = []; |
||
63 | |||
64 | /** |
||
65 | * Loader::$namespaceDirsMap |
||
66 | * |
||
67 | * Loader Namespaces Directories Maps. |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $namespaceDirsMap = []; |
||
72 | |||
73 | /** |
||
74 | * Loader::$loadedHelpers |
||
75 | * |
||
76 | * Loader Loaded Helpers Registry. |
||
77 | * |
||
78 | * @var array |
||
79 | */ |
||
80 | protected $loadedHelpers = []; |
||
81 | |||
82 | // ------------------------------------------------------------------------ |
||
83 | |||
84 | /** |
||
85 | * Loader::__construct |
||
86 | */ |
||
87 | public function __construct() |
||
88 | { |
||
89 | $this->register(); |
||
90 | |||
91 | // Add Kernel Namespace |
||
92 | $this->addNamespace('O2System\Kernel', PATH_KERNEL); |
||
93 | |||
94 | if (class_exists('O2System', false)) { |
||
95 | $this->addNamespace('O2System\Framework', PATH_FRAMEWORK); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | // ------------------------------------------------------------------------ |
||
100 | |||
101 | /** |
||
102 | * Register loader with SPL autoloader stack. |
||
103 | * |
||
104 | * @return void |
||
105 | */ |
||
106 | public function register() |
||
107 | { |
||
108 | // Prepend the PSR4 autoloader for maximum performance. |
||
109 | spl_autoload_register([&$this, 'loadClass'], true, true); |
||
110 | |||
111 | // Append the custom modular PSR4 autoloader. |
||
112 | spl_autoload_register([&$this, 'loadModuleClass'], true, false); |
||
113 | } |
||
114 | |||
115 | // ------------------------------------------------------------------------ |
||
116 | |||
117 | /** |
||
118 | * Adds a base directory for a namespace prefix. |
||
119 | * |
||
120 | * @param string $namespace The namespace prefix. |
||
121 | * @param string $baseDirectory A base directory for class files in the |
||
122 | * namespace. |
||
123 | * @param bool $prepend If true, prepend the base directory to the stack |
||
124 | * instead of appending it; this causes it to be searched first rather |
||
125 | * than last. |
||
126 | * |
||
127 | * @return void |
||
128 | */ |
||
129 | public function addNamespace($namespace, $baseDirectory, $prepend = false) |
||
130 | { |
||
131 | // normalize namespace prefix |
||
132 | $namespace = trim($namespace, '\\') . '\\'; |
||
133 | |||
134 | if (empty($namespace) OR $namespace === '\\') { |
||
135 | return; |
||
136 | } |
||
137 | |||
138 | // normalize the base directory with a trailing separator |
||
139 | $baseDirectory = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $baseDirectory); |
||
140 | $baseDirectory = rtrim($baseDirectory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
||
141 | |||
142 | if (is_dir($baseDirectory)) { |
||
143 | // initialize the namespace prefix array |
||
144 | if (isset($this->namespaceDirs[ $namespace ]) === false) { |
||
145 | $this->namespaceDirs[ $namespace ] = []; |
||
146 | } |
||
147 | |||
148 | // retain the base directory for the namespace prefix |
||
149 | if ( ! in_array($baseDirectory, $this->namespaceDirs[ $namespace ])) { |
||
150 | if ($prepend) { |
||
151 | array_unshift($this->namespaceDirs[ $namespace ], $baseDirectory); |
||
152 | } else { |
||
153 | array_push($this->namespaceDirs[ $namespace ], $baseDirectory); |
||
154 | } |
||
155 | } |
||
156 | |||
157 | $this->namespaceDirsMap[ $baseDirectory ] = $namespace; |
||
158 | |||
159 | // Register Namespace Language |
||
160 | language()->addFilePath($baseDirectory); |
||
161 | |||
162 | // Register Namespace Output FilePath |
||
163 | output()->addFilePath($baseDirectory); |
||
164 | |||
165 | // Register Namespace Views FilePath |
||
166 | if (services()->has('view')) { |
||
|
|||
167 | view()->addFilePath($baseDirectory); |
||
168 | } |
||
169 | |||
170 | // Autoload Composer |
||
171 | if (is_file($baseDirectory . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php')) { |
||
172 | require($baseDirectory . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'); |
||
173 | } |
||
174 | } |
||
175 | } |
||
176 | |||
177 | // ------------------------------------------------------------------------ |
||
178 | |||
179 | /** |
||
180 | * Loader::addPublicDir |
||
181 | * |
||
182 | * Adds a public directory for assets. |
||
183 | * |
||
184 | * @param string $publicDir |
||
185 | */ |
||
186 | public function addPublicDir($publicDir, $offset = null) |
||
187 | { |
||
188 | // normalize the public directory with a trailing separator |
||
189 | $publicDir = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $publicDir); |
||
190 | $publicDir = PATH_PUBLIC . str_replace(PATH_PUBLIC, '', $publicDir); |
||
191 | $publicDir = rtrim($publicDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
||
192 | |||
193 | if (is_dir($publicDir) and ! in_array($publicDir, $this->publicDirs)) { |
||
194 | if (isset($offset)) { |
||
195 | $this->publicDirs[ $offset ] = $publicDir; |
||
196 | } else { |
||
197 | $this->publicDirs[] = $publicDir; |
||
198 | } |
||
199 | } |
||
200 | } |
||
201 | |||
202 | // ------------------------------------------------------------------------ |
||
203 | |||
204 | /** |
||
205 | * Loader::addResourceDir |
||
206 | * |
||
207 | * Adds a resource directory for assets. |
||
208 | * |
||
209 | * @param string $publicDir |
||
210 | */ |
||
211 | public function addResourceDir($resourcesDir, $offset = null) |
||
212 | { |
||
213 | // normalize the public directory with a trailing separator |
||
214 | $resourcesDir = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $resourcesDir); |
||
215 | $resourcesDir = PATH_PUBLIC . str_replace(PATH_PUBLIC, '', $resourcesDir); |
||
216 | $resourcesDir = rtrim($resourcesDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
||
217 | |||
218 | if (is_dir($resourcesDir) and ! in_array($resourcesDir, $this->resourcesDirs)) { |
||
219 | if (isset($offset)) { |
||
220 | $this->resourcesDirs[ $offset ] = $resourcesDir; |
||
221 | } else { |
||
222 | $this->resourcesDirs[] = $resourcesDir; |
||
223 | } |
||
224 | } |
||
225 | } |
||
226 | |||
227 | // ------------------------------------------------------------------------ |
||
228 | |||
229 | /** |
||
230 | * Loader::getPublicDirs |
||
231 | * |
||
232 | * Gets all public directories |
||
233 | * |
||
234 | * @param bool $reverse |
||
235 | * |
||
236 | * @return array |
||
237 | */ |
||
238 | public function getPublicDirs($reverse = false) |
||
239 | { |
||
240 | return $reverse === true ? array_reverse($this->publicDirs) : $this->publicDirs; |
||
241 | } |
||
242 | |||
243 | // ------------------------------------------------------------------------ |
||
244 | |||
245 | /** |
||
246 | * Loader::getResourcesDirs |
||
247 | * |
||
248 | * Gets all resources directories |
||
249 | * |
||
250 | * @param bool $reverse |
||
251 | * |
||
252 | * @return array |
||
253 | */ |
||
254 | public function getResourcesDirs($reverse = false) |
||
255 | { |
||
256 | return $reverse === true ? array_reverse($this->resourcesDirs) : $this->resourcesDirs; |
||
257 | } |
||
258 | |||
259 | // ------------------------------------------------------------------------ |
||
260 | |||
261 | /** |
||
262 | * Get Namespace |
||
263 | * |
||
264 | * Get PSR4 Directory base on directory path |
||
265 | * |
||
266 | * @param string $dir |
||
267 | * |
||
268 | * @return string|bool |
||
269 | */ |
||
270 | public function getDirNamespace($dir) |
||
271 | { |
||
272 | $dir = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $dir); |
||
273 | |||
274 | $dir = realpath($dir); |
||
275 | $dir = rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
||
276 | |||
277 | if (array_key_exists($dir, $this->namespaceDirsMap)) { |
||
278 | return $this->namespaceDirsMap[ $dir ]; |
||
279 | } |
||
280 | |||
281 | return false; |
||
282 | } |
||
283 | |||
284 | // ------------------------------------------------------------------------ |
||
285 | |||
286 | /** |
||
287 | * Get Namespace Class Directory |
||
288 | * |
||
289 | * @param string $className |
||
290 | * |
||
291 | * @return string|null |
||
292 | */ |
||
293 | public function getClassNamespaceDirs($className) |
||
294 | { |
||
295 | $className = ltrim($className, '\\'); |
||
296 | $namespace = null; |
||
297 | |||
298 | if ($lastNsPos = strripos($className, '\\')) { |
||
299 | return $this->getNamespaceDirs(substr($className, 0, $lastNsPos)); |
||
300 | } |
||
301 | |||
302 | return false; |
||
303 | } |
||
304 | |||
305 | // ------------------------------------------------------------------------ |
||
306 | |||
307 | /** |
||
308 | * Get Namespace Directory |
||
309 | * |
||
310 | * @param string $namespace |
||
311 | * |
||
312 | * @return string |
||
313 | */ |
||
314 | public function getNamespaceDirs($namespace) |
||
315 | { |
||
316 | $namespace = trim($namespace, '\\') . '\\'; |
||
317 | |||
318 | if (array_key_exists($namespace, $this->namespaceDirs)) { |
||
319 | return $this->namespaceDirs[ $namespace ]; |
||
320 | } |
||
321 | |||
322 | return false; |
||
323 | } |
||
324 | |||
325 | // ------------------------------------------------------------------------ |
||
326 | |||
327 | public function loadHelpers(array $helpers) |
||
328 | { |
||
329 | foreach ($helpers as $helper) { |
||
330 | $this->loadHelper($helper); |
||
331 | } |
||
332 | } |
||
333 | |||
334 | public function loadHelper($helper) |
||
335 | { |
||
336 | if (array_key_exists($helper, $this->loadedHelpers)) { |
||
337 | |||
338 | return; |
||
339 | } |
||
340 | |||
341 | if ($this->requireFile($helper)) { |
||
342 | $this->loadedHelpers[ pathinfo($helper, PATHINFO_FILENAME) ][] = $helper; |
||
343 | |||
344 | return; |
||
345 | } |
||
346 | |||
347 | $helperDirectories = [ |
||
348 | PATH_KERNEL . 'Helpers' . DIRECTORY_SEPARATOR, |
||
349 | PATH_FRAMEWORK . 'Helpers' . DIRECTORY_SEPARATOR, |
||
350 | PATH_APP . 'Helpers' . DIRECTORY_SEPARATOR, |
||
351 | ]; |
||
352 | |||
353 | if (method_exists(modules(), 'current')) { |
||
354 | array_push($helperDirectories, modules()->current()->getPath() . 'Helpers' . DIRECTORY_SEPARATOR); |
||
355 | } |
||
356 | |||
357 | if ( ! array_key_exists($helper, $this->loadedHelpers)) { |
||
358 | $this->loadedHelpers[ $helper ] = []; |
||
359 | } |
||
360 | |||
361 | foreach ($helperDirectories as $helperDirectory) { |
||
362 | |||
363 | $helperFilePath = $helperDirectory . studlycase($helper) . '.php'; |
||
364 | |||
365 | if (in_array($helperFilePath, $this->loadedHelpers[ $helper ])) { |
||
366 | continue; |
||
367 | } elseif ($this->requireFile($helperFilePath)) { |
||
368 | $this->loadedHelpers[ $helper ][] = $helperFilePath; |
||
369 | } |
||
370 | } |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * If a file exists, require it from the file system. |
||
375 | * |
||
376 | * @param string $file The file to require. |
||
377 | * |
||
378 | * @return bool True if the file exists, false if not. |
||
379 | */ |
||
380 | public function requireFile($file) |
||
381 | { |
||
382 | if (is_file($file)) { |
||
383 | require_once $file; |
||
384 | |||
385 | return true; |
||
386 | } |
||
387 | |||
388 | return false; |
||
389 | } |
||
390 | |||
391 | // ------------------------------------------------------------------------ |
||
392 | |||
393 | public function loadModuleClass($class) |
||
394 | { |
||
395 | if (modules() instanceof Modules) { |
||
396 | if (false !== ($modules = modules()->getRegistry())) { |
||
397 | foreach ($modules as $module) { |
||
398 | if ($module instanceof Module) { |
||
399 | if (empty($this->namespaceDirs[ $module->getNamespace() ])) { |
||
400 | $this->addNamespace($module->getNamespace(), $module->getRealPath()); |
||
401 | } |
||
402 | } |
||
403 | } |
||
404 | } |
||
405 | } |
||
406 | |||
407 | return $this->loadClass($class); |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * Loads the class file for a given class name. |
||
412 | * |
||
413 | * @param string $class The fully-qualified class name. |
||
414 | * |
||
415 | * @return mixed The mapped file name on success, or boolean false on |
||
416 | * failure. |
||
417 | */ |
||
418 | public function loadClass($class) |
||
419 | { |
||
420 | // the current namespace prefix |
||
421 | $namespace = $class; |
||
422 | |||
423 | // work backwards through the namespace names of the fully-qualified |
||
424 | // class name to find a mapped file name |
||
425 | while (false !== $pos = strrpos($namespace, '\\')) { |
||
426 | // retain the trailing namespace separator in the prefix |
||
427 | $namespace = substr($class, 0, $pos + 1); |
||
428 | |||
429 | // the rest is the relative class name |
||
430 | $relativeClass = substr($class, $pos + 1); |
||
431 | |||
432 | // try to load a mapped file for the prefix and relative class |
||
433 | $mappedFile = $this->loadMappedFile($namespace, $relativeClass); |
||
434 | if ($mappedFile) { |
||
435 | return $mappedFile; |
||
436 | } |
||
437 | |||
438 | // remove the trailing namespace separator for the next iteration |
||
439 | // of strrpos() |
||
440 | $namespace = rtrim($namespace, '\\'); |
||
441 | } |
||
442 | |||
443 | // never found a mapped file |
||
444 | return false; |
||
445 | } |
||
446 | |||
447 | // ------------------------------------------------------------------------ |
||
448 | |||
449 | /** |
||
450 | * Load the mapped file for a namespace prefix and relative class. |
||
451 | * |
||
452 | * @param string $namespace The namespace prefix. |
||
453 | * @param string $relativeClass The relative class name. |
||
454 | * |
||
455 | * @return mixed Boolean false if no mapped file can be loaded, or the |
||
456 | * name of the mapped file that was loaded. |
||
457 | */ |
||
458 | public function loadMappedFile($namespace, $relativeClass) |
||
459 | { |
||
460 | // are there any base directories for this namespace prefix? |
||
461 | if (isset($this->namespaceDirs[ $namespace ]) === false) { |
||
462 | return false; |
||
463 | } |
||
464 | |||
465 | // look through base directories for this namespace prefix |
||
466 | foreach ($this->namespaceDirs[ $namespace ] as $namespaceDirectory) { |
||
467 | |||
468 | // replace the namespace prefix with the base directory, |
||
469 | // replace namespace separators with directory separators |
||
470 | // in the relative class name, append with .php |
||
471 | $file = $namespaceDirectory |
||
472 | . str_replace('\\', '/', $relativeClass) |
||
473 | . '.php'; |
||
474 | |||
475 | // if the mapped file exists, require it |
||
476 | if ($this->requireFile($file)) { |
||
477 | // yes, we're done |
||
478 | return $file; |
||
479 | } |
||
480 | } |
||
481 | |||
482 | // never found it |
||
483 | return false; |
||
484 | } |
||
485 | |||
486 | // ------------------------------------------------------------------------ |
||
487 | |||
488 | public function config($config) |
||
489 | { |
||
490 | return config()->loadFile($config, true); |
||
491 | } |
||
492 | |||
493 | public function helper($helper) |
||
494 | { |
||
495 | $this->loadHelper($helper); |
||
496 | } |
||
497 | |||
498 | // ------------------------------------------------------------------------ |
||
499 | |||
500 | public function helpers(array $helpers) |
||
501 | { |
||
502 | $this->loadHelpers($helpers); |
||
503 | } |
||
504 | |||
505 | // ------------------------------------------------------------------------ |
||
506 | |||
507 | public function library($class, $name = null) |
||
508 | { |
||
509 | $this->service($class, $name); |
||
510 | } |
||
511 | |||
512 | // ------------------------------------------------------------------------ |
||
513 | |||
514 | public function libraries(array $classes) |
||
515 | { |
||
516 | $this->services($classes); |
||
517 | } |
||
518 | |||
519 | // ------------------------------------------------------------------------ |
||
520 | |||
521 | public function service($class, $name = null) |
||
522 | { |
||
523 | services()->load($class, $name); |
||
524 | } |
||
525 | |||
526 | // ------------------------------------------------------------------------ |
||
527 | |||
528 | public function services(array $classes) |
||
529 | { |
||
530 | foreach ($classes as $name => $class) { |
||
531 | if(is_numeric($name)) { |
||
532 | services()->load($class); |
||
533 | } elseif(is_string($name)) { |
||
534 | services()->load($class, $name); |
||
535 | } |
||
536 | } |
||
537 | } |
||
538 | |||
539 | // ------------------------------------------------------------------------ |
||
540 | |||
541 | public function model($model) |
||
542 | { |
||
543 | models()->load($model); |
||
544 | } |
||
545 | |||
546 | // ------------------------------------------------------------------------ |
||
547 | |||
548 | public function models(array $models) |
||
549 | { |
||
550 | foreach($models as $model) { |
||
551 | models()->load($model); |
||
552 | } |
||
553 | } |
||
554 | |||
555 | // ------------------------------------------------------------------------ |
||
556 | |||
557 | public function view($file, array $vars = [], $return = false) |
||
558 | { |
||
559 | return view($file, $vars, $return); |
||
560 | } |
||
561 | |||
562 | // ------------------------------------------------------------------------ |
||
563 | |||
564 | public function page($file, array $vars = [], $return = false) |
||
565 | { |
||
566 | return view()->page($file, $vars, $return); |
||
567 | } |
||
568 | |||
569 | // ------------------------------------------------------------------------ |
||
570 | |||
571 | public function modal($file, array $vars = []) |
||
574 | } |
||
575 | |||
576 | public function language($file) |
||
579 | } |
||
580 | } |