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;
15 * ClassLoader implements an PSR-0 class loader.
17 * See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
19 * $loader = new ClassLoader();
21 * // register classes with namespaces
22 * $loader->addPrefix('Symfony\Component', __DIR__.'/component');
23 * $loader->addPrefix('Symfony', __DIR__.'/framework');
25 * // activate the autoloader
26 * $loader->register();
28 * // to enable searching the include path (e.g. for PEAR packages)
29 * $loader->setUseIncludePath(true);
31 * In this example, if you try to use a class in the Symfony\Component
32 * namespace or one of its children (Symfony\Component\Console for instance),
33 * the autoloader will first look for the class under the component/
34 * directory, and it will then fallback to the framework/ directory if not
35 * found before giving up.
37 * @author Fabien Potencier <fabien@symfony.com>
38 * @author Jordi Boggiano <j.boggiano@seld.be>
42 private $prefixes = array();
43 private $fallbackDirs = array();
44 private $useIncludePath = false;
51 public function getPrefixes()
53 return $this->prefixes;
57 * Returns fallback directories.
61 public function getFallbackDirs()
63 return $this->fallbackDirs;
69 * @param array $prefixes Prefixes to add
71 public function addPrefixes(array $prefixes)
73 foreach ($prefixes as $prefix => $path) {
74 $this->addPrefix($prefix, $path);
79 * Registers a set of classes.
81 * @param string $prefix The classes prefix
82 * @param array|string $paths The location(s) of the classes
84 public function addPrefix($prefix, $paths)
87 foreach ((array) $paths as $path) {
88 $this->fallbackDirs[] = $path;
93 if (isset($this->prefixes[$prefix])) {
94 if (is_array($paths)) {
95 $this->prefixes[$prefix] = array_unique(array_merge(
96 $this->prefixes[$prefix],
99 } elseif (!in_array($paths, $this->prefixes[$prefix])) {
100 $this->prefixes[$prefix][] = $paths;
103 $this->prefixes[$prefix] = array_unique((array) $paths);
108 * Turns on searching the include for class files.
110 * @param bool $useIncludePath
112 public function setUseIncludePath($useIncludePath)
114 $this->useIncludePath = (bool) $useIncludePath;
118 * Can be used to check if the autoloader uses the include path to check
123 public function getUseIncludePath()
125 return $this->useIncludePath;
129 * Registers this instance as an autoloader.
131 * @param bool $prepend Whether to prepend the autoloader or not
133 public function register($prepend = false)
135 spl_autoload_register(array($this, 'loadClass'), true, $prepend);
139 * Unregisters this instance as an autoloader.
141 public function unregister()
143 spl_autoload_unregister(array($this, 'loadClass'));
147 * Loads the given class or interface.
149 * @param string $class The name of the class
151 * @return bool|null True, if loaded
153 public function loadClass($class)
155 if ($file = $this->findFile($class)) {
163 * Finds the path to the file where the class is defined.
165 * @param string $class The name of the class
167 * @return string|null The path, if found
169 public function findFile($class)
171 if (false !== $pos = strrpos($class, '\\')) {
172 // namespaced class name
173 $classPath = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos)).DIRECTORY_SEPARATOR;
174 $className = substr($class, $pos + 1);
176 // PEAR-like class name
181 $classPath .= str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
183 foreach ($this->prefixes as $prefix => $dirs) {
184 if ($class === strstr($class, $prefix)) {
185 foreach ($dirs as $dir) {
186 if (file_exists($dir.DIRECTORY_SEPARATOR.$classPath)) {
187 return $dir.DIRECTORY_SEPARATOR.$classPath;
193 foreach ($this->fallbackDirs as $dir) {
194 if (file_exists($dir.DIRECTORY_SEPARATOR.$classPath)) {
195 return $dir.DIRECTORY_SEPARATOR.$classPath;
199 if ($this->useIncludePath && $file = stream_resolve_include_path($classPath)) {