1: | <?php
|
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: |
|
11: | namespace Opencart\System\Engine;
|
12: | |
13: | |
14: |
|
15: | class Autoloader {
|
16: | |
17: | |
18: |
|
19: | private array $path = [];
|
20: |
|
21: | |
22: | |
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: | |
31: | |
32: | |
33: | |
34: | |
35: | |
36: | |
37: | |
38: | |
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: | |
49: | |
50: | |
51: | |
52: | |
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: | |