Advertisement
Guest User

Untitled

a guest
May 29th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. <?php
  2. /**
  3. * @Author neun
  4. * @since 20.04.2015 23:55
  5. */
  6.  
  7. Autoload::load();
  8.  
  9. class Autoload
  10. {
  11. /**
  12. * The class map.
  13. *
  14. * @var array
  15. */
  16. private static $aClasses = array();
  17.  
  18. /**
  19. * Root directory of the application.
  20. *
  21. * @var string
  22. */
  23. private static $sRootDir;
  24.  
  25. /**
  26. * Load class map and register.
  27. */
  28. public static function load()
  29. {
  30. self::$sRootDir = str_replace(
  31. array('libs\mine', 'libs/mine', '\\'),
  32. array('', '', '/'),
  33. __DIR__
  34. );
  35.  
  36. self::loadClassMap();
  37. self::register();
  38. }
  39.  
  40. /**
  41. * Register mapped classes.
  42. */
  43. private static function register()
  44. {
  45. spl_autoload_register(
  46. /**
  47. * @param $sClass
  48. */
  49. function($sClass)
  50. {
  51. $sClassFile = self::preRegister($sClass);
  52.  
  53. if (is_readable($sClassFile))
  54. {
  55. require_once($sClassFile);
  56. }
  57.  
  58. if (!class_exists($sClass))
  59. {
  60. die("Class {$sClass} not found!");
  61. }
  62. });
  63. }
  64.  
  65. /**
  66. * Check whether the requested class exists, re-generate map if neccessary.
  67. *
  68. * @param string $sClass
  69. * @param bool $isRetry
  70. * @return string
  71. * @throws Exception
  72. */
  73. private static function preRegister($sClass, $isRetry = false)
  74. {
  75. if (!array_key_exists($sClass, self::$aClasses))
  76. {
  77. if (false === $isRetry)
  78. {
  79. // Try to regenerate once.
  80. self::generateClassMap();
  81. }
  82. elseif(!array_key_exists($sClass, self::$aClasses))
  83. {
  84. // Die if the class is still not there
  85. die("Class {$sClass} appears to be invalid - dying!");
  86. }
  87.  
  88. self::preRegister($sClass, true);
  89. }
  90. else
  91. {
  92. return rtrim(self::$sRootDir) .'/'. self::$aClasses[$sClass];
  93. }
  94.  
  95. return rtrim(self::$sRootDir) .'/'. self::$aClasses[$sClass];
  96. }
  97.  
  98. /**
  99. * Try loading the classmap from an existing file else regenerate.
  100. */
  101. private static function loadClassMap()
  102. {
  103. if (is_readable('classmap.json'))
  104. {
  105. // Load from existing file.
  106. self::$aClasses = (array) json_decode(file_get_contents(__DIR__ .'/classmap.json'));
  107. }
  108. else
  109. {
  110. // Regenerate and write file.
  111. self::generateClassMap();
  112. }
  113. }
  114.  
  115. /**
  116. * Generate Classmap and dump to json file.
  117. */
  118. private static function generateClassMap()
  119. {
  120. // Generate class map.
  121. self::readFolders(self::$sRootDir);
  122.  
  123. // Dump class map to json file.
  124. file_put_contents(__DIR__ .'/classmap.json', json_encode(self::$aClasses, JSON_UNESCAPED_SLASHES));
  125. }
  126.  
  127. /**
  128. * Add classes and paths recursive.
  129. *
  130. * @param $sDir
  131. */
  132. private static function readFolders($sDir)
  133. {
  134. foreach (glob($sDir . '/*') as $sPath)
  135. {
  136. // If its a directory, scan again else add class to map.
  137. if (is_dir($sPath))
  138. {
  139. self::readFolders($sPath);
  140. }
  141. else
  142. {
  143. if (preg_match('#.*\/([A-Z][A-Za-z\d]+)\.php$#', $sPath, $m) && $m[1] != 'Autoload')
  144. {
  145. self::$aClasses[$m[1]] = str_replace(self::$sRootDir . '/', '', $m[0]);
  146. }
  147. }
  148. }
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement