autoloader.php 5.5 KB

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