xref: /plugin/oauth/vendor/composer/ClassLoader.php (revision 98a3611675f696f14f2d12205fb81f7db0cf7b25)
1*98a36116SAndreas Gohr<?php
2*98a36116SAndreas Gohr
3*98a36116SAndreas Gohr/*
4*98a36116SAndreas Gohr * This file is part of Composer.
5*98a36116SAndreas Gohr *
6*98a36116SAndreas Gohr * (c) Nils Adermann <naderman@naderman.de>
7*98a36116SAndreas Gohr *     Jordi Boggiano <j.boggiano@seld.be>
8*98a36116SAndreas Gohr *
9*98a36116SAndreas Gohr * For the full copyright and license information, please view the LICENSE
10*98a36116SAndreas Gohr * file that was distributed with this source code.
11*98a36116SAndreas Gohr */
12*98a36116SAndreas Gohr
13*98a36116SAndreas Gohrnamespace Composer\Autoload;
14*98a36116SAndreas Gohr
15*98a36116SAndreas Gohr/**
16*98a36116SAndreas Gohr * ClassLoader implements a PSR-0, PSR-4 and classmap class loader.
17*98a36116SAndreas Gohr *
18*98a36116SAndreas Gohr *     $loader = new \Composer\Autoload\ClassLoader();
19*98a36116SAndreas Gohr *
20*98a36116SAndreas Gohr *     // register classes with namespaces
21*98a36116SAndreas Gohr *     $loader->add('Symfony\Component', __DIR__.'/component');
22*98a36116SAndreas Gohr *     $loader->add('Symfony',           __DIR__.'/framework');
23*98a36116SAndreas Gohr *
24*98a36116SAndreas Gohr *     // activate the autoloader
25*98a36116SAndreas Gohr *     $loader->register();
26*98a36116SAndreas Gohr *
27*98a36116SAndreas Gohr *     // to enable searching the include path (eg. for PEAR packages)
28*98a36116SAndreas Gohr *     $loader->setUseIncludePath(true);
29*98a36116SAndreas Gohr *
30*98a36116SAndreas Gohr * In this example, if you try to use a class in the Symfony\Component
31*98a36116SAndreas Gohr * namespace or one of its children (Symfony\Component\Console for instance),
32*98a36116SAndreas Gohr * the autoloader will first look for the class under the component/
33*98a36116SAndreas Gohr * directory, and it will then fallback to the framework/ directory if not
34*98a36116SAndreas Gohr * found before giving up.
35*98a36116SAndreas Gohr *
36*98a36116SAndreas Gohr * This class is loosely based on the Symfony UniversalClassLoader.
37*98a36116SAndreas Gohr *
38*98a36116SAndreas Gohr * @author Fabien Potencier <fabien@symfony.com>
39*98a36116SAndreas Gohr * @author Jordi Boggiano <j.boggiano@seld.be>
40*98a36116SAndreas Gohr * @see    http://www.php-fig.org/psr/psr-0/
41*98a36116SAndreas Gohr * @see    http://www.php-fig.org/psr/psr-4/
42*98a36116SAndreas Gohr */
43*98a36116SAndreas Gohrclass ClassLoader
44*98a36116SAndreas Gohr{
45*98a36116SAndreas Gohr    // PSR-4
46*98a36116SAndreas Gohr    private $prefixLengthsPsr4 = array();
47*98a36116SAndreas Gohr    private $prefixDirsPsr4 = array();
48*98a36116SAndreas Gohr    private $fallbackDirsPsr4 = array();
49*98a36116SAndreas Gohr
50*98a36116SAndreas Gohr    // PSR-0
51*98a36116SAndreas Gohr    private $prefixesPsr0 = array();
52*98a36116SAndreas Gohr    private $fallbackDirsPsr0 = array();
53*98a36116SAndreas Gohr
54*98a36116SAndreas Gohr    private $useIncludePath = false;
55*98a36116SAndreas Gohr    private $classMap = array();
56*98a36116SAndreas Gohr    private $classMapAuthoritative = false;
57*98a36116SAndreas Gohr    private $missingClasses = array();
58*98a36116SAndreas Gohr    private $apcuPrefix;
59*98a36116SAndreas Gohr
60*98a36116SAndreas Gohr    public function getPrefixes()
61*98a36116SAndreas Gohr    {
62*98a36116SAndreas Gohr        if (!empty($this->prefixesPsr0)) {
63*98a36116SAndreas Gohr            return call_user_func_array('array_merge', $this->prefixesPsr0);
64*98a36116SAndreas Gohr        }
65*98a36116SAndreas Gohr
66*98a36116SAndreas Gohr        return array();
67*98a36116SAndreas Gohr    }
68*98a36116SAndreas Gohr
69*98a36116SAndreas Gohr    public function getPrefixesPsr4()
70*98a36116SAndreas Gohr    {
71*98a36116SAndreas Gohr        return $this->prefixDirsPsr4;
72*98a36116SAndreas Gohr    }
73*98a36116SAndreas Gohr
74*98a36116SAndreas Gohr    public function getFallbackDirs()
75*98a36116SAndreas Gohr    {
76*98a36116SAndreas Gohr        return $this->fallbackDirsPsr0;
77*98a36116SAndreas Gohr    }
78*98a36116SAndreas Gohr
79*98a36116SAndreas Gohr    public function getFallbackDirsPsr4()
80*98a36116SAndreas Gohr    {
81*98a36116SAndreas Gohr        return $this->fallbackDirsPsr4;
82*98a36116SAndreas Gohr    }
83*98a36116SAndreas Gohr
84*98a36116SAndreas Gohr    public function getClassMap()
85*98a36116SAndreas Gohr    {
86*98a36116SAndreas Gohr        return $this->classMap;
87*98a36116SAndreas Gohr    }
88*98a36116SAndreas Gohr
89*98a36116SAndreas Gohr    /**
90*98a36116SAndreas Gohr     * @param array $classMap Class to filename map
91*98a36116SAndreas Gohr     */
92*98a36116SAndreas Gohr    public function addClassMap(array $classMap)
93*98a36116SAndreas Gohr    {
94*98a36116SAndreas Gohr        if ($this->classMap) {
95*98a36116SAndreas Gohr            $this->classMap = array_merge($this->classMap, $classMap);
96*98a36116SAndreas Gohr        } else {
97*98a36116SAndreas Gohr            $this->classMap = $classMap;
98*98a36116SAndreas Gohr        }
99*98a36116SAndreas Gohr    }
100*98a36116SAndreas Gohr
101*98a36116SAndreas Gohr    /**
102*98a36116SAndreas Gohr     * Registers a set of PSR-0 directories for a given prefix, either
103*98a36116SAndreas Gohr     * appending or prepending to the ones previously set for this prefix.
104*98a36116SAndreas Gohr     *
105*98a36116SAndreas Gohr     * @param string       $prefix  The prefix
106*98a36116SAndreas Gohr     * @param array|string $paths   The PSR-0 root directories
107*98a36116SAndreas Gohr     * @param bool         $prepend Whether to prepend the directories
108*98a36116SAndreas Gohr     */
109*98a36116SAndreas Gohr    public function add($prefix, $paths, $prepend = false)
110*98a36116SAndreas Gohr    {
111*98a36116SAndreas Gohr        if (!$prefix) {
112*98a36116SAndreas Gohr            if ($prepend) {
113*98a36116SAndreas Gohr                $this->fallbackDirsPsr0 = array_merge(
114*98a36116SAndreas Gohr                    (array) $paths,
115*98a36116SAndreas Gohr                    $this->fallbackDirsPsr0
116*98a36116SAndreas Gohr                );
117*98a36116SAndreas Gohr            } else {
118*98a36116SAndreas Gohr                $this->fallbackDirsPsr0 = array_merge(
119*98a36116SAndreas Gohr                    $this->fallbackDirsPsr0,
120*98a36116SAndreas Gohr                    (array) $paths
121*98a36116SAndreas Gohr                );
122*98a36116SAndreas Gohr            }
123*98a36116SAndreas Gohr
124*98a36116SAndreas Gohr            return;
125*98a36116SAndreas Gohr        }
126*98a36116SAndreas Gohr
127*98a36116SAndreas Gohr        $first = $prefix[0];
128*98a36116SAndreas Gohr        if (!isset($this->prefixesPsr0[$first][$prefix])) {
129*98a36116SAndreas Gohr            $this->prefixesPsr0[$first][$prefix] = (array) $paths;
130*98a36116SAndreas Gohr
131*98a36116SAndreas Gohr            return;
132*98a36116SAndreas Gohr        }
133*98a36116SAndreas Gohr        if ($prepend) {
134*98a36116SAndreas Gohr            $this->prefixesPsr0[$first][$prefix] = array_merge(
135*98a36116SAndreas Gohr                (array) $paths,
136*98a36116SAndreas Gohr                $this->prefixesPsr0[$first][$prefix]
137*98a36116SAndreas Gohr            );
138*98a36116SAndreas Gohr        } else {
139*98a36116SAndreas Gohr            $this->prefixesPsr0[$first][$prefix] = array_merge(
140*98a36116SAndreas Gohr                $this->prefixesPsr0[$first][$prefix],
141*98a36116SAndreas Gohr                (array) $paths
142*98a36116SAndreas Gohr            );
143*98a36116SAndreas Gohr        }
144*98a36116SAndreas Gohr    }
145*98a36116SAndreas Gohr
146*98a36116SAndreas Gohr    /**
147*98a36116SAndreas Gohr     * Registers a set of PSR-4 directories for a given namespace, either
148*98a36116SAndreas Gohr     * appending or prepending to the ones previously set for this namespace.
149*98a36116SAndreas Gohr     *
150*98a36116SAndreas Gohr     * @param string       $prefix  The prefix/namespace, with trailing '\\'
151*98a36116SAndreas Gohr     * @param array|string $paths   The PSR-4 base directories
152*98a36116SAndreas Gohr     * @param bool         $prepend Whether to prepend the directories
153*98a36116SAndreas Gohr     *
154*98a36116SAndreas Gohr     * @throws \InvalidArgumentException
155*98a36116SAndreas Gohr     */
156*98a36116SAndreas Gohr    public function addPsr4($prefix, $paths, $prepend = false)
157*98a36116SAndreas Gohr    {
158*98a36116SAndreas Gohr        if (!$prefix) {
159*98a36116SAndreas Gohr            // Register directories for the root namespace.
160*98a36116SAndreas Gohr            if ($prepend) {
161*98a36116SAndreas Gohr                $this->fallbackDirsPsr4 = array_merge(
162*98a36116SAndreas Gohr                    (array) $paths,
163*98a36116SAndreas Gohr                    $this->fallbackDirsPsr4
164*98a36116SAndreas Gohr                );
165*98a36116SAndreas Gohr            } else {
166*98a36116SAndreas Gohr                $this->fallbackDirsPsr4 = array_merge(
167*98a36116SAndreas Gohr                    $this->fallbackDirsPsr4,
168*98a36116SAndreas Gohr                    (array) $paths
169*98a36116SAndreas Gohr                );
170*98a36116SAndreas Gohr            }
171*98a36116SAndreas Gohr        } elseif (!isset($this->prefixDirsPsr4[$prefix])) {
172*98a36116SAndreas Gohr            // Register directories for a new namespace.
173*98a36116SAndreas Gohr            $length = strlen($prefix);
174*98a36116SAndreas Gohr            if ('\\' !== $prefix[$length - 1]) {
175*98a36116SAndreas Gohr                throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
176*98a36116SAndreas Gohr            }
177*98a36116SAndreas Gohr            $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
178*98a36116SAndreas Gohr            $this->prefixDirsPsr4[$prefix] = (array) $paths;
179*98a36116SAndreas Gohr        } elseif ($prepend) {
180*98a36116SAndreas Gohr            // Prepend directories for an already registered namespace.
181*98a36116SAndreas Gohr            $this->prefixDirsPsr4[$prefix] = array_merge(
182*98a36116SAndreas Gohr                (array) $paths,
183*98a36116SAndreas Gohr                $this->prefixDirsPsr4[$prefix]
184*98a36116SAndreas Gohr            );
185*98a36116SAndreas Gohr        } else {
186*98a36116SAndreas Gohr            // Append directories for an already registered namespace.
187*98a36116SAndreas Gohr            $this->prefixDirsPsr4[$prefix] = array_merge(
188*98a36116SAndreas Gohr                $this->prefixDirsPsr4[$prefix],
189*98a36116SAndreas Gohr                (array) $paths
190*98a36116SAndreas Gohr            );
191*98a36116SAndreas Gohr        }
192*98a36116SAndreas Gohr    }
193*98a36116SAndreas Gohr
194*98a36116SAndreas Gohr    /**
195*98a36116SAndreas Gohr     * Registers a set of PSR-0 directories for a given prefix,
196*98a36116SAndreas Gohr     * replacing any others previously set for this prefix.
197*98a36116SAndreas Gohr     *
198*98a36116SAndreas Gohr     * @param string       $prefix The prefix
199*98a36116SAndreas Gohr     * @param array|string $paths  The PSR-0 base directories
200*98a36116SAndreas Gohr     */
201*98a36116SAndreas Gohr    public function set($prefix, $paths)
202*98a36116SAndreas Gohr    {
203*98a36116SAndreas Gohr        if (!$prefix) {
204*98a36116SAndreas Gohr            $this->fallbackDirsPsr0 = (array) $paths;
205*98a36116SAndreas Gohr        } else {
206*98a36116SAndreas Gohr            $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths;
207*98a36116SAndreas Gohr        }
208*98a36116SAndreas Gohr    }
209*98a36116SAndreas Gohr
210*98a36116SAndreas Gohr    /**
211*98a36116SAndreas Gohr     * Registers a set of PSR-4 directories for a given namespace,
212*98a36116SAndreas Gohr     * replacing any others previously set for this namespace.
213*98a36116SAndreas Gohr     *
214*98a36116SAndreas Gohr     * @param string       $prefix The prefix/namespace, with trailing '\\'
215*98a36116SAndreas Gohr     * @param array|string $paths  The PSR-4 base directories
216*98a36116SAndreas Gohr     *
217*98a36116SAndreas Gohr     * @throws \InvalidArgumentException
218*98a36116SAndreas Gohr     */
219*98a36116SAndreas Gohr    public function setPsr4($prefix, $paths)
220*98a36116SAndreas Gohr    {
221*98a36116SAndreas Gohr        if (!$prefix) {
222*98a36116SAndreas Gohr            $this->fallbackDirsPsr4 = (array) $paths;
223*98a36116SAndreas Gohr        } else {
224*98a36116SAndreas Gohr            $length = strlen($prefix);
225*98a36116SAndreas Gohr            if ('\\' !== $prefix[$length - 1]) {
226*98a36116SAndreas Gohr                throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
227*98a36116SAndreas Gohr            }
228*98a36116SAndreas Gohr            $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
229*98a36116SAndreas Gohr            $this->prefixDirsPsr4[$prefix] = (array) $paths;
230*98a36116SAndreas Gohr        }
231*98a36116SAndreas Gohr    }
232*98a36116SAndreas Gohr
233*98a36116SAndreas Gohr    /**
234*98a36116SAndreas Gohr     * Turns on searching the include path for class files.
235*98a36116SAndreas Gohr     *
236*98a36116SAndreas Gohr     * @param bool $useIncludePath
237*98a36116SAndreas Gohr     */
238*98a36116SAndreas Gohr    public function setUseIncludePath($useIncludePath)
239*98a36116SAndreas Gohr    {
240*98a36116SAndreas Gohr        $this->useIncludePath = $useIncludePath;
241*98a36116SAndreas Gohr    }
242*98a36116SAndreas Gohr
243*98a36116SAndreas Gohr    /**
244*98a36116SAndreas Gohr     * Can be used to check if the autoloader uses the include path to check
245*98a36116SAndreas Gohr     * for classes.
246*98a36116SAndreas Gohr     *
247*98a36116SAndreas Gohr     * @return bool
248*98a36116SAndreas Gohr     */
249*98a36116SAndreas Gohr    public function getUseIncludePath()
250*98a36116SAndreas Gohr    {
251*98a36116SAndreas Gohr        return $this->useIncludePath;
252*98a36116SAndreas Gohr    }
253*98a36116SAndreas Gohr
254*98a36116SAndreas Gohr    /**
255*98a36116SAndreas Gohr     * Turns off searching the prefix and fallback directories for classes
256*98a36116SAndreas Gohr     * that have not been registered with the class map.
257*98a36116SAndreas Gohr     *
258*98a36116SAndreas Gohr     * @param bool $classMapAuthoritative
259*98a36116SAndreas Gohr     */
260*98a36116SAndreas Gohr    public function setClassMapAuthoritative($classMapAuthoritative)
261*98a36116SAndreas Gohr    {
262*98a36116SAndreas Gohr        $this->classMapAuthoritative = $classMapAuthoritative;
263*98a36116SAndreas Gohr    }
264*98a36116SAndreas Gohr
265*98a36116SAndreas Gohr    /**
266*98a36116SAndreas Gohr     * Should class lookup fail if not found in the current class map?
267*98a36116SAndreas Gohr     *
268*98a36116SAndreas Gohr     * @return bool
269*98a36116SAndreas Gohr     */
270*98a36116SAndreas Gohr    public function isClassMapAuthoritative()
271*98a36116SAndreas Gohr    {
272*98a36116SAndreas Gohr        return $this->classMapAuthoritative;
273*98a36116SAndreas Gohr    }
274*98a36116SAndreas Gohr
275*98a36116SAndreas Gohr    /**
276*98a36116SAndreas Gohr     * APCu prefix to use to cache found/not-found classes, if the extension is enabled.
277*98a36116SAndreas Gohr     *
278*98a36116SAndreas Gohr     * @param string|null $apcuPrefix
279*98a36116SAndreas Gohr     */
280*98a36116SAndreas Gohr    public function setApcuPrefix($apcuPrefix)
281*98a36116SAndreas Gohr    {
282*98a36116SAndreas Gohr        $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null;
283*98a36116SAndreas Gohr    }
284*98a36116SAndreas Gohr
285*98a36116SAndreas Gohr    /**
286*98a36116SAndreas Gohr     * The APCu prefix in use, or null if APCu caching is not enabled.
287*98a36116SAndreas Gohr     *
288*98a36116SAndreas Gohr     * @return string|null
289*98a36116SAndreas Gohr     */
290*98a36116SAndreas Gohr    public function getApcuPrefix()
291*98a36116SAndreas Gohr    {
292*98a36116SAndreas Gohr        return $this->apcuPrefix;
293*98a36116SAndreas Gohr    }
294*98a36116SAndreas Gohr
295*98a36116SAndreas Gohr    /**
296*98a36116SAndreas Gohr     * Registers this instance as an autoloader.
297*98a36116SAndreas Gohr     *
298*98a36116SAndreas Gohr     * @param bool $prepend Whether to prepend the autoloader or not
299*98a36116SAndreas Gohr     */
300*98a36116SAndreas Gohr    public function register($prepend = false)
301*98a36116SAndreas Gohr    {
302*98a36116SAndreas Gohr        spl_autoload_register(array($this, 'loadClass'), true, $prepend);
303*98a36116SAndreas Gohr    }
304*98a36116SAndreas Gohr
305*98a36116SAndreas Gohr    /**
306*98a36116SAndreas Gohr     * Unregisters this instance as an autoloader.
307*98a36116SAndreas Gohr     */
308*98a36116SAndreas Gohr    public function unregister()
309*98a36116SAndreas Gohr    {
310*98a36116SAndreas Gohr        spl_autoload_unregister(array($this, 'loadClass'));
311*98a36116SAndreas Gohr    }
312*98a36116SAndreas Gohr
313*98a36116SAndreas Gohr    /**
314*98a36116SAndreas Gohr     * Loads the given class or interface.
315*98a36116SAndreas Gohr     *
316*98a36116SAndreas Gohr     * @param  string    $class The name of the class
317*98a36116SAndreas Gohr     * @return bool|null True if loaded, null otherwise
318*98a36116SAndreas Gohr     */
319*98a36116SAndreas Gohr    public function loadClass($class)
320*98a36116SAndreas Gohr    {
321*98a36116SAndreas Gohr        if ($file = $this->findFile($class)) {
322*98a36116SAndreas Gohr            includeFile($file);
323*98a36116SAndreas Gohr
324*98a36116SAndreas Gohr            return true;
325*98a36116SAndreas Gohr        }
326*98a36116SAndreas Gohr    }
327*98a36116SAndreas Gohr
328*98a36116SAndreas Gohr    /**
329*98a36116SAndreas Gohr     * Finds the path to the file where the class is defined.
330*98a36116SAndreas Gohr     *
331*98a36116SAndreas Gohr     * @param string $class The name of the class
332*98a36116SAndreas Gohr     *
333*98a36116SAndreas Gohr     * @return string|false The path if found, false otherwise
334*98a36116SAndreas Gohr     */
335*98a36116SAndreas Gohr    public function findFile($class)
336*98a36116SAndreas Gohr    {
337*98a36116SAndreas Gohr        // class map lookup
338*98a36116SAndreas Gohr        if (isset($this->classMap[$class])) {
339*98a36116SAndreas Gohr            return $this->classMap[$class];
340*98a36116SAndreas Gohr        }
341*98a36116SAndreas Gohr        if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) {
342*98a36116SAndreas Gohr            return false;
343*98a36116SAndreas Gohr        }
344*98a36116SAndreas Gohr        if (null !== $this->apcuPrefix) {
345*98a36116SAndreas Gohr            $file = apcu_fetch($this->apcuPrefix.$class, $hit);
346*98a36116SAndreas Gohr            if ($hit) {
347*98a36116SAndreas Gohr                return $file;
348*98a36116SAndreas Gohr            }
349*98a36116SAndreas Gohr        }
350*98a36116SAndreas Gohr
351*98a36116SAndreas Gohr        $file = $this->findFileWithExtension($class, '.php');
352*98a36116SAndreas Gohr
353*98a36116SAndreas Gohr        // Search for Hack files if we are running on HHVM
354*98a36116SAndreas Gohr        if (false === $file && defined('HHVM_VERSION')) {
355*98a36116SAndreas Gohr            $file = $this->findFileWithExtension($class, '.hh');
356*98a36116SAndreas Gohr        }
357*98a36116SAndreas Gohr
358*98a36116SAndreas Gohr        if (null !== $this->apcuPrefix) {
359*98a36116SAndreas Gohr            apcu_add($this->apcuPrefix.$class, $file);
360*98a36116SAndreas Gohr        }
361*98a36116SAndreas Gohr
362*98a36116SAndreas Gohr        if (false === $file) {
363*98a36116SAndreas Gohr            // Remember that this class does not exist.
364*98a36116SAndreas Gohr            $this->missingClasses[$class] = true;
365*98a36116SAndreas Gohr        }
366*98a36116SAndreas Gohr
367*98a36116SAndreas Gohr        return $file;
368*98a36116SAndreas Gohr    }
369*98a36116SAndreas Gohr
370*98a36116SAndreas Gohr    private function findFileWithExtension($class, $ext)
371*98a36116SAndreas Gohr    {
372*98a36116SAndreas Gohr        // PSR-4 lookup
373*98a36116SAndreas Gohr        $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;
374*98a36116SAndreas Gohr
375*98a36116SAndreas Gohr        $first = $class[0];
376*98a36116SAndreas Gohr        if (isset($this->prefixLengthsPsr4[$first])) {
377*98a36116SAndreas Gohr            $subPath = $class;
378*98a36116SAndreas Gohr            while (false !== $lastPos = strrpos($subPath, '\\')) {
379*98a36116SAndreas Gohr                $subPath = substr($subPath, 0, $lastPos);
380*98a36116SAndreas Gohr                $search = $subPath . '\\';
381*98a36116SAndreas Gohr                if (isset($this->prefixDirsPsr4[$search])) {
382*98a36116SAndreas Gohr                    $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1);
383*98a36116SAndreas Gohr                    foreach ($this->prefixDirsPsr4[$search] as $dir) {
384*98a36116SAndreas Gohr                        if (file_exists($file = $dir . $pathEnd)) {
385*98a36116SAndreas Gohr                            return $file;
386*98a36116SAndreas Gohr                        }
387*98a36116SAndreas Gohr                    }
388*98a36116SAndreas Gohr                }
389*98a36116SAndreas Gohr            }
390*98a36116SAndreas Gohr        }
391*98a36116SAndreas Gohr
392*98a36116SAndreas Gohr        // PSR-4 fallback dirs
393*98a36116SAndreas Gohr        foreach ($this->fallbackDirsPsr4 as $dir) {
394*98a36116SAndreas Gohr            if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
395*98a36116SAndreas Gohr                return $file;
396*98a36116SAndreas Gohr            }
397*98a36116SAndreas Gohr        }
398*98a36116SAndreas Gohr
399*98a36116SAndreas Gohr        // PSR-0 lookup
400*98a36116SAndreas Gohr        if (false !== $pos = strrpos($class, '\\')) {
401*98a36116SAndreas Gohr            // namespaced class name
402*98a36116SAndreas Gohr            $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
403*98a36116SAndreas Gohr                . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
404*98a36116SAndreas Gohr        } else {
405*98a36116SAndreas Gohr            // PEAR-like class name
406*98a36116SAndreas Gohr            $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext;
407*98a36116SAndreas Gohr        }
408*98a36116SAndreas Gohr
409*98a36116SAndreas Gohr        if (isset($this->prefixesPsr0[$first])) {
410*98a36116SAndreas Gohr            foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
411*98a36116SAndreas Gohr                if (0 === strpos($class, $prefix)) {
412*98a36116SAndreas Gohr                    foreach ($dirs as $dir) {
413*98a36116SAndreas Gohr                        if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
414*98a36116SAndreas Gohr                            return $file;
415*98a36116SAndreas Gohr                        }
416*98a36116SAndreas Gohr                    }
417*98a36116SAndreas Gohr                }
418*98a36116SAndreas Gohr            }
419*98a36116SAndreas Gohr        }
420*98a36116SAndreas Gohr
421*98a36116SAndreas Gohr        // PSR-0 fallback dirs
422*98a36116SAndreas Gohr        foreach ($this->fallbackDirsPsr0 as $dir) {
423*98a36116SAndreas Gohr            if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
424*98a36116SAndreas Gohr                return $file;
425*98a36116SAndreas Gohr            }
426*98a36116SAndreas Gohr        }
427*98a36116SAndreas Gohr
428*98a36116SAndreas Gohr        // PSR-0 include paths.
429*98a36116SAndreas Gohr        if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) {
430*98a36116SAndreas Gohr            return $file;
431*98a36116SAndreas Gohr        }
432*98a36116SAndreas Gohr
433*98a36116SAndreas Gohr        return false;
434*98a36116SAndreas Gohr    }
435*98a36116SAndreas Gohr}
436*98a36116SAndreas Gohr
437*98a36116SAndreas Gohr/**
438*98a36116SAndreas Gohr * Scope isolated include.
439*98a36116SAndreas Gohr *
440*98a36116SAndreas Gohr * Prevents access to $this/self from included files.
441*98a36116SAndreas Gohr */
442*98a36116SAndreas Gohrfunction includeFile($file)
443*98a36116SAndreas Gohr{
444*98a36116SAndreas Gohr    include $file;
445*98a36116SAndreas Gohr}
446