1
0

Autoloader.php 2.6 KB

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