4 * This file is part of Composer.
6 * (c) Nils Adermann <naderman@naderman.de>
7 * Jordi Boggiano <j.boggiano@seld.be>
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
13 namespace Composer\Autoload
;
16 * ClassLoader implements a PSR-0 class loader
18 * See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
20 * $loader = new \Composer\Autoload\ClassLoader();
22 * // register classes with namespaces
23 * $loader->add('Symfony\Component', __DIR__.'/component');
24 * $loader->add('Symfony', __DIR__.'/framework');
26 * // activate the autoloader
27 * $loader->register();
29 * // to enable searching the include path (eg. for PEAR packages)
30 * $loader->setUseIncludePath(true);
32 * In this example, if you try to use a class in the Symfony\Component
33 * namespace or one of its children (Symfony\Component\Console for instance),
34 * the autoloader will first look for the class under the component/
35 * directory, and it will then fallback to the framework/ directory if not
36 * found before giving up.
38 * This class is loosely based on the Symfony UniversalClassLoader.
40 * @author Fabien Potencier <fabien@symfony.com>
41 * @author Jordi Boggiano <j.boggiano@seld.be>
45 private $prefixes = array();
46 private $fallbackDirs = array();
47 private $useIncludePath = false;
48 private $classMap = array();
50 public function getPrefixes()
52 return call_user_func_array('array_merge', $this->prefixes
);
55 public function getFallbackDirs()
57 return $this->fallbackDirs
;
60 public function getClassMap()
62 return $this->classMap
;
66 * @param array $classMap Class to filename map
68 public function addClassMap(array $classMap)
70 if ($this->classMap
) {
71 $this->classMap
= array_merge($this->classMap
, $classMap);
73 $this->classMap
= $classMap;
78 * Registers a set of classes, merging with any others previously set.
80 * @param string $prefix The classes prefix
81 * @param array|string $paths The location(s) of the classes
82 * @param bool $prepend Prepend the location(s)
84 public function add($prefix, $paths, $prepend = false)
88 $this->fallbackDirs
= array_merge(
93 $this->fallbackDirs
= array_merge(
103 if (!isset($this->prefixes
[$first][$prefix])) {
104 $this->prefixes
[$first][$prefix] = (array) $paths;
109 $this->prefixes
[$first][$prefix] = array_merge(
111 $this->prefixes
[$first][$prefix]
114 $this->prefixes
[$first][$prefix] = array_merge(
115 $this->prefixes
[$first][$prefix],
122 * Registers a set of classes, replacing any others previously set.
124 * @param string $prefix The classes prefix
125 * @param array|string $paths The location(s) of the classes
127 public function set($prefix, $paths)
130 $this->fallbackDirs
= (array) $paths;
134 $this->prefixes
[substr($prefix, 0, 1)][$prefix] = (array) $paths;
138 * Turns on searching the include path for class files.
140 * @param bool $useIncludePath
142 public function setUseIncludePath($useIncludePath)
144 $this->useIncludePath
= $useIncludePath;
148 * Can be used to check if the autoloader uses the include path to check
153 public function getUseIncludePath()
155 return $this->useIncludePath
;
159 * Registers this instance as an autoloader.
161 * @param bool $prepend Whether to prepend the autoloader or not
163 public function register($prepend = false)
165 spl_autoload_register(array($this, 'loadClass'), true, $prepend);
169 * Unregisters this instance as an autoloader.
171 public function unregister()
173 spl_autoload_unregister(array($this, 'loadClass'));
177 * Loads the given class or interface.
179 * @param string $class The name of the class
180 * @return bool|null True if loaded, null otherwise
182 public function loadClass($class)
184 if ($file = $this->findFile($class)) {
192 * Finds the path to the file where the class is defined.
194 * @param string $class The name of the class
196 * @return string|false The path if found, false otherwise
198 public function findFile($class)
200 // work around for PHP 5.3.0 - 5.3.2 https://bugs.php.net/50731
201 if ('\\' == $class[0]) {
202 $class = substr($class, 1);
205 if (isset($this->classMap
[$class])) {
206 return $this->classMap
[$class];
209 if (false !== $pos = strrpos($class, '\\')) {
210 // namespaced class name
211 $classPath = strtr(substr($class, 0, $pos), '\\', DIRECTORY_SEPARATOR
) . DIRECTORY_SEPARATOR
;
212 $className = substr($class, $pos +
1);
214 // PEAR-like class name
219 $classPath .= strtr($className, '_', DIRECTORY_SEPARATOR
) . '.php';
222 if (isset($this->prefixes
[$first])) {
223 foreach ($this->prefixes
[$first] as $prefix => $dirs) {
224 if (0 === strpos($class, $prefix)) {
225 foreach ($dirs as $dir) {
226 if (file_exists($dir . DIRECTORY_SEPARATOR
. $classPath)) {
227 return $dir . DIRECTORY_SEPARATOR
. $classPath;
234 foreach ($this->fallbackDirs
as $dir) {
235 if (file_exists($dir . DIRECTORY_SEPARATOR
. $classPath)) {
236 return $dir . DIRECTORY_SEPARATOR
. $classPath;
240 if ($this->useIncludePath
&& $file = stream_resolve_include_path($classPath)) {
244 return $this->classMap
[$class] = false;