docs/api/source-system.engine.autoloader.html
| 1: | <?php |
| 2: | /** |
| 3: | * @package OpenCart |
| 4: | * |
| 5: | * @author Daniel Kerr |
| 6: | * @copyright Copyright (c) 2005 - 2017, OpenCart, Ltd. (https://www.opencart.com/) |
| 7: | * @license https://opensource.org/licenses/GPL-3.0 |
| 8: | * |
| 9: | * @see https://www.opencart.com |
| 10: | */ |
| 11: | namespace Opencart\System\Engine; |
| 12: | /** |
| 13: | * Class Autoloader |
| 14: | */ |
| 15: | class Autoloader { |
| 16: | /** |
| 17: | * @var array<string, array<string, mixed>> |
| 18: | */ |
| 19: | private array $path = []; |
| 20: | |
| 21: | /** |
| 22: | * Constructor |
| 23: | */ |
| 24: | public function __construct() { |
| 25: | spl_autoload_register(function(string $class): void { $this->load($class); }); |
| 26: | spl_autoload_extensions('.php'); |
| 27: | } |
| 28: | |
| 29: | /** |
| 30: | * Register |
| 31: | * |
| 32: | * @param string $namespace |
| 33: | * @param string $directory |
| 34: | * @param bool $psr4 |
| 35: | * |
| 36: | * @return void |
| 37: | * |
| 38: | * @psr-4 filename standard is stupid composer has lower case file structure than its packages have camelcase file names! |
| 39: | */ |
| 40: | public function register(string $namespace, string $directory, bool $psr4 = false): void { |
| 41: | $this->path[$namespace] = [ |
| 42: | 'directory' => $directory, |
| 43: | 'psr4' => $psr4 |
| 44: | ]; |
| 45: | } |
| 46: | |
| 47: | /** |
| 48: | * Load |
| 49: | * |
| 50: | * @param string $class |
| 51: | * |
| 52: | * @return bool |
| 53: | */ |
| 54: | public function load(string $class): bool { |
| 55: | $namespace = ''; |
| 56: | |
| 57: | $parts = explode('\', $class); |
| 58: | |
| 59: | foreach ($parts as $part) { |
| 60: | if (!$namespace) { |
| 61: | $namespace .= $part; |
| 62: | } else { |
| 63: | $namespace .= '\' . $part; |
| 64: | } |
| 65: | |
| 66: | if (isset($this->path[$namespace])) { |
| 67: | if (!$this->path[$namespace]['psr4']) { |
| 68: | $file = $this->path[$namespace]['directory'] . trim(str_replace('\', '/', strtolower(preg_replace('([a-z])([A-Z]|[0-9])', '\1_\2', substr($class, strlen($namespace))))), '/') . '.php'; |
| 69: | } else { |
| 70: | $file = $this->path[$namespace]['directory'] . trim(str_replace('\', '/', substr($class, strlen($namespace))), '/') . '.php'; |
| 71: | } |
| 72: | } |
| 73: | } |
| 74: | |
| 75: | if (isset($file) && is_file($file)) { |
| 76: | include_once($file); |
| 77: | |
| 78: | return true; |
| 79: | } else { |
| 80: | return false; |
| 81: | } |
| 82: | } |
| 83: | } |
| 84: | |
OpenCart API API documentation generated by ApiGen dev-master