autoloader.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php namespace Laravel; defined('DS') or die('No direct script access.');
  2. class Autoloader {
  3. /**
  4. * The mappings from class names to file paths.
  5. *
  6. * @var array
  7. */
  8. public static $mappings = array();
  9. /**
  10. * The directories that use the PSR-0 naming convention.
  11. *
  12. * @var array
  13. */
  14. public static $directories = array();
  15. /**
  16. * The mappings for namespaces to directories.
  17. *
  18. * @var array
  19. */
  20. public static $namespaces = array();
  21. /**
  22. * The mappings for underscored libraries to directories.
  23. *
  24. * @var array
  25. */
  26. public static $underscored = array();
  27. /**
  28. * All of the class aliases registered with the auto-loader.
  29. *
  30. * @var array
  31. */
  32. public static $aliases = array();
  33. /**
  34. * Load the file corresponding to a given class.
  35. *
  36. * This method is registerd in the bootstrap file as an SPL auto-loader.
  37. *
  38. * @param string $class
  39. * @return void
  40. */
  41. public static function load($class)
  42. {
  43. // First, we will check to see if the class has been aliased. If it has,
  44. // we will register the alias, which may cause the auto-loader to be
  45. // called again for the "real" class name to load its file.
  46. if (isset(static::$aliases[$class]))
  47. {
  48. class_alias(static::$aliases[$class], $class);
  49. }
  50. // All classes in Laravel are staticly mapped. There is no crazy search
  51. // routine that digs through directories. It's just a simple array of
  52. // class to file path maps for ultra-fast file loading.
  53. elseif (isset(static::$mappings[$class]))
  54. {
  55. require static::$mappings[$class];
  56. }
  57. // If the class namespace is mapped to a directory, we will load the
  58. // class using the PSR-0 standards from that directory accounting
  59. // for the root of the namespace by trimming it off.
  60. foreach (static::$namespaces as $namespace => $directory)
  61. {
  62. if (starts_with($class, $namespace))
  63. {
  64. return static::load_namespaced($class, $namespace, $directory);
  65. }
  66. }
  67. // If the class uses PEAR-ish style underscores for indicating its
  68. // directory structure we'll load the class using PSR-0 standards
  69. // standards from that directory, trimming the root.
  70. foreach (static::$underscored as $prefix => $directory)
  71. {
  72. if (starts_with($class, $prefix))
  73. {
  74. return static::load_namespaced($class, $prefix, $directory);
  75. }
  76. }
  77. // If all else fails we will just iterator through the mapped
  78. // PSR-0 directories looking for the class. This is the last
  79. // resort and slowest loading option for the class.
  80. static::load_psr($class);
  81. }
  82. /**
  83. * Load a namespaced class from a given directory.
  84. *
  85. * @param string $class
  86. * @param string $namespace
  87. * @param string $directory
  88. * @return void
  89. */
  90. protected static function load_namespaced($class, $namespace, $directory)
  91. {
  92. return static::load_psr(substr($class, strlen($namespace)), $directory);
  93. }
  94. /**
  95. * Attempt to resolve a class using the PSR-0 standard.
  96. *
  97. * @param string $class
  98. * @param string $directory
  99. * @return void
  100. */
  101. protected static function load_psr($class, $directory = null)
  102. {
  103. // The PSR-0 standard indicates that class namespaces and underscores
  104. // shoould be used to indcate the directory tree in which the class
  105. // resides, so we'll convert them to slashes.
  106. $file = str_replace(array('\\', '_'), '/', $class);
  107. $directories = $directory ?: static::$directories;
  108. $lower = strtolower($file);
  109. // Once we have formatted the class name, we'll simply spin through
  110. // the registered PSR-0 directories and attempt to locate and load
  111. // the class file into the script.
  112. foreach ((array) $directories as $directory)
  113. {
  114. if (file_exists($path = $directory.$lower.EXT))
  115. {
  116. return require $path;
  117. }
  118. elseif (file_exists($path = $directory.$file.EXT))
  119. {
  120. return require $path;
  121. }
  122. }
  123. }
  124. /**
  125. * Register an array of class to path mappings.
  126. *
  127. * @param array $mappings
  128. * @return void
  129. */
  130. public static function map($mappings)
  131. {
  132. static::$mappings = array_merge(static::$mappings, $mappings);
  133. }
  134. /**
  135. * Register a class alias with the auto-loader.
  136. *
  137. * @param string $class
  138. * @param string $alias
  139. * @return void
  140. */
  141. public static function alias($class, $alias)
  142. {
  143. static::$aliases[$alias] = $class;
  144. }
  145. /**
  146. * Register directories to be searched as a PSR-0 library.
  147. *
  148. * @param string|array $directory
  149. * @return void
  150. */
  151. public static function directories($directory)
  152. {
  153. $directories = static::format($directory);
  154. static::$directories = array_unique(array_merge(static::$directories, $directories));
  155. }
  156. /**
  157. * Register underscored "namespaces" to directory mappings.
  158. *
  159. * @param array $mappings
  160. * @return void
  161. */
  162. public static function underscored($mappings)
  163. {
  164. $mappings = static::format_mappings($mappings, '_');
  165. static::$underscored = array_merge($mappings, static::$underscored);
  166. }
  167. /**
  168. * Map namespaces to directories.
  169. *
  170. * @param array $mappings
  171. * @return void
  172. */
  173. public static function namespaces($mappings)
  174. {
  175. $mappings = static::format_mappings($mappings, '\\');
  176. static::$namespaces = array_merge($mappings, static::$namespaces);
  177. }
  178. /**
  179. * Format an array of namespace to directory mappings.
  180. *
  181. * @param array $mappings
  182. * @param string $append
  183. * @return array
  184. */
  185. protected static function format_mappings($mappings, $append)
  186. {
  187. foreach ($mappings as $namespace => $directory)
  188. {
  189. // When adding new namespaces to the mappings, we will unset the previously
  190. // mapped value if it existed. This allows previously registered spaces to
  191. // be mapped to new directories on the fly.
  192. $namespace = trim($namespace, $append).$append;
  193. unset(static::$namespaces[$namespace]);
  194. $namespaces[$namespace] = head(static::format($directory));
  195. }
  196. return $namespaces;
  197. }
  198. /**
  199. * Format an array of directories with the proper trailing slashes.
  200. *
  201. * @param array $directories
  202. * @return array
  203. */
  204. protected static function format($directories)
  205. {
  206. return array_map(function($directory)
  207. {
  208. return rtrim($directory, DS).DS;
  209. }, (array) $directories);
  210. }
  211. }