|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace YahnisElsts\PluginUpdateChecker\v5p4; |
| 4 | + |
| 5 | +if ( !class_exists(Autoloader::class, false) ): |
| 6 | + |
| 7 | + class Autoloader { |
| 8 | + const DEFAULT_NS_PREFIX = 'YahnisElsts\\PluginUpdateChecker\\'; |
| 9 | + |
| 10 | + private $prefix; |
| 11 | + private $rootDir; |
| 12 | + private $libraryDir; |
| 13 | + |
| 14 | + private $staticMap; |
| 15 | + |
| 16 | + public function __construct() { |
| 17 | + $this->rootDir = dirname(__FILE__) . '/'; |
| 18 | + |
| 19 | + $namespaceWithSlash = __NAMESPACE__ . '\\'; |
| 20 | + $this->prefix = $namespaceWithSlash; |
| 21 | + |
| 22 | + $this->libraryDir = $this->rootDir . '../..'; |
| 23 | + if ( !self::isPhar() ) { |
| 24 | + $this->libraryDir = realpath($this->libraryDir); |
| 25 | + } |
| 26 | + $this->libraryDir = $this->libraryDir . '/'; |
| 27 | + |
| 28 | + //Usually, dependencies like Parsedown are in the global namespace, |
| 29 | + //but if someone adds a custom namespace to the entire library, they |
| 30 | + //will be in the same namespace as this class. |
| 31 | + $isCustomNamespace = ( |
| 32 | + substr($namespaceWithSlash, 0, strlen(self::DEFAULT_NS_PREFIX)) !== self::DEFAULT_NS_PREFIX |
| 33 | + ); |
| 34 | + $libraryPrefix = $isCustomNamespace ? $namespaceWithSlash : ''; |
| 35 | + |
| 36 | + $this->staticMap = array( |
| 37 | + $libraryPrefix . 'PucReadmeParser' => 'vendor/PucReadmeParser.php', |
| 38 | + $libraryPrefix . 'Parsedown' => 'vendor/Parsedown.php', |
| 39 | + ); |
| 40 | + |
| 41 | + //Add the generic, major-version-only factory class to the static map. |
| 42 | + $versionSeparatorPos = strrpos(__NAMESPACE__, '\\v'); |
| 43 | + if ( $versionSeparatorPos !== false ) { |
| 44 | + $versionSegment = substr(__NAMESPACE__, $versionSeparatorPos + 1); |
| 45 | + $pointPos = strpos($versionSegment, 'p'); |
| 46 | + if ( ($pointPos !== false) && ($pointPos > 1) ) { |
| 47 | + $majorVersionSegment = substr($versionSegment, 0, $pointPos); |
| 48 | + $majorVersionNs = __NAMESPACE__ . '\\' . $majorVersionSegment; |
| 49 | + $this->staticMap[$majorVersionNs . '\\PucFactory'] = |
| 50 | + 'Puc/' . $majorVersionSegment . '/Factory.php'; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + spl_autoload_register(array($this, 'autoload')); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Determine if this file is running as part of a Phar archive. |
| 59 | + * |
| 60 | + * @return bool |
| 61 | + */ |
| 62 | + private static function isPhar() { |
| 63 | + //Check if the current file path starts with "phar://". |
| 64 | + static $pharProtocol = 'phar://'; |
| 65 | + return (substr(__FILE__, 0, strlen($pharProtocol)) === $pharProtocol); |
| 66 | + } |
| 67 | + |
| 68 | + public function autoload($className) { |
| 69 | + if ( isset($this->staticMap[$className]) && file_exists($this->libraryDir . $this->staticMap[$className]) ) { |
| 70 | + include($this->libraryDir . $this->staticMap[$className]); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + if ( strpos($className, $this->prefix) === 0 ) { |
| 75 | + $path = substr($className, strlen($this->prefix)); |
| 76 | + $path = str_replace(array('_', '\\'), '/', $path); |
| 77 | + $path = $this->rootDir . $path . '.php'; |
| 78 | + |
| 79 | + if ( file_exists($path) ) { |
| 80 | + include $path; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | +endif; |
0 commit comments