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