autoloader.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. $namespace = root_namespace($class).'\\';
  61. if (isset(static::$namespaces[$namespace]))
  62. {
  63. $directory = static::$namespaces[$namespace];
  64. return static::load_namespaced($class, $namespace, $directory);
  65. }
  66. // If the class uses PEAR-ish style underscores for indicating its
  67. // directory structure we'll load the class using PSR-0 standards
  68. // standards from that directory, trimming the root.
  69. $namespace = root_namespace($class, '_').'_';
  70. if (isset(static::$underscored[$namespace]))
  71. {
  72. $directory = static::$underscored[$namespace];
  73. return static::load_namespaced($class, $namespace, $directory);
  74. }
  75. // If all else fails we will just iterator through the mapped
  76. // PSR-0 directories looking for the class. This is the last
  77. // resort and slowest loading option for the class.
  78. static::load_psr($class);
  79. }
  80. /**
  81. * Load a namespaced class from a given directory.
  82. *
  83. * @param string $class
  84. * @param string $namespace
  85. * @param string $directory
  86. * @return void
  87. */
  88. protected static function load_namespaced($class, $namespace, $directory)
  89. {
  90. return static::load_psr(substr($class, strlen($namespace)), $directory);
  91. }
  92. /**
  93. * Attempt to resolve a class using the PSR-0 standard.
  94. *
  95. * @param string $class
  96. * @param string $directory
  97. * @return void
  98. */
  99. protected static function load_psr($class, $directory = null)
  100. {
  101. // The PSR-0 standard indicates that class namespaces and underscores
  102. // shoould be used to indcate the directory tree in which the class
  103. // resides, so we'll convert them to slashes.
  104. $file = str_replace(array('\\', '_'), '/', $class);
  105. $directories = $directory ?: static::$directories;
  106. $lower = strtolower($file);
  107. // Once we have formatted the class name, we'll simply spin through
  108. // the registered PSR-0 directories and attempt to locate and load
  109. // the class file into the script.
  110. foreach ((array) $directories as $directory)
  111. {
  112. if (file_exists($path = $directory.$lower.EXT))
  113. {
  114. return require $path;
  115. }
  116. elseif (file_exists($path = $directory.$file.EXT))
  117. {
  118. return require $path;
  119. }
  120. }
  121. }
  122. /**
  123. * Register an array of class to path mappings.
  124. *
  125. * @param array $mappings
  126. * @return void
  127. */
  128. public static function map($mappings)
  129. {
  130. static::$mappings = array_merge(static::$mappings, $mappings);
  131. }
  132. /**
  133. * Register a class alias with the auto-loader.
  134. *
  135. * @param string $class
  136. * @param string $alias
  137. * @return void
  138. */
  139. public static function alias($class, $alias)
  140. {
  141. static::$aliases[$alias] = $class;
  142. }
  143. /**
  144. * Register directories to be searched as a PSR-0 library.
  145. *
  146. * @param string|array $directory
  147. * @return void
  148. */
  149. public static function directories($directory)
  150. {
  151. $directories = static::format($directory);
  152. static::$directories = array_unique(array_merge(static::$directories, $directories));
  153. }
  154. /**
  155. * Register underscored "namespaces" to directory mappings.
  156. *
  157. * @param array $mappings
  158. * @return void
  159. */
  160. public static function underscored($mappings)
  161. {
  162. $mappings = static::format_mappings($mappings, '_');
  163. static::$underscored = array_merge($mappings, static::$underscored);
  164. }
  165. /**
  166. * Map namespaces to directories.
  167. *
  168. * @param array $mappings
  169. * @return void
  170. */
  171. public static function namespaces($mappings)
  172. {
  173. $mappings = static::format_mappings($mappings, '\\');
  174. static::$namespaces = array_merge($mappings, static::$namespaces);
  175. }
  176. /**
  177. * Format an array of namespace to directory mappings.
  178. *
  179. * @param array $mappings
  180. * @param string $append
  181. * @return array
  182. */
  183. protected static function format_mappings($mappings, $append)
  184. {
  185. foreach ($mappings as $namespace => $directory)
  186. {
  187. // When adding new namespaces to the mappings, we will unset the previously
  188. // mapped value if it existed. This allows previously registered spaces to
  189. // be mapped to new directories on the fly.
  190. $namespace = trim($namespace, $append).$append;
  191. unset(static::$namespaces[$namespace]);
  192. $namespaces[$namespace] = head(static::format($directory));
  193. }
  194. return $namespaces;
  195. }
  196. /**
  197. * Format an array of directories with the proper trailing slashes.
  198. *
  199. * @param array $directories
  200. * @return array
  201. */
  202. protected static function format($directories)
  203. {
  204. return array_map(function($directory)
  205. {
  206. return rtrim($directory, DS).DS;
  207. }, (array) $directories);
  208. }
  209. }