Autoloader.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. if ( !class_exists('Puc_v4p11_Autoloader', false) ):
  3. class Puc_v4p11_Autoloader {
  4. private $prefix = '';
  5. private $rootDir = '';
  6. private $libraryDir = '';
  7. private $staticMap;
  8. public function __construct() {
  9. $this->rootDir = dirname(__FILE__) . '/';
  10. $nameParts = explode('_', __CLASS__, 3);
  11. $this->prefix = $nameParts[0] . '_' . $nameParts[1] . '_';
  12. $this->libraryDir = $this->rootDir . '../..';
  13. if ( !self::isPhar() ) {
  14. $this->libraryDir = realpath($this->libraryDir);
  15. }
  16. $this->libraryDir = $this->libraryDir . '/';
  17. $this->staticMap = array(
  18. 'PucReadmeParser' => 'vendor/PucReadmeParser.php',
  19. 'Parsedown' => 'vendor/Parsedown.php',
  20. 'Puc_v4_Factory' => 'Puc/v4/Factory.php',
  21. );
  22. spl_autoload_register(array($this, 'autoload'));
  23. }
  24. /**
  25. * Determine if this file is running as part of a Phar archive.
  26. *
  27. * @return bool
  28. */
  29. private static function isPhar() {
  30. //Check if the current file path starts with "phar://".
  31. static $pharProtocol = 'phar://';
  32. return (substr(__FILE__, 0, strlen($pharProtocol)) === $pharProtocol);
  33. }
  34. public function autoload($className) {
  35. if ( isset($this->staticMap[$className]) && file_exists($this->libraryDir . $this->staticMap[$className]) ) {
  36. /** @noinspection PhpIncludeInspection */
  37. include ($this->libraryDir . $this->staticMap[$className]);
  38. return;
  39. }
  40. if (strpos($className, $this->prefix) === 0) {
  41. $path = substr($className, strlen($this->prefix));
  42. $path = str_replace('_', '/', $path);
  43. $path = $this->rootDir . $path . '.php';
  44. if (file_exists($path)) {
  45. /** @noinspection PhpIncludeInspection */
  46. include $path;
  47. }
  48. }
  49. }
  50. }
  51. endif;