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