961c7518016bb312deed4b91b2c30f8ccae49584
[yaffs-website] / vendor / symfony / class-loader / UniversalClassLoader.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\ClassLoader;
13
14 @trigger_error('The '.__NAMESPACE__.'\UniversalClassLoader class is deprecated since version 2.7 and will be removed in 3.0. Use the Symfony\Component\ClassLoader\ClassLoader class instead.', E_USER_DEPRECATED);
15
16 /**
17  * UniversalClassLoader implements a "universal" autoloader for PHP 5.3.
18  *
19  * It is able to load classes that use either:
20  *
21  *  * The technical interoperability standards for PHP 5.3 namespaces and
22  *    class names (https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md);
23  *
24  *  * The PEAR naming convention for classes (http://pear.php.net/).
25  *
26  * Classes from a sub-namespace or a sub-hierarchy of PEAR classes can be
27  * looked for in a list of locations to ease the vendoring of a sub-set of
28  * classes for large projects.
29  *
30  * Example usage:
31  *
32  *     $loader = new UniversalClassLoader();
33  *
34  *     // register classes with namespaces
35  *     $loader->registerNamespaces(array(
36  *         'Symfony\Component' => __DIR__.'/component',
37  *         'Symfony' => __DIR__.'/framework',
38  *         'Sensio' => array(__DIR__.'/src', __DIR__.'/vendor'),
39  *     ));
40  *
41  *     // register a library using the PEAR naming convention
42  *     $loader->registerPrefixes(array(
43  *         'Swift_' => __DIR__.'/Swift',
44  *     ));
45  *
46  *
47  *     // to enable searching the include path (e.g. for PEAR packages)
48  *     $loader->useIncludePath(true);
49  *
50  *     // activate the autoloader
51  *     $loader->register();
52  *
53  * In this example, if you try to use a class in the Symfony\Component
54  * namespace or one of its children (Symfony\Component\Console for instance),
55  * the autoloader will first look for the class under the component/
56  * directory, and it will then fallback to the framework/ directory if not
57  * found before giving up.
58  *
59  * @author Fabien Potencier <fabien@symfony.com>
60  *
61  * @deprecated since version 2.4, to be removed in 3.0.
62  *             Use the {@link ClassLoader} class instead.
63  */
64 class UniversalClassLoader
65 {
66     private $namespaces = array();
67     private $prefixes = array();
68     private $namespaceFallbacks = array();
69     private $prefixFallbacks = array();
70     private $useIncludePath = false;
71
72     /**
73      * Turns on searching the include for class files. Allows easy loading
74      * of installed PEAR packages.
75      *
76      * @param bool $useIncludePath
77      */
78     public function useIncludePath($useIncludePath)
79     {
80         $this->useIncludePath = (bool) $useIncludePath;
81     }
82
83     /**
84      * Can be used to check if the autoloader uses the include path to check
85      * for classes.
86      *
87      * @return bool
88      */
89     public function getUseIncludePath()
90     {
91         return $this->useIncludePath;
92     }
93
94     /**
95      * Gets the configured namespaces.
96      *
97      * @return array A hash with namespaces as keys and directories as values
98      */
99     public function getNamespaces()
100     {
101         return $this->namespaces;
102     }
103
104     /**
105      * Gets the configured class prefixes.
106      *
107      * @return array A hash with class prefixes as keys and directories as values
108      */
109     public function getPrefixes()
110     {
111         return $this->prefixes;
112     }
113
114     /**
115      * Gets the directory(ies) to use as a fallback for namespaces.
116      *
117      * @return array An array of directories
118      */
119     public function getNamespaceFallbacks()
120     {
121         return $this->namespaceFallbacks;
122     }
123
124     /**
125      * Gets the directory(ies) to use as a fallback for class prefixes.
126      *
127      * @return array An array of directories
128      */
129     public function getPrefixFallbacks()
130     {
131         return $this->prefixFallbacks;
132     }
133
134     /**
135      * Registers the directory to use as a fallback for namespaces.
136      *
137      * @param array $dirs An array of directories
138      */
139     public function registerNamespaceFallbacks(array $dirs)
140     {
141         $this->namespaceFallbacks = $dirs;
142     }
143
144     /**
145      * Registers a directory to use as a fallback for namespaces.
146      *
147      * @param string $dir A directory
148      */
149     public function registerNamespaceFallback($dir)
150     {
151         $this->namespaceFallbacks[] = $dir;
152     }
153
154     /**
155      * Registers directories to use as a fallback for class prefixes.
156      *
157      * @param array $dirs An array of directories
158      */
159     public function registerPrefixFallbacks(array $dirs)
160     {
161         $this->prefixFallbacks = $dirs;
162     }
163
164     /**
165      * Registers a directory to use as a fallback for class prefixes.
166      *
167      * @param string $dir A directory
168      */
169     public function registerPrefixFallback($dir)
170     {
171         $this->prefixFallbacks[] = $dir;
172     }
173
174     /**
175      * Registers an array of namespaces.
176      *
177      * @param array $namespaces An array of namespaces (namespaces as keys and locations as values)
178      */
179     public function registerNamespaces(array $namespaces)
180     {
181         foreach ($namespaces as $namespace => $locations) {
182             $this->namespaces[$namespace] = (array) $locations;
183         }
184     }
185
186     /**
187      * Registers a namespace.
188      *
189      * @param string       $namespace The namespace
190      * @param array|string $paths     The location(s) of the namespace
191      */
192     public function registerNamespace($namespace, $paths)
193     {
194         $this->namespaces[$namespace] = (array) $paths;
195     }
196
197     /**
198      * Registers an array of classes using the PEAR naming convention.
199      *
200      * @param array $classes An array of classes (prefixes as keys and locations as values)
201      */
202     public function registerPrefixes(array $classes)
203     {
204         foreach ($classes as $prefix => $locations) {
205             $this->prefixes[$prefix] = (array) $locations;
206         }
207     }
208
209     /**
210      * Registers a set of classes using the PEAR naming convention.
211      *
212      * @param string       $prefix The classes prefix
213      * @param array|string $paths  The location(s) of the classes
214      */
215     public function registerPrefix($prefix, $paths)
216     {
217         $this->prefixes[$prefix] = (array) $paths;
218     }
219
220     /**
221      * Registers this instance as an autoloader.
222      *
223      * @param bool $prepend Whether to prepend the autoloader or not
224      */
225     public function register($prepend = false)
226     {
227         spl_autoload_register(array($this, 'loadClass'), true, $prepend);
228     }
229
230     /**
231      * Loads the given class or interface.
232      *
233      * @param string $class The name of the class
234      *
235      * @return bool|null True, if loaded
236      */
237     public function loadClass($class)
238     {
239         if ($file = $this->findFile($class)) {
240             require $file;
241
242             return true;
243         }
244     }
245
246     /**
247      * Finds the path to the file where the class is defined.
248      *
249      * @param string $class The name of the class
250      *
251      * @return string|null The path, if found
252      */
253     public function findFile($class)
254     {
255         if (false !== $pos = strrpos($class, '\\')) {
256             // namespaced class name
257             $namespace = substr($class, 0, $pos);
258             $className = substr($class, $pos + 1);
259             $normalizedClass = str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
260             foreach ($this->namespaces as $ns => $dirs) {
261                 if (0 !== strpos($namespace, $ns)) {
262                     continue;
263                 }
264
265                 foreach ($dirs as $dir) {
266                     $file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
267                     if (is_file($file)) {
268                         return $file;
269                     }
270                 }
271             }
272
273             foreach ($this->namespaceFallbacks as $dir) {
274                 $file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
275                 if (is_file($file)) {
276                     return $file;
277                 }
278             }
279         } else {
280             // PEAR-like class name
281             $normalizedClass = str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
282             foreach ($this->prefixes as $prefix => $dirs) {
283                 if (0 !== strpos($class, $prefix)) {
284                     continue;
285                 }
286
287                 foreach ($dirs as $dir) {
288                     $file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
289                     if (is_file($file)) {
290                         return $file;
291                     }
292                 }
293             }
294
295             foreach ($this->prefixFallbacks as $dir) {
296                 $file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
297                 if (is_file($file)) {
298                     return $file;
299                 }
300             }
301         }
302
303         if ($this->useIncludePath && $file = stream_resolve_include_path($normalizedClass)) {
304             return $file;
305         }
306     }
307 }