1 | <?php |
||
13 | class Autoloader |
||
14 | { |
||
15 | /** |
||
16 | * An associative array where the key is a namespace prefix and the value |
||
17 | * is an array of base directories for classes in that namespace. |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $prefixes = []; |
||
22 | |||
23 | /** |
||
24 | * Register loader with SPL autoloader stack. |
||
25 | * |
||
26 | * @codeCoverageIgnore |
||
27 | */ |
||
28 | public function register() |
||
32 | |||
33 | /** |
||
34 | * Adds a base directory for a namespace prefix. |
||
35 | * |
||
36 | * @param string $prefix The namespace prefix. |
||
37 | * @param string $baseDir A base directory for class files in the |
||
38 | * namespace. |
||
39 | */ |
||
40 | 5 | public function addNamespace(string $prefix, string $baseDir) |
|
51 | |||
52 | /** |
||
53 | * Loads the class file for a given class name. |
||
54 | * |
||
55 | * @param string $class The fully-qualified class name. |
||
56 | * @return string The mapped file name on success, or emtpy string on |
||
57 | * failure. |
||
58 | */ |
||
59 | 5 | public function loadClass($class): string |
|
77 | |||
78 | /** |
||
79 | * Load the mapped file for a namespace prefix and relative class. |
||
80 | * |
||
81 | * @param string $prefix The namespace prefix. |
||
82 | * @param string $relativeClass The relative class name. |
||
83 | * |
||
84 | * @return string Empty string if no mapped file can be |
||
85 | * loaded, or the name of the mapped file that was loaded. |
||
86 | */ |
||
87 | 5 | protected function loadMappedFile($prefix, $relativeClass) |
|
88 | { |
||
89 | 5 | if (false === isset($this->prefixes[$prefix])) { |
|
90 | 2 | return ''; |
|
91 | } |
||
92 | |||
93 | 4 | $relativeFile = $this->getRelativeFile($relativeClass); |
|
94 | |||
95 | // Look through base directories for this namespace prefix. |
||
96 | 4 | foreach ($this->prefixes[$prefix] as $baseDir) { |
|
97 | 4 | $file = $baseDir . $relativeFile; |
|
98 | |||
99 | // If the mapped file exists, require it. |
||
100 | 4 | if ($this->requireFile($file)) { |
|
101 | 4 | return $file; |
|
102 | } |
||
103 | } |
||
104 | |||
105 | 1 | return ''; |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * Normalize path using WordPress naming convertions. |
||
110 | * |
||
111 | * @param string $path Relative class path. |
||
112 | * @return string |
||
113 | */ |
||
114 | 4 | protected function normalizePath(string $path): string |
|
118 | |||
119 | /** |
||
120 | * Get relative file path. |
||
121 | * |
||
122 | * @param string $relativeClass Relative class. |
||
123 | * @return string |
||
124 | */ |
||
125 | 4 | protected function getRelativeFile(string $relativeClass): string |
|
138 | |||
139 | /** |
||
140 | * If a file exists, require it from the file system. |
||
141 | * |
||
142 | * @codeCoverageIgnore |
||
143 | * |
||
144 | * @param string $file The file to require. |
||
145 | * @return bool True if the file exists, false if not. |
||
146 | */ |
||
147 | protected function requireFile(string $file): bool |
||
155 | } |
||
156 |