xref: /dokuwiki/vendor/composer/ClassLoader.php (revision e43cd7e11322648414daef21f777734a2cafc5c8)
1605f8e8dSAndreas Gohr<?php
2605f8e8dSAndreas Gohr
3605f8e8dSAndreas Gohr/*
4605f8e8dSAndreas Gohr * This file is part of Composer.
5605f8e8dSAndreas Gohr *
6605f8e8dSAndreas Gohr * (c) Nils Adermann <naderman@naderman.de>
7605f8e8dSAndreas Gohr *     Jordi Boggiano <j.boggiano@seld.be>
8605f8e8dSAndreas Gohr *
9605f8e8dSAndreas Gohr * For the full copyright and license information, please view the LICENSE
10605f8e8dSAndreas Gohr * file that was distributed with this source code.
11605f8e8dSAndreas Gohr */
12605f8e8dSAndreas Gohr
13605f8e8dSAndreas Gohrnamespace Composer\Autoload;
14605f8e8dSAndreas Gohr
15605f8e8dSAndreas Gohr/**
167a33d2f8SNiklas Keller * ClassLoader implements a PSR-0, PSR-4 and classmap class loader.
17605f8e8dSAndreas Gohr *
18605f8e8dSAndreas Gohr *     $loader = new \Composer\Autoload\ClassLoader();
19605f8e8dSAndreas Gohr *
20605f8e8dSAndreas Gohr *     // register classes with namespaces
21605f8e8dSAndreas Gohr *     $loader->add('Symfony\Component', __DIR__.'/component');
22605f8e8dSAndreas Gohr *     $loader->add('Symfony',           __DIR__.'/framework');
23605f8e8dSAndreas Gohr *
24605f8e8dSAndreas Gohr *     // activate the autoloader
25605f8e8dSAndreas Gohr *     $loader->register();
26605f8e8dSAndreas Gohr *
27605f8e8dSAndreas Gohr *     // to enable searching the include path (eg. for PEAR packages)
28605f8e8dSAndreas Gohr *     $loader->setUseIncludePath(true);
29605f8e8dSAndreas Gohr *
30605f8e8dSAndreas Gohr * In this example, if you try to use a class in the Symfony\Component
31605f8e8dSAndreas Gohr * namespace or one of its children (Symfony\Component\Console for instance),
32605f8e8dSAndreas Gohr * the autoloader will first look for the class under the component/
33605f8e8dSAndreas Gohr * directory, and it will then fallback to the framework/ directory if not
34605f8e8dSAndreas Gohr * found before giving up.
35605f8e8dSAndreas Gohr *
36605f8e8dSAndreas Gohr * This class is loosely based on the Symfony UniversalClassLoader.
37605f8e8dSAndreas Gohr *
38605f8e8dSAndreas Gohr * @author Fabien Potencier <fabien@symfony.com>
39605f8e8dSAndreas Gohr * @author Jordi Boggiano <j.boggiano@seld.be>
407a33d2f8SNiklas Keller * @see    http://www.php-fig.org/psr/psr-0/
417a33d2f8SNiklas Keller * @see    http://www.php-fig.org/psr/psr-4/
42605f8e8dSAndreas Gohr */
43605f8e8dSAndreas Gohrclass ClassLoader
44605f8e8dSAndreas Gohr{
45605f8e8dSAndreas Gohr    // PSR-4
46605f8e8dSAndreas Gohr    private $prefixLengthsPsr4 = array();
47605f8e8dSAndreas Gohr    private $prefixDirsPsr4 = array();
48605f8e8dSAndreas Gohr    private $fallbackDirsPsr4 = array();
49605f8e8dSAndreas Gohr
50605f8e8dSAndreas Gohr    // PSR-0
51605f8e8dSAndreas Gohr    private $prefixesPsr0 = array();
52605f8e8dSAndreas Gohr    private $fallbackDirsPsr0 = array();
53605f8e8dSAndreas Gohr
54605f8e8dSAndreas Gohr    private $useIncludePath = false;
55605f8e8dSAndreas Gohr    private $classMap = array();
56605f8e8dSAndreas Gohr    private $classMapAuthoritative = false;
577a33d2f8SNiklas Keller    private $missingClasses = array();
58e0dd796dSAndreas Gohr    private $apcuPrefix;
59605f8e8dSAndreas Gohr
60605f8e8dSAndreas Gohr    public function getPrefixes()
61605f8e8dSAndreas Gohr    {
62605f8e8dSAndreas Gohr        if (!empty($this->prefixesPsr0)) {
63605f8e8dSAndreas Gohr            return call_user_func_array('array_merge', $this->prefixesPsr0);
64605f8e8dSAndreas Gohr        }
65605f8e8dSAndreas Gohr
66605f8e8dSAndreas Gohr        return array();
67605f8e8dSAndreas Gohr    }
68605f8e8dSAndreas Gohr
69605f8e8dSAndreas Gohr    public function getPrefixesPsr4()
70605f8e8dSAndreas Gohr    {
71605f8e8dSAndreas Gohr        return $this->prefixDirsPsr4;
72605f8e8dSAndreas Gohr    }
73605f8e8dSAndreas Gohr
74605f8e8dSAndreas Gohr    public function getFallbackDirs()
75605f8e8dSAndreas Gohr    {
76605f8e8dSAndreas Gohr        return $this->fallbackDirsPsr0;
77605f8e8dSAndreas Gohr    }
78605f8e8dSAndreas Gohr
79605f8e8dSAndreas Gohr    public function getFallbackDirsPsr4()
80605f8e8dSAndreas Gohr    {
81605f8e8dSAndreas Gohr        return $this->fallbackDirsPsr4;
82605f8e8dSAndreas Gohr    }
83605f8e8dSAndreas Gohr
84605f8e8dSAndreas Gohr    public function getClassMap()
85605f8e8dSAndreas Gohr    {
86605f8e8dSAndreas Gohr        return $this->classMap;
87605f8e8dSAndreas Gohr    }
88605f8e8dSAndreas Gohr
89605f8e8dSAndreas Gohr    /**
90605f8e8dSAndreas Gohr     * @param array $classMap Class to filename map
91605f8e8dSAndreas Gohr     */
92605f8e8dSAndreas Gohr    public function addClassMap(array $classMap)
93605f8e8dSAndreas Gohr    {
94605f8e8dSAndreas Gohr        if ($this->classMap) {
95605f8e8dSAndreas Gohr            $this->classMap = array_merge($this->classMap, $classMap);
96605f8e8dSAndreas Gohr        } else {
97605f8e8dSAndreas Gohr            $this->classMap = $classMap;
98605f8e8dSAndreas Gohr        }
99605f8e8dSAndreas Gohr    }
100605f8e8dSAndreas Gohr
101605f8e8dSAndreas Gohr    /**
102605f8e8dSAndreas Gohr     * Registers a set of PSR-0 directories for a given prefix, either
103605f8e8dSAndreas Gohr     * appending or prepending to the ones previously set for this prefix.
104605f8e8dSAndreas Gohr     *
105605f8e8dSAndreas Gohr     * @param string       $prefix  The prefix
106605f8e8dSAndreas Gohr     * @param array|string $paths   The PSR-0 root directories
107605f8e8dSAndreas Gohr     * @param bool         $prepend Whether to prepend the directories
108605f8e8dSAndreas Gohr     */
109605f8e8dSAndreas Gohr    public function add($prefix, $paths, $prepend = false)
110605f8e8dSAndreas Gohr    {
111605f8e8dSAndreas Gohr        if (!$prefix) {
112605f8e8dSAndreas Gohr            if ($prepend) {
113605f8e8dSAndreas Gohr                $this->fallbackDirsPsr0 = array_merge(
114605f8e8dSAndreas Gohr                    (array) $paths,
115605f8e8dSAndreas Gohr                    $this->fallbackDirsPsr0
116605f8e8dSAndreas Gohr                );
117605f8e8dSAndreas Gohr            } else {
118605f8e8dSAndreas Gohr                $this->fallbackDirsPsr0 = array_merge(
119605f8e8dSAndreas Gohr                    $this->fallbackDirsPsr0,
120605f8e8dSAndreas Gohr                    (array) $paths
121605f8e8dSAndreas Gohr                );
122605f8e8dSAndreas Gohr            }
123605f8e8dSAndreas Gohr
124605f8e8dSAndreas Gohr            return;
125605f8e8dSAndreas Gohr        }
126605f8e8dSAndreas Gohr
127605f8e8dSAndreas Gohr        $first = $prefix[0];
128605f8e8dSAndreas Gohr        if (!isset($this->prefixesPsr0[$first][$prefix])) {
129605f8e8dSAndreas Gohr            $this->prefixesPsr0[$first][$prefix] = (array) $paths;
130605f8e8dSAndreas Gohr
131605f8e8dSAndreas Gohr            return;
132605f8e8dSAndreas Gohr        }
133605f8e8dSAndreas Gohr        if ($prepend) {
134605f8e8dSAndreas Gohr            $this->prefixesPsr0[$first][$prefix] = array_merge(
135605f8e8dSAndreas Gohr                (array) $paths,
136605f8e8dSAndreas Gohr                $this->prefixesPsr0[$first][$prefix]
137605f8e8dSAndreas Gohr            );
138605f8e8dSAndreas Gohr        } else {
139605f8e8dSAndreas Gohr            $this->prefixesPsr0[$first][$prefix] = array_merge(
140605f8e8dSAndreas Gohr                $this->prefixesPsr0[$first][$prefix],
141605f8e8dSAndreas Gohr                (array) $paths
142605f8e8dSAndreas Gohr            );
143605f8e8dSAndreas Gohr        }
144605f8e8dSAndreas Gohr    }
145605f8e8dSAndreas Gohr
146605f8e8dSAndreas Gohr    /**
147605f8e8dSAndreas Gohr     * Registers a set of PSR-4 directories for a given namespace, either
148605f8e8dSAndreas Gohr     * appending or prepending to the ones previously set for this namespace.
149605f8e8dSAndreas Gohr     *
150605f8e8dSAndreas Gohr     * @param string       $prefix  The prefix/namespace, with trailing '\\'
1517a33d2f8SNiklas Keller     * @param array|string $paths   The PSR-4 base directories
152605f8e8dSAndreas Gohr     * @param bool         $prepend Whether to prepend the directories
153605f8e8dSAndreas Gohr     *
154605f8e8dSAndreas Gohr     * @throws \InvalidArgumentException
155605f8e8dSAndreas Gohr     */
156605f8e8dSAndreas Gohr    public function addPsr4($prefix, $paths, $prepend = false)
157605f8e8dSAndreas Gohr    {
158605f8e8dSAndreas Gohr        if (!$prefix) {
159605f8e8dSAndreas Gohr            // Register directories for the root namespace.
160605f8e8dSAndreas Gohr            if ($prepend) {
161605f8e8dSAndreas Gohr                $this->fallbackDirsPsr4 = array_merge(
162605f8e8dSAndreas Gohr                    (array) $paths,
163605f8e8dSAndreas Gohr                    $this->fallbackDirsPsr4
164605f8e8dSAndreas Gohr                );
165605f8e8dSAndreas Gohr            } else {
166605f8e8dSAndreas Gohr                $this->fallbackDirsPsr4 = array_merge(
167605f8e8dSAndreas Gohr                    $this->fallbackDirsPsr4,
168605f8e8dSAndreas Gohr                    (array) $paths
169605f8e8dSAndreas Gohr                );
170605f8e8dSAndreas Gohr            }
171605f8e8dSAndreas Gohr        } elseif (!isset($this->prefixDirsPsr4[$prefix])) {
172605f8e8dSAndreas Gohr            // Register directories for a new namespace.
173605f8e8dSAndreas Gohr            $length = strlen($prefix);
174605f8e8dSAndreas Gohr            if ('\\' !== $prefix[$length - 1]) {
175605f8e8dSAndreas Gohr                throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
176605f8e8dSAndreas Gohr            }
177605f8e8dSAndreas Gohr            $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
178605f8e8dSAndreas Gohr            $this->prefixDirsPsr4[$prefix] = (array) $paths;
179605f8e8dSAndreas Gohr        } elseif ($prepend) {
180605f8e8dSAndreas Gohr            // Prepend directories for an already registered namespace.
181605f8e8dSAndreas Gohr            $this->prefixDirsPsr4[$prefix] = array_merge(
182605f8e8dSAndreas Gohr                (array) $paths,
183605f8e8dSAndreas Gohr                $this->prefixDirsPsr4[$prefix]
184605f8e8dSAndreas Gohr            );
185605f8e8dSAndreas Gohr        } else {
186605f8e8dSAndreas Gohr            // Append directories for an already registered namespace.
187605f8e8dSAndreas Gohr            $this->prefixDirsPsr4[$prefix] = array_merge(
188605f8e8dSAndreas Gohr                $this->prefixDirsPsr4[$prefix],
189605f8e8dSAndreas Gohr                (array) $paths
190605f8e8dSAndreas Gohr            );
191605f8e8dSAndreas Gohr        }
192605f8e8dSAndreas Gohr    }
193605f8e8dSAndreas Gohr
194605f8e8dSAndreas Gohr    /**
195605f8e8dSAndreas Gohr     * Registers a set of PSR-0 directories for a given prefix,
196605f8e8dSAndreas Gohr     * replacing any others previously set for this prefix.
197605f8e8dSAndreas Gohr     *
198605f8e8dSAndreas Gohr     * @param string       $prefix The prefix
199605f8e8dSAndreas Gohr     * @param array|string $paths  The PSR-0 base directories
200605f8e8dSAndreas Gohr     */
201605f8e8dSAndreas Gohr    public function set($prefix, $paths)
202605f8e8dSAndreas Gohr    {
203605f8e8dSAndreas Gohr        if (!$prefix) {
204605f8e8dSAndreas Gohr            $this->fallbackDirsPsr0 = (array) $paths;
205605f8e8dSAndreas Gohr        } else {
206605f8e8dSAndreas Gohr            $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths;
207605f8e8dSAndreas Gohr        }
208605f8e8dSAndreas Gohr    }
209605f8e8dSAndreas Gohr
210605f8e8dSAndreas Gohr    /**
211605f8e8dSAndreas Gohr     * Registers a set of PSR-4 directories for a given namespace,
212605f8e8dSAndreas Gohr     * replacing any others previously set for this namespace.
213605f8e8dSAndreas Gohr     *
214605f8e8dSAndreas Gohr     * @param string       $prefix The prefix/namespace, with trailing '\\'
215605f8e8dSAndreas Gohr     * @param array|string $paths  The PSR-4 base directories
216605f8e8dSAndreas Gohr     *
217605f8e8dSAndreas Gohr     * @throws \InvalidArgumentException
218605f8e8dSAndreas Gohr     */
219605f8e8dSAndreas Gohr    public function setPsr4($prefix, $paths)
220605f8e8dSAndreas Gohr    {
221605f8e8dSAndreas Gohr        if (!$prefix) {
222605f8e8dSAndreas Gohr            $this->fallbackDirsPsr4 = (array) $paths;
223605f8e8dSAndreas Gohr        } else {
224605f8e8dSAndreas Gohr            $length = strlen($prefix);
225605f8e8dSAndreas Gohr            if ('\\' !== $prefix[$length - 1]) {
226605f8e8dSAndreas Gohr                throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
227605f8e8dSAndreas Gohr            }
228605f8e8dSAndreas Gohr            $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
229605f8e8dSAndreas Gohr            $this->prefixDirsPsr4[$prefix] = (array) $paths;
230605f8e8dSAndreas Gohr        }
231605f8e8dSAndreas Gohr    }
232605f8e8dSAndreas Gohr
233605f8e8dSAndreas Gohr    /**
234605f8e8dSAndreas Gohr     * Turns on searching the include path for class files.
235605f8e8dSAndreas Gohr     *
236605f8e8dSAndreas Gohr     * @param bool $useIncludePath
237605f8e8dSAndreas Gohr     */
238605f8e8dSAndreas Gohr    public function setUseIncludePath($useIncludePath)
239605f8e8dSAndreas Gohr    {
240605f8e8dSAndreas Gohr        $this->useIncludePath = $useIncludePath;
241605f8e8dSAndreas Gohr    }
242605f8e8dSAndreas Gohr
243605f8e8dSAndreas Gohr    /**
244605f8e8dSAndreas Gohr     * Can be used to check if the autoloader uses the include path to check
245605f8e8dSAndreas Gohr     * for classes.
246605f8e8dSAndreas Gohr     *
247605f8e8dSAndreas Gohr     * @return bool
248605f8e8dSAndreas Gohr     */
249605f8e8dSAndreas Gohr    public function getUseIncludePath()
250605f8e8dSAndreas Gohr    {
251605f8e8dSAndreas Gohr        return $this->useIncludePath;
252605f8e8dSAndreas Gohr    }
253605f8e8dSAndreas Gohr
254605f8e8dSAndreas Gohr    /**
255605f8e8dSAndreas Gohr     * Turns off searching the prefix and fallback directories for classes
256605f8e8dSAndreas Gohr     * that have not been registered with the class map.
257605f8e8dSAndreas Gohr     *
258605f8e8dSAndreas Gohr     * @param bool $classMapAuthoritative
259605f8e8dSAndreas Gohr     */
260605f8e8dSAndreas Gohr    public function setClassMapAuthoritative($classMapAuthoritative)
261605f8e8dSAndreas Gohr    {
262605f8e8dSAndreas Gohr        $this->classMapAuthoritative = $classMapAuthoritative;
263605f8e8dSAndreas Gohr    }
264605f8e8dSAndreas Gohr
265605f8e8dSAndreas Gohr    /**
266605f8e8dSAndreas Gohr     * Should class lookup fail if not found in the current class map?
267605f8e8dSAndreas Gohr     *
268605f8e8dSAndreas Gohr     * @return bool
269605f8e8dSAndreas Gohr     */
270605f8e8dSAndreas Gohr    public function isClassMapAuthoritative()
271605f8e8dSAndreas Gohr    {
272605f8e8dSAndreas Gohr        return $this->classMapAuthoritative;
273605f8e8dSAndreas Gohr    }
274605f8e8dSAndreas Gohr
275605f8e8dSAndreas Gohr    /**
276e0dd796dSAndreas Gohr     * APCu prefix to use to cache found/not-found classes, if the extension is enabled.
277e0dd796dSAndreas Gohr     *
278e0dd796dSAndreas Gohr     * @param string|null $apcuPrefix
279e0dd796dSAndreas Gohr     */
280e0dd796dSAndreas Gohr    public function setApcuPrefix($apcuPrefix)
281e0dd796dSAndreas Gohr    {
282*e43cd7e1SAndreas Gohr        $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null;
283e0dd796dSAndreas Gohr    }
284e0dd796dSAndreas Gohr
285e0dd796dSAndreas Gohr    /**
286e0dd796dSAndreas Gohr     * The APCu prefix in use, or null if APCu caching is not enabled.
287e0dd796dSAndreas Gohr     *
288e0dd796dSAndreas Gohr     * @return string|null
289e0dd796dSAndreas Gohr     */
290e0dd796dSAndreas Gohr    public function getApcuPrefix()
291e0dd796dSAndreas Gohr    {
292e0dd796dSAndreas Gohr        return $this->apcuPrefix;
293e0dd796dSAndreas Gohr    }
294e0dd796dSAndreas Gohr
295e0dd796dSAndreas Gohr    /**
296605f8e8dSAndreas Gohr     * Registers this instance as an autoloader.
297605f8e8dSAndreas Gohr     *
298605f8e8dSAndreas Gohr     * @param bool $prepend Whether to prepend the autoloader or not
299605f8e8dSAndreas Gohr     */
300605f8e8dSAndreas Gohr    public function register($prepend = false)
301605f8e8dSAndreas Gohr    {
302605f8e8dSAndreas Gohr        spl_autoload_register(array($this, 'loadClass'), true, $prepend);
303605f8e8dSAndreas Gohr    }
304605f8e8dSAndreas Gohr
305605f8e8dSAndreas Gohr    /**
306605f8e8dSAndreas Gohr     * Unregisters this instance as an autoloader.
307605f8e8dSAndreas Gohr     */
308605f8e8dSAndreas Gohr    public function unregister()
309605f8e8dSAndreas Gohr    {
310605f8e8dSAndreas Gohr        spl_autoload_unregister(array($this, 'loadClass'));
311605f8e8dSAndreas Gohr    }
312605f8e8dSAndreas Gohr
313605f8e8dSAndreas Gohr    /**
314605f8e8dSAndreas Gohr     * Loads the given class or interface.
315605f8e8dSAndreas Gohr     *
316605f8e8dSAndreas Gohr     * @param  string    $class The name of the class
317605f8e8dSAndreas Gohr     * @return bool|null True if loaded, null otherwise
318605f8e8dSAndreas Gohr     */
319605f8e8dSAndreas Gohr    public function loadClass($class)
320605f8e8dSAndreas Gohr    {
321605f8e8dSAndreas Gohr        if ($file = $this->findFile($class)) {
322605f8e8dSAndreas Gohr            includeFile($file);
323605f8e8dSAndreas Gohr
324605f8e8dSAndreas Gohr            return true;
325605f8e8dSAndreas Gohr        }
326605f8e8dSAndreas Gohr    }
327605f8e8dSAndreas Gohr
328605f8e8dSAndreas Gohr    /**
329605f8e8dSAndreas Gohr     * Finds the path to the file where the class is defined.
330605f8e8dSAndreas Gohr     *
331605f8e8dSAndreas Gohr     * @param string $class The name of the class
332605f8e8dSAndreas Gohr     *
333605f8e8dSAndreas Gohr     * @return string|false The path if found, false otherwise
334605f8e8dSAndreas Gohr     */
335605f8e8dSAndreas Gohr    public function findFile($class)
336605f8e8dSAndreas Gohr    {
337605f8e8dSAndreas Gohr        // class map lookup
338605f8e8dSAndreas Gohr        if (isset($this->classMap[$class])) {
339605f8e8dSAndreas Gohr            return $this->classMap[$class];
340605f8e8dSAndreas Gohr        }
3417a33d2f8SNiklas Keller        if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) {
342605f8e8dSAndreas Gohr            return false;
343605f8e8dSAndreas Gohr        }
344e0dd796dSAndreas Gohr        if (null !== $this->apcuPrefix) {
345e0dd796dSAndreas Gohr            $file = apcu_fetch($this->apcuPrefix.$class, $hit);
346e0dd796dSAndreas Gohr            if ($hit) {
347e0dd796dSAndreas Gohr                return $file;
348e0dd796dSAndreas Gohr            }
349e0dd796dSAndreas Gohr        }
350605f8e8dSAndreas Gohr
351605f8e8dSAndreas Gohr        $file = $this->findFileWithExtension($class, '.php');
352605f8e8dSAndreas Gohr
353605f8e8dSAndreas Gohr        // Search for Hack files if we are running on HHVM
3547a33d2f8SNiklas Keller        if (false === $file && defined('HHVM_VERSION')) {
355605f8e8dSAndreas Gohr            $file = $this->findFileWithExtension($class, '.hh');
356605f8e8dSAndreas Gohr        }
357605f8e8dSAndreas Gohr
358e0dd796dSAndreas Gohr        if (null !== $this->apcuPrefix) {
359e0dd796dSAndreas Gohr            apcu_add($this->apcuPrefix.$class, $file);
360e0dd796dSAndreas Gohr        }
361e0dd796dSAndreas Gohr
3627a33d2f8SNiklas Keller        if (false === $file) {
363605f8e8dSAndreas Gohr            // Remember that this class does not exist.
3647a33d2f8SNiklas Keller            $this->missingClasses[$class] = true;
365605f8e8dSAndreas Gohr        }
366605f8e8dSAndreas Gohr
367605f8e8dSAndreas Gohr        return $file;
368605f8e8dSAndreas Gohr    }
369605f8e8dSAndreas Gohr
370605f8e8dSAndreas Gohr    private function findFileWithExtension($class, $ext)
371605f8e8dSAndreas Gohr    {
372605f8e8dSAndreas Gohr        // PSR-4 lookup
373605f8e8dSAndreas Gohr        $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;
374605f8e8dSAndreas Gohr
375605f8e8dSAndreas Gohr        $first = $class[0];
376605f8e8dSAndreas Gohr        if (isset($this->prefixLengthsPsr4[$first])) {
377e0dd796dSAndreas Gohr            $subPath = $class;
378e0dd796dSAndreas Gohr            while (false !== $lastPos = strrpos($subPath, '\\')) {
379e0dd796dSAndreas Gohr                $subPath = substr($subPath, 0, $lastPos);
380e0dd796dSAndreas Gohr                $search = $subPath . '\\';
381e0dd796dSAndreas Gohr                if (isset($this->prefixDirsPsr4[$search])) {
382*e43cd7e1SAndreas Gohr                    $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1);
383e0dd796dSAndreas Gohr                    foreach ($this->prefixDirsPsr4[$search] as $dir) {
384*e43cd7e1SAndreas Gohr                        if (file_exists($file = $dir . $pathEnd)) {
385605f8e8dSAndreas Gohr                            return $file;
386605f8e8dSAndreas Gohr                        }
387605f8e8dSAndreas Gohr                    }
388605f8e8dSAndreas Gohr                }
389605f8e8dSAndreas Gohr            }
390605f8e8dSAndreas Gohr        }
391605f8e8dSAndreas Gohr
392605f8e8dSAndreas Gohr        // PSR-4 fallback dirs
393605f8e8dSAndreas Gohr        foreach ($this->fallbackDirsPsr4 as $dir) {
3947a33d2f8SNiklas Keller            if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
395605f8e8dSAndreas Gohr                return $file;
396605f8e8dSAndreas Gohr            }
397605f8e8dSAndreas Gohr        }
398605f8e8dSAndreas Gohr
399605f8e8dSAndreas Gohr        // PSR-0 lookup
400605f8e8dSAndreas Gohr        if (false !== $pos = strrpos($class, '\\')) {
401605f8e8dSAndreas Gohr            // namespaced class name
402605f8e8dSAndreas Gohr            $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
403605f8e8dSAndreas Gohr                . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
404605f8e8dSAndreas Gohr        } else {
405605f8e8dSAndreas Gohr            // PEAR-like class name
406605f8e8dSAndreas Gohr            $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext;
407605f8e8dSAndreas Gohr        }
408605f8e8dSAndreas Gohr
409605f8e8dSAndreas Gohr        if (isset($this->prefixesPsr0[$first])) {
410605f8e8dSAndreas Gohr            foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
411605f8e8dSAndreas Gohr                if (0 === strpos($class, $prefix)) {
412605f8e8dSAndreas Gohr                    foreach ($dirs as $dir) {
4137a33d2f8SNiklas Keller                        if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
414605f8e8dSAndreas Gohr                            return $file;
415605f8e8dSAndreas Gohr                        }
416605f8e8dSAndreas Gohr                    }
417605f8e8dSAndreas Gohr                }
418605f8e8dSAndreas Gohr            }
419605f8e8dSAndreas Gohr        }
420605f8e8dSAndreas Gohr
421605f8e8dSAndreas Gohr        // PSR-0 fallback dirs
422605f8e8dSAndreas Gohr        foreach ($this->fallbackDirsPsr0 as $dir) {
4237a33d2f8SNiklas Keller            if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
424605f8e8dSAndreas Gohr                return $file;
425605f8e8dSAndreas Gohr            }
426605f8e8dSAndreas Gohr        }
427605f8e8dSAndreas Gohr
428605f8e8dSAndreas Gohr        // PSR-0 include paths.
429605f8e8dSAndreas Gohr        if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) {
430605f8e8dSAndreas Gohr            return $file;
431605f8e8dSAndreas Gohr        }
4327a33d2f8SNiklas Keller
4337a33d2f8SNiklas Keller        return false;
434605f8e8dSAndreas Gohr    }
435605f8e8dSAndreas Gohr}
436605f8e8dSAndreas Gohr
437605f8e8dSAndreas Gohr/**
438605f8e8dSAndreas Gohr * Scope isolated include.
439605f8e8dSAndreas Gohr *
440605f8e8dSAndreas Gohr * Prevents access to $this/self from included files.
441605f8e8dSAndreas Gohr */
442605f8e8dSAndreas Gohrfunction includeFile($file)
443605f8e8dSAndreas Gohr{
444605f8e8dSAndreas Gohr    include $file;
445605f8e8dSAndreas Gohr}
446