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