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