4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\ClassLoader;
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);
17 * UniversalClassLoader implements a "universal" autoloader for PHP 5.3.
19 * It is able to load classes that use either:
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);
24 * * The PEAR naming convention for classes (http://pear.php.net/).
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.
32 * $loader = new UniversalClassLoader();
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'),
41 * // register a library using the PEAR naming convention
42 * $loader->registerPrefixes(array(
43 * 'Swift_' => __DIR__.'/Swift',
47 * // to enable searching the include path (e.g. for PEAR packages)
48 * $loader->useIncludePath(true);
50 * // activate the autoloader
51 * $loader->register();
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.
59 * @author Fabien Potencier <fabien@symfony.com>
61 * @deprecated since version 2.4, to be removed in 3.0.
62 * Use the {@link ClassLoader} class instead.
64 class UniversalClassLoader
66 private $namespaces = array();
67 private $prefixes = array();
68 private $namespaceFallbacks = array();
69 private $prefixFallbacks = array();
70 private $useIncludePath = false;
73 * Turns on searching the include for class files. Allows easy loading
74 * of installed PEAR packages.
76 * @param bool $useIncludePath
78 public function useIncludePath($useIncludePath)
80 $this->useIncludePath = (bool) $useIncludePath;
84 * Can be used to check if the autoloader uses the include path to check
89 public function getUseIncludePath()
91 return $this->useIncludePath;
95 * Gets the configured namespaces.
97 * @return array A hash with namespaces as keys and directories as values
99 public function getNamespaces()
101 return $this->namespaces;
105 * Gets the configured class prefixes.
107 * @return array A hash with class prefixes as keys and directories as values
109 public function getPrefixes()
111 return $this->prefixes;
115 * Gets the directory(ies) to use as a fallback for namespaces.
117 * @return array An array of directories
119 public function getNamespaceFallbacks()
121 return $this->namespaceFallbacks;
125 * Gets the directory(ies) to use as a fallback for class prefixes.
127 * @return array An array of directories
129 public function getPrefixFallbacks()
131 return $this->prefixFallbacks;
135 * Registers the directory to use as a fallback for namespaces.
137 * @param array $dirs An array of directories
139 public function registerNamespaceFallbacks(array $dirs)
141 $this->namespaceFallbacks = $dirs;
145 * Registers a directory to use as a fallback for namespaces.
147 * @param string $dir A directory
149 public function registerNamespaceFallback($dir)
151 $this->namespaceFallbacks[] = $dir;
155 * Registers directories to use as a fallback for class prefixes.
157 * @param array $dirs An array of directories
159 public function registerPrefixFallbacks(array $dirs)
161 $this->prefixFallbacks = $dirs;
165 * Registers a directory to use as a fallback for class prefixes.
167 * @param string $dir A directory
169 public function registerPrefixFallback($dir)
171 $this->prefixFallbacks[] = $dir;
175 * Registers an array of namespaces.
177 * @param array $namespaces An array of namespaces (namespaces as keys and locations as values)
179 public function registerNamespaces(array $namespaces)
181 foreach ($namespaces as $namespace => $locations) {
182 $this->namespaces[$namespace] = (array) $locations;
187 * Registers a namespace.
189 * @param string $namespace The namespace
190 * @param array|string $paths The location(s) of the namespace
192 public function registerNamespace($namespace, $paths)
194 $this->namespaces[$namespace] = (array) $paths;
198 * Registers an array of classes using the PEAR naming convention.
200 * @param array $classes An array of classes (prefixes as keys and locations as values)
202 public function registerPrefixes(array $classes)
204 foreach ($classes as $prefix => $locations) {
205 $this->prefixes[$prefix] = (array) $locations;
210 * Registers a set of classes using the PEAR naming convention.
212 * @param string $prefix The classes prefix
213 * @param array|string $paths The location(s) of the classes
215 public function registerPrefix($prefix, $paths)
217 $this->prefixes[$prefix] = (array) $paths;
221 * Registers this instance as an autoloader.
223 * @param bool $prepend Whether to prepend the autoloader or not
225 public function register($prepend = false)
227 spl_autoload_register(array($this, 'loadClass'), true, $prepend);
231 * Loads the given class or interface.
233 * @param string $class The name of the class
235 * @return bool|null True, if loaded
237 public function loadClass($class)
239 if ($file = $this->findFile($class)) {
247 * Finds the path to the file where the class is defined.
249 * @param string $class The name of the class
251 * @return string|null The path, if found
253 public function findFile($class)
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)) {
265 foreach ($dirs as $dir) {
266 $file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
267 if (is_file($file)) {
273 foreach ($this->namespaceFallbacks as $dir) {
274 $file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
275 if (is_file($file)) {
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)) {
287 foreach ($dirs as $dir) {
288 $file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
289 if (is_file($file)) {
295 foreach ($this->prefixFallbacks as $dir) {
296 $file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
297 if (is_file($file)) {
303 if ($this->useIncludePath && $file = stream_resolve_include_path($normalizedClass)) {