installers
11 years ago
ClassLoader.php
11 years ago
autoload_classmap.php
11 years ago
autoload_namespaces.php
11 years ago
autoload_psr4.php
11 years ago
autoload_real.php
11 years ago
installed.json
11 years ago
ClassLoader.php
379 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of Composer. |
| 5 | * |
| 6 | * (c) Nils Adermann <naderman@naderman.de> |
| 7 | * Jordi Boggiano <j.boggiano@seld.be> |
| 8 | * |
| 9 | * For the full copyright and license information, please view the LICENSE |
| 10 | * file that was distributed with this source code. |
| 11 | */ |
| 12 | |
| 13 | namespace Composer\Autoload; |
| 14 | |
| 15 | /** |
| 16 | * ClassLoader implements a PSR-0 class loader |
| 17 | * |
| 18 | * See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md |
| 19 | * |
| 20 | * $loader = new \Composer\Autoload\ClassLoader(); |
| 21 | * |
| 22 | * // register classes with namespaces |
| 23 | * $loader->add('Symfony\Component', __DIR__.'/component'); |
| 24 | * $loader->add('Symfony', __DIR__.'/framework'); |
| 25 | * |
| 26 | * // activate the autoloader |
| 27 | * $loader->register(); |
| 28 | * |
| 29 | * // to enable searching the include path (eg. for PEAR packages) |
| 30 | * $loader->setUseIncludePath(true); |
| 31 | * |
| 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. |
| 37 | * |
| 38 | * This class is loosely based on the Symfony UniversalClassLoader. |
| 39 | * |
| 40 | * @author Fabien Potencier <fabien@symfony.com> |
| 41 | * @author Jordi Boggiano <j.boggiano@seld.be> |
| 42 | */ |
| 43 | class ClassLoader |
| 44 | { |
| 45 | // PSR-4 |
| 46 | private $prefixLengthsPsr4 = array(); |
| 47 | private $prefixDirsPsr4 = array(); |
| 48 | private $fallbackDirsPsr4 = array(); |
| 49 | |
| 50 | // PSR-0 |
| 51 | private $prefixesPsr0 = array(); |
| 52 | private $fallbackDirsPsr0 = array(); |
| 53 | |
| 54 | private $useIncludePath = false; |
| 55 | private $classMap = array(); |
| 56 | |
| 57 | public function getPrefixes() |
| 58 | { |
| 59 | return call_user_func_array('array_merge', $this->prefixesPsr0); |
| 60 | } |
| 61 | |
| 62 | public function getPrefixesPsr4() |
| 63 | { |
| 64 | return $this->prefixDirsPsr4; |
| 65 | } |
| 66 | |
| 67 | public function getFallbackDirs() |
| 68 | { |
| 69 | return $this->fallbackDirsPsr0; |
| 70 | } |
| 71 | |
| 72 | public function getFallbackDirsPsr4() |
| 73 | { |
| 74 | return $this->fallbackDirsPsr4; |
| 75 | } |
| 76 | |
| 77 | public function getClassMap() |
| 78 | { |
| 79 | return $this->classMap; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @param array $classMap Class to filename map |
| 84 | */ |
| 85 | public function addClassMap(array $classMap) |
| 86 | { |
| 87 | if ($this->classMap) { |
| 88 | $this->classMap = array_merge($this->classMap, $classMap); |
| 89 | } else { |
| 90 | $this->classMap = $classMap; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Registers a set of PSR-0 directories for a given prefix, either |
| 96 | * appending or prepending to the ones previously set for this prefix. |
| 97 | * |
| 98 | * @param string $prefix The prefix |
| 99 | * @param array|string $paths The PSR-0 root directories |
| 100 | * @param bool $prepend Whether to prepend the directories |
| 101 | */ |
| 102 | public function add($prefix, $paths, $prepend = false) |
| 103 | { |
| 104 | if (!$prefix) { |
| 105 | if ($prepend) { |
| 106 | $this->fallbackDirsPsr0 = array_merge( |
| 107 | (array) $paths, |
| 108 | $this->fallbackDirsPsr0 |
| 109 | ); |
| 110 | } else { |
| 111 | $this->fallbackDirsPsr0 = array_merge( |
| 112 | $this->fallbackDirsPsr0, |
| 113 | (array) $paths |
| 114 | ); |
| 115 | } |
| 116 | |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | $first = $prefix[0]; |
| 121 | if (!isset($this->prefixesPsr0[$first][$prefix])) { |
| 122 | $this->prefixesPsr0[$first][$prefix] = (array) $paths; |
| 123 | |
| 124 | return; |
| 125 | } |
| 126 | if ($prepend) { |
| 127 | $this->prefixesPsr0[$first][$prefix] = array_merge( |
| 128 | (array) $paths, |
| 129 | $this->prefixesPsr0[$first][$prefix] |
| 130 | ); |
| 131 | } else { |
| 132 | $this->prefixesPsr0[$first][$prefix] = array_merge( |
| 133 | $this->prefixesPsr0[$first][$prefix], |
| 134 | (array) $paths |
| 135 | ); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Registers a set of PSR-4 directories for a given namespace, either |
| 141 | * appending or prepending to the ones previously set for this namespace. |
| 142 | * |
| 143 | * @param string $prefix The prefix/namespace, with trailing '\\' |
| 144 | * @param array|string $paths The PSR-0 base directories |
| 145 | * @param bool $prepend Whether to prepend the directories |
| 146 | */ |
| 147 | public function addPsr4($prefix, $paths, $prepend = false) |
| 148 | { |
| 149 | if (!$prefix) { |
| 150 | // Register directories for the root namespace. |
| 151 | if ($prepend) { |
| 152 | $this->fallbackDirsPsr4 = array_merge( |
| 153 | (array) $paths, |
| 154 | $this->fallbackDirsPsr4 |
| 155 | ); |
| 156 | } else { |
| 157 | $this->fallbackDirsPsr4 = array_merge( |
| 158 | $this->fallbackDirsPsr4, |
| 159 | (array) $paths |
| 160 | ); |
| 161 | } |
| 162 | } elseif (!isset($this->prefixDirsPsr4[$prefix])) { |
| 163 | // Register directories for a new namespace. |
| 164 | $length = strlen($prefix); |
| 165 | if ('\\' !== $prefix[$length - 1]) { |
| 166 | throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
| 167 | } |
| 168 | $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
| 169 | $this->prefixDirsPsr4[$prefix] = (array) $paths; |
| 170 | } elseif ($prepend) { |
| 171 | // Prepend directories for an already registered namespace. |
| 172 | $this->prefixDirsPsr4[$prefix] = array_merge( |
| 173 | (array) $paths, |
| 174 | $this->prefixDirsPsr4[$prefix] |
| 175 | ); |
| 176 | } else { |
| 177 | // Append directories for an already registered namespace. |
| 178 | $this->prefixDirsPsr4[$prefix] = array_merge( |
| 179 | $this->prefixDirsPsr4[$prefix], |
| 180 | (array) $paths |
| 181 | ); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Registers a set of PSR-0 directories for a given prefix, |
| 187 | * replacing any others previously set for this prefix. |
| 188 | * |
| 189 | * @param string $prefix The prefix |
| 190 | * @param array|string $paths The PSR-0 base directories |
| 191 | */ |
| 192 | public function set($prefix, $paths) |
| 193 | { |
| 194 | if (!$prefix) { |
| 195 | $this->fallbackDirsPsr0 = (array) $paths; |
| 196 | } else { |
| 197 | $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Registers a set of PSR-4 directories for a given namespace, |
| 203 | * replacing any others previously set for this namespace. |
| 204 | * |
| 205 | * @param string $prefix The prefix/namespace, with trailing '\\' |
| 206 | * @param array|string $paths The PSR-4 base directories |
| 207 | */ |
| 208 | public function setPsr4($prefix, $paths) { |
| 209 | if (!$prefix) { |
| 210 | $this->fallbackDirsPsr4 = (array) $paths; |
| 211 | } else { |
| 212 | $length = strlen($prefix); |
| 213 | if ('\\' !== $prefix[$length - 1]) { |
| 214 | throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
| 215 | } |
| 216 | $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
| 217 | $this->prefixDirsPsr4[$prefix] = (array) $paths; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Turns on searching the include path for class files. |
| 223 | * |
| 224 | * @param bool $useIncludePath |
| 225 | */ |
| 226 | public function setUseIncludePath($useIncludePath) |
| 227 | { |
| 228 | $this->useIncludePath = $useIncludePath; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Can be used to check if the autoloader uses the include path to check |
| 233 | * for classes. |
| 234 | * |
| 235 | * @return bool |
| 236 | */ |
| 237 | public function getUseIncludePath() |
| 238 | { |
| 239 | return $this->useIncludePath; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Registers this instance as an autoloader. |
| 244 | * |
| 245 | * @param bool $prepend Whether to prepend the autoloader or not |
| 246 | */ |
| 247 | public function register($prepend = false) |
| 248 | { |
| 249 | spl_autoload_register(array($this, 'loadClass'), true, $prepend); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Unregisters this instance as an autoloader. |
| 254 | */ |
| 255 | public function unregister() |
| 256 | { |
| 257 | spl_autoload_unregister(array($this, 'loadClass')); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Loads the given class or interface. |
| 262 | * |
| 263 | * @param string $class The name of the class |
| 264 | * @return bool|null True if loaded, null otherwise |
| 265 | */ |
| 266 | public function loadClass($class) |
| 267 | { |
| 268 | if ($file = $this->findFile($class)) { |
| 269 | includeFile($file); |
| 270 | |
| 271 | return true; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Finds the path to the file where the class is defined. |
| 277 | * |
| 278 | * @param string $class The name of the class |
| 279 | * |
| 280 | * @return string|false The path if found, false otherwise |
| 281 | */ |
| 282 | public function findFile($class) |
| 283 | { |
| 284 | // work around for PHP 5.3.0 - 5.3.2 https://bugs.php.net/50731 |
| 285 | if ('\\' == $class[0]) { |
| 286 | $class = substr($class, 1); |
| 287 | } |
| 288 | |
| 289 | // class map lookup |
| 290 | if (isset($this->classMap[$class])) { |
| 291 | return $this->classMap[$class]; |
| 292 | } |
| 293 | |
| 294 | $file = $this->findFileWithExtension($class, '.php'); |
| 295 | |
| 296 | // Search for Hack files if we are running on HHVM |
| 297 | if ($file === null && defined('HHVM_VERSION')) { |
| 298 | $file = $this->findFileWithExtension($class, '.hh'); |
| 299 | } |
| 300 | |
| 301 | if ($file === null) { |
| 302 | // Remember that this class does not exist. |
| 303 | return $this->classMap[$class] = false; |
| 304 | } |
| 305 | |
| 306 | return $file; |
| 307 | } |
| 308 | |
| 309 | private function findFileWithExtension($class, $ext) |
| 310 | { |
| 311 | // PSR-4 lookup |
| 312 | $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; |
| 313 | |
| 314 | $first = $class[0]; |
| 315 | if (isset($this->prefixLengthsPsr4[$first])) { |
| 316 | foreach ($this->prefixLengthsPsr4[$first] as $prefix => $length) { |
| 317 | if (0 === strpos($class, $prefix)) { |
| 318 | foreach ($this->prefixDirsPsr4[$prefix] as $dir) { |
| 319 | if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) { |
| 320 | return $file; |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | // PSR-4 fallback dirs |
| 328 | foreach ($this->fallbackDirsPsr4 as $dir) { |
| 329 | if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) { |
| 330 | return $file; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | // PSR-0 lookup |
| 335 | if (false !== $pos = strrpos($class, '\\')) { |
| 336 | // namespaced class name |
| 337 | $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1) |
| 338 | . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); |
| 339 | } else { |
| 340 | // PEAR-like class name |
| 341 | $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; |
| 342 | } |
| 343 | |
| 344 | if (isset($this->prefixesPsr0[$first])) { |
| 345 | foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) { |
| 346 | if (0 === strpos($class, $prefix)) { |
| 347 | foreach ($dirs as $dir) { |
| 348 | if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
| 349 | return $file; |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | // PSR-0 fallback dirs |
| 357 | foreach ($this->fallbackDirsPsr0 as $dir) { |
| 358 | if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
| 359 | return $file; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // PSR-0 include paths. |
| 364 | if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) { |
| 365 | return $file; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Scope isolated include. |
| 372 | * |
| 373 | * Prevents access to $this/self from included files. |
| 374 | */ |
| 375 | function includeFile($file) |
| 376 | { |
| 377 | include $file; |
| 378 | } |
| 379 |