autoloader.php 5.3 KB

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