installers
2 weeks ago
ClassLoader.php
2 weeks ago
InstalledVersions.php
2 weeks ago
LICENSE
5 years ago
autoload_classmap.php
2 weeks ago
autoload_files.php
2 weeks ago
autoload_namespaces.php
2 weeks ago
autoload_psr4.php
2 weeks ago
autoload_real.php
2 weeks ago
autoload_static.php
2 weeks ago
installed.php
2 weeks ago
platform_check.php
2 weeks ago
ClassLoader.php
342 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 | class ClassLoader |
| 16 | { |
| 17 | private $vendorDir; |
| 18 | |
| 19 | private $prefixLengthsPsr4 = array(); |
| 20 | private $prefixDirsPsr4 = array(); |
| 21 | private $fallbackDirsPsr4 = array(); |
| 22 | |
| 23 | private $prefixesPsr0 = array(); |
| 24 | private $fallbackDirsPsr0 = array(); |
| 25 | |
| 26 | private $useIncludePath = false; |
| 27 | |
| 28 | private $classMap = array(); |
| 29 | |
| 30 | private $classMapAuthoritative = false; |
| 31 | |
| 32 | private $missingClasses = array(); |
| 33 | |
| 34 | private $apcuPrefix; |
| 35 | |
| 36 | private static $registeredLoaders = array(); |
| 37 | |
| 38 | public function __construct($vendorDir = null) |
| 39 | { |
| 40 | $this->vendorDir = $vendorDir; |
| 41 | } |
| 42 | |
| 43 | public function getPrefixes() |
| 44 | { |
| 45 | if (!empty($this->prefixesPsr0)) { |
| 46 | return call_user_func_array('array_merge', array_values($this->prefixesPsr0)); |
| 47 | } |
| 48 | |
| 49 | return array(); |
| 50 | } |
| 51 | |
| 52 | public function getPrefixesPsr4() |
| 53 | { |
| 54 | return $this->prefixDirsPsr4; |
| 55 | } |
| 56 | |
| 57 | public function getFallbackDirs() |
| 58 | { |
| 59 | return $this->fallbackDirsPsr0; |
| 60 | } |
| 61 | |
| 62 | public function getFallbackDirsPsr4() |
| 63 | { |
| 64 | return $this->fallbackDirsPsr4; |
| 65 | } |
| 66 | |
| 67 | public function getClassMap() |
| 68 | { |
| 69 | return $this->classMap; |
| 70 | } |
| 71 | |
| 72 | public function addClassMap(array $classMap) |
| 73 | { |
| 74 | if ($this->classMap) { |
| 75 | $this->classMap = array_merge($this->classMap, $classMap); |
| 76 | } else { |
| 77 | $this->classMap = $classMap; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | public function add($prefix, $paths, $prepend = false) |
| 82 | { |
| 83 | if (!$prefix) { |
| 84 | if ($prepend) { |
| 85 | $this->fallbackDirsPsr0 = array_merge( |
| 86 | (array) $paths, |
| 87 | $this->fallbackDirsPsr0 |
| 88 | ); |
| 89 | } else { |
| 90 | $this->fallbackDirsPsr0 = array_merge( |
| 91 | $this->fallbackDirsPsr0, |
| 92 | (array) $paths |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | $first = $prefix[0]; |
| 100 | if (!isset($this->prefixesPsr0[$first][$prefix])) { |
| 101 | $this->prefixesPsr0[$first][$prefix] = (array) $paths; |
| 102 | |
| 103 | return; |
| 104 | } |
| 105 | if ($prepend) { |
| 106 | $this->prefixesPsr0[$first][$prefix] = array_merge( |
| 107 | (array) $paths, |
| 108 | $this->prefixesPsr0[$first][$prefix] |
| 109 | ); |
| 110 | } else { |
| 111 | $this->prefixesPsr0[$first][$prefix] = array_merge( |
| 112 | $this->prefixesPsr0[$first][$prefix], |
| 113 | (array) $paths |
| 114 | ); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | public function addPsr4($prefix, $paths, $prepend = false) |
| 119 | { |
| 120 | if (!$prefix) { |
| 121 | if ($prepend) { |
| 122 | $this->fallbackDirsPsr4 = array_merge( |
| 123 | (array) $paths, |
| 124 | $this->fallbackDirsPsr4 |
| 125 | ); |
| 126 | } else { |
| 127 | $this->fallbackDirsPsr4 = array_merge( |
| 128 | $this->fallbackDirsPsr4, |
| 129 | (array) $paths |
| 130 | ); |
| 131 | } |
| 132 | } elseif (!isset($this->prefixDirsPsr4[$prefix])) { |
| 133 | $length = strlen($prefix); |
| 134 | if ('\\' !== $prefix[$length - 1]) { |
| 135 | throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
| 136 | } |
| 137 | $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
| 138 | $this->prefixDirsPsr4[$prefix] = (array) $paths; |
| 139 | } elseif ($prepend) { |
| 140 | $this->prefixDirsPsr4[$prefix] = array_merge( |
| 141 | (array) $paths, |
| 142 | $this->prefixDirsPsr4[$prefix] |
| 143 | ); |
| 144 | } else { |
| 145 | $this->prefixDirsPsr4[$prefix] = array_merge( |
| 146 | $this->prefixDirsPsr4[$prefix], |
| 147 | (array) $paths |
| 148 | ); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | public function set($prefix, $paths) |
| 153 | { |
| 154 | if (!$prefix) { |
| 155 | $this->fallbackDirsPsr0 = (array) $paths; |
| 156 | } else { |
| 157 | $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | public function setPsr4($prefix, $paths) |
| 162 | { |
| 163 | if (!$prefix) { |
| 164 | $this->fallbackDirsPsr4 = (array) $paths; |
| 165 | } else { |
| 166 | $length = strlen($prefix); |
| 167 | if ('\\' !== $prefix[$length - 1]) { |
| 168 | throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
| 169 | } |
| 170 | $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
| 171 | $this->prefixDirsPsr4[$prefix] = (array) $paths; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | public function setUseIncludePath($useIncludePath) |
| 176 | { |
| 177 | $this->useIncludePath = $useIncludePath; |
| 178 | } |
| 179 | |
| 180 | public function getUseIncludePath() |
| 181 | { |
| 182 | return $this->useIncludePath; |
| 183 | } |
| 184 | |
| 185 | public function setClassMapAuthoritative($classMapAuthoritative) |
| 186 | { |
| 187 | $this->classMapAuthoritative = $classMapAuthoritative; |
| 188 | } |
| 189 | |
| 190 | public function isClassMapAuthoritative() |
| 191 | { |
| 192 | return $this->classMapAuthoritative; |
| 193 | } |
| 194 | |
| 195 | public function setApcuPrefix($apcuPrefix) |
| 196 | { |
| 197 | $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null; |
| 198 | } |
| 199 | |
| 200 | public function getApcuPrefix() |
| 201 | { |
| 202 | return $this->apcuPrefix; |
| 203 | } |
| 204 | |
| 205 | public function register($prepend = false) |
| 206 | { |
| 207 | spl_autoload_register(array($this, 'loadClass'), true, $prepend); |
| 208 | |
| 209 | if (null === $this->vendorDir) { |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | if ($prepend) { |
| 214 | self::$registeredLoaders = array($this->vendorDir => $this) + self::$registeredLoaders; |
| 215 | } else { |
| 216 | unset(self::$registeredLoaders[$this->vendorDir]); |
| 217 | self::$registeredLoaders[$this->vendorDir] = $this; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | public function unregister() |
| 222 | { |
| 223 | spl_autoload_unregister(array($this, 'loadClass')); |
| 224 | |
| 225 | if (null !== $this->vendorDir) { |
| 226 | unset(self::$registeredLoaders[$this->vendorDir]); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | public function loadClass($class) |
| 231 | { |
| 232 | if ($file = $this->findFile($class)) { |
| 233 | includeFile($file); |
| 234 | |
| 235 | return true; |
| 236 | } |
| 237 | |
| 238 | return null; |
| 239 | } |
| 240 | |
| 241 | public function findFile($class) |
| 242 | { |
| 243 | if (isset($this->classMap[$class])) { |
| 244 | return $this->classMap[$class]; |
| 245 | } |
| 246 | if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { |
| 247 | return false; |
| 248 | } |
| 249 | if (null !== $this->apcuPrefix) { |
| 250 | $file = apcu_fetch($this->apcuPrefix.$class, $hit); |
| 251 | if ($hit) { |
| 252 | return $file; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | $file = $this->findFileWithExtension($class, '.php'); |
| 257 | |
| 258 | if (false === $file && defined('HHVM_VERSION')) { |
| 259 | $file = $this->findFileWithExtension($class, '.hh'); |
| 260 | } |
| 261 | |
| 262 | if (null !== $this->apcuPrefix) { |
| 263 | apcu_add($this->apcuPrefix.$class, $file); |
| 264 | } |
| 265 | |
| 266 | if (false === $file) { |
| 267 | $this->missingClasses[$class] = true; |
| 268 | } |
| 269 | |
| 270 | return $file; |
| 271 | } |
| 272 | |
| 273 | public static function getRegisteredLoaders() |
| 274 | { |
| 275 | return self::$registeredLoaders; |
| 276 | } |
| 277 | |
| 278 | private function findFileWithExtension($class, $ext) |
| 279 | { |
| 280 | $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; |
| 281 | |
| 282 | $first = $class[0]; |
| 283 | if (isset($this->prefixLengthsPsr4[$first])) { |
| 284 | $subPath = $class; |
| 285 | while (false !== $lastPos = strrpos($subPath, '\\')) { |
| 286 | $subPath = substr($subPath, 0, $lastPos); |
| 287 | $search = $subPath . '\\'; |
| 288 | if (isset($this->prefixDirsPsr4[$search])) { |
| 289 | $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1); |
| 290 | foreach ($this->prefixDirsPsr4[$search] as $dir) { |
| 291 | if (file_exists($file = $dir . $pathEnd)) { |
| 292 | return $file; |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | foreach ($this->fallbackDirsPsr4 as $dir) { |
| 300 | if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) { |
| 301 | return $file; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | if (false !== $pos = strrpos($class, '\\')) { |
| 306 | $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1) |
| 307 | . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); |
| 308 | } else { |
| 309 | $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; |
| 310 | } |
| 311 | |
| 312 | if (isset($this->prefixesPsr0[$first])) { |
| 313 | foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) { |
| 314 | if (0 === strpos($class, $prefix)) { |
| 315 | foreach ($dirs as $dir) { |
| 316 | if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
| 317 | return $file; |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | foreach ($this->fallbackDirsPsr0 as $dir) { |
| 325 | if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
| 326 | return $file; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) { |
| 331 | return $file; |
| 332 | } |
| 333 | |
| 334 | return false; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | function includeFile($file) |
| 339 | { |
| 340 | include $file; |
| 341 | } |
| 342 |