1 | <?php |
||
91 | class Psr4ClassLoader |
||
92 | { |
||
93 | /** |
||
94 | * An associative array where the key is a namespace prefix and the value |
||
95 | * is an array of base directories for classes in that namespace. |
||
96 | * |
||
97 | * @var array |
||
98 | */ |
||
99 | protected $prefixes = array(); |
||
100 | |||
101 | /** |
||
102 | * addLoader sets all basic options and registers the autoloader |
||
103 | * |
||
104 | * @param type $namespace namespace |
||
105 | * @param mixed $path path(s) to the namespace's directories |
||
106 | * Can be string - only one directory |
||
107 | * or array of strings - multiple directories |
||
108 | * |
||
109 | * @return SplClassLoader |
||
110 | */ |
||
111 | public static function addLoader($namespace, $path) |
||
125 | |||
126 | /** |
||
127 | * Register loader with SPL autoloader stack. |
||
128 | * |
||
129 | * @return null |
||
130 | */ |
||
131 | public function register() |
||
135 | |||
136 | /** |
||
137 | * Adds a base directory for a namespace prefix. |
||
138 | * |
||
139 | * @param string $prefix The namespace prefix. |
||
140 | * @param string $base_dir Base directory for class files in namespace. |
||
141 | * @param bool $prepend If true, prepend the base directory to the |
||
142 | * stack instead of appending it; this causes |
||
143 | * it to be searched first rather than last. |
||
144 | * |
||
145 | * @return null |
||
146 | */ |
||
147 | public function addNamespace($prefix, $base_dir, $prepend = false) |
||
168 | |||
169 | /** |
||
170 | * Loads the class file for a given class name. |
||
171 | * |
||
172 | * @param string $class The fully-qualified class name. |
||
173 | * |
||
174 | * @return string|false The mapped file name on success, or boolean false on |
||
175 | * failure. |
||
176 | */ |
||
177 | 8 | public function loadClass($class) |
|
178 | { |
||
179 | // the current namespace prefix |
||
180 | 8 | $prefix = $class; |
|
181 | |||
182 | // work backwards through the namespace names of the fully-qualified |
||
183 | // class name to find a mapped file name |
||
184 | 8 | while (false !== $pos = strrpos($prefix, '\\')) { |
|
185 | // retain the trailing namespace separator in the prefix |
||
186 | 4 | $prefix = substr($class, 0, $pos + 1); |
|
187 | |||
188 | // the rest is the relative class name |
||
189 | 4 | $relative_class = substr($class, $pos + 1); |
|
190 | |||
191 | // try to load a mapped file for the prefix and relative class |
||
192 | 4 | $mapped_file = $this->loadMappedFile($prefix, $relative_class); |
|
193 | 4 | if ($mapped_file !== false) { |
|
194 | return $mapped_file; |
||
195 | } |
||
196 | |||
197 | // remove the trailing namespace separator for the next iteration |
||
198 | // of strrpos() |
||
199 | 4 | $prefix = rtrim($prefix, '\\'); |
|
200 | } |
||
201 | |||
202 | // never found a mapped file |
||
203 | 8 | return false; |
|
204 | } |
||
205 | |||
206 | /** |
||
207 | * Load the mapped file for a namespace prefix and relative class. |
||
208 | * |
||
209 | * @param string $prefix The namespace prefix. |
||
210 | * @param string $relative_class The relative class name. |
||
211 | * |
||
212 | * @return false|string Boolean false if no mapped file can be loaded, or the |
||
213 | * name of the mapped file that was loaded. |
||
214 | */ |
||
215 | 4 | protected function loadMappedFile($prefix, $relative_class) |
|
216 | { |
||
217 | // are there any base directories for this namespace prefix? |
||
218 | 4 | if (isset($this->prefixes[$prefix]) === false) { |
|
219 | 4 | return false; |
|
220 | } |
||
221 | |||
222 | // look through base directories for this namespace prefix |
||
223 | foreach ($this->prefixes[$prefix] as $base_dir) { |
||
224 | // replace the namespace prefix with the base directory, |
||
225 | // replace namespace separators with directory separators |
||
226 | // in the relative class name, append with .php |
||
227 | $file = $base_dir |
||
228 | . str_replace('\\', '/', $relative_class) |
||
229 | . '.php'; |
||
230 | |||
231 | // if the mapped file exists, require it |
||
232 | if ($this->requireFile($file)) { |
||
233 | // yes, we're done |
||
234 | return $file; |
||
235 | } |
||
236 | } |
||
237 | |||
238 | // never found it |
||
239 | return false; |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * If a file exists, require it from the file system. |
||
244 | * |
||
245 | * @param string $file The file to require. |
||
246 | * |
||
247 | * @return bool True if the file exists, false if not. |
||
248 | */ |
||
249 | protected function requireFile($file) |
||
259 | } |
||
260 |