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__.'\ClassLoader class is deprecated since Symfony 3.3 and will be removed in 4.0. Use Composer instead.', E_USER_DEPRECATED);
17 * ClassLoader implements an PSR-0 class loader.
19 * See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
21 * $loader = new ClassLoader();
23 * // register classes with namespaces
24 * $loader->addPrefix('Symfony\Component', __DIR__.'/component');
25 * $loader->addPrefix('Symfony', __DIR__.'/framework');
27 * // activate the autoloader
28 * $loader->register();
30 * // to enable searching the include path (e.g. for PEAR packages)
31 * $loader->setUseIncludePath(true);
33 * In this example, if you try to use a class in the Symfony\Component
34 * namespace or one of its children (Symfony\Component\Console for instance),
35 * the autoloader will first look for the class under the component/
36 * directory, and it will then fallback to the framework/ directory if not
37 * found before giving up.
39 * @author Fabien Potencier <fabien@symfony.com>
40 * @author Jordi Boggiano <j.boggiano@seld.be>
42 * @deprecated since version 3.3, to be removed in 4.0.
46 private $prefixes = array();
47 private $fallbackDirs = array();
48 private $useIncludePath = false;
55 public function getPrefixes()
57 return $this->prefixes;
61 * Returns fallback directories.
65 public function getFallbackDirs()
67 return $this->fallbackDirs;
73 * @param array $prefixes Prefixes to add
75 public function addPrefixes(array $prefixes)
77 foreach ($prefixes as $prefix => $path) {
78 $this->addPrefix($prefix, $path);
83 * Registers a set of classes.
85 * @param string $prefix The classes prefix
86 * @param array|string $paths The location(s) of the classes
88 public function addPrefix($prefix, $paths)
91 foreach ((array) $paths as $path) {
92 $this->fallbackDirs[] = $path;
97 if (isset($this->prefixes[$prefix])) {
98 if (is_array($paths)) {
99 $this->prefixes[$prefix] = array_unique(array_merge(
100 $this->prefixes[$prefix],
103 } elseif (!in_array($paths, $this->prefixes[$prefix])) {
104 $this->prefixes[$prefix][] = $paths;
107 $this->prefixes[$prefix] = array_unique((array) $paths);
112 * Turns on searching the include for class files.
114 * @param bool $useIncludePath
116 public function setUseIncludePath($useIncludePath)
118 $this->useIncludePath = (bool) $useIncludePath;
122 * Can be used to check if the autoloader uses the include path to check
127 public function getUseIncludePath()
129 return $this->useIncludePath;
133 * Registers this instance as an autoloader.
135 * @param bool $prepend Whether to prepend the autoloader or not
137 public function register($prepend = false)
139 spl_autoload_register(array($this, 'loadClass'), true, $prepend);
143 * Unregisters this instance as an autoloader.
145 public function unregister()
147 spl_autoload_unregister(array($this, 'loadClass'));
151 * Loads the given class or interface.
153 * @param string $class The name of the class
155 * @return bool|null True, if loaded
157 public function loadClass($class)
159 if ($file = $this->findFile($class)) {
167 * Finds the path to the file where the class is defined.
169 * @param string $class The name of the class
171 * @return string|null The path, if found
173 public function findFile($class)
175 if (false !== $pos = strrpos($class, '\\')) {
176 // namespaced class name
177 $classPath = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos)).DIRECTORY_SEPARATOR;
178 $className = substr($class, $pos + 1);
180 // PEAR-like class name
185 $classPath .= str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
187 foreach ($this->prefixes as $prefix => $dirs) {
188 if ($class === strstr($class, $prefix)) {
189 foreach ($dirs as $dir) {
190 if (file_exists($dir.DIRECTORY_SEPARATOR.$classPath)) {
191 return $dir.DIRECTORY_SEPARATOR.$classPath;
197 foreach ($this->fallbackDirs as $dir) {
198 if (file_exists($dir.DIRECTORY_SEPARATOR.$classPath)) {
199 return $dir.DIRECTORY_SEPARATOR.$classPath;
203 if ($this->useIncludePath && $file = stream_resolve_include_path($classPath)) {