autoloader.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 $psr = array();
  15. /**
  16. * The mappings for namespaces to directories.
  17. *
  18. * @var array
  19. */
  20. public static $namespaces = array();
  21. /**
  22. * All of the class aliases registered with the auto-loader.
  23. *
  24. * @var array
  25. */
  26. public static $aliases = array();
  27. /**
  28. * Load the file corresponding to a given class.
  29. *
  30. * This method is registerd in the bootstrap file as an SPL auto-loader.
  31. *
  32. * @param string $class
  33. * @return void
  34. */
  35. public static function load($class)
  36. {
  37. // First, we will check to see if the class has been aliased. If it has,
  38. // we will register the alias, which may cause the auto-loader to be
  39. // called again for the "real" class name.
  40. if (isset(static::$aliases[$class]))
  41. {
  42. class_alias(static::$aliases[$class], $class);
  43. }
  44. // All classes in Laravel are staticly mapped. There is no crazy search
  45. // routine that digs through directories. It's just a simple array of
  46. // class to file path maps for ultra-fast file loading.
  47. elseif (isset(static::$mappings[$class]))
  48. {
  49. require static::$mappings[$class];
  50. }
  51. // If the class namespace is mapped to a directory, we will load the
  52. // class using the PSR-0 standards from that directory; however, we
  53. // will trim off the beginning of the namespace to account for
  54. // the root of the mapped directory.
  55. if ( ! is_null($info = static::namespaced($class)))
  56. {
  57. $class = substr($class, strlen($info['namespace']));
  58. return static::load_psr($class, $info['directory']);
  59. }
  60. // If the class is not maped and is not part of a bundle or a mapped
  61. // namespace, we'll make a last ditch effort to load the class via
  62. // the PSR-0 from one of the registered directories.
  63. static::load_psr($class);
  64. }
  65. /**
  66. * Attempt to resolve a class using the PSR-0 standard.
  67. *
  68. * @param string $class
  69. * @param string $directory
  70. * @return void
  71. */
  72. protected static function load_psr($class, $directory = null)
  73. {
  74. // The PSR-0 standard indicates that class namespaces and underscores
  75. // shoould be used to indcate the directory tree in which the class
  76. // resides, so we'll convert them to slashes.
  77. $file = str_replace(array('\\', '_'), '/', $class);
  78. $directories = $directory ?: static::$psr;
  79. $lower = strtolower($file);
  80. // Once we have formatted the class name, we'll simply spin through
  81. // the registered PSR-0 directories and attempt to locate and load
  82. // the class file into the script.
  83. foreach ((array) $directories as $directory)
  84. {
  85. if (file_exists($path = $directory.$lower.EXT))
  86. {
  87. return require $path;
  88. }
  89. elseif (file_exists($path = $directory.$file.EXT))
  90. {
  91. return require $path;
  92. }
  93. }
  94. }
  95. /**
  96. * Get the directory for a given namespaced class.
  97. *
  98. * @param string $class
  99. * @return string
  100. */
  101. protected static function namespaced($class)
  102. {
  103. foreach (static::$namespaces as $namespace => $directory)
  104. {
  105. if (starts_with($class, $namespace))
  106. {
  107. return compact('namespace', 'directory');
  108. }
  109. }
  110. }
  111. /**
  112. * Register an array of class to path mappings.
  113. *
  114. * @param array $mappings
  115. * @return void
  116. */
  117. public static function map($mappings)
  118. {
  119. static::$mappings = array_merge(static::$mappings, $mappings);
  120. }
  121. /**
  122. * Register a class alias with the auto-loader.
  123. *
  124. * @param string $class
  125. * @param string $alias
  126. * @return void
  127. */
  128. public static function alias($class, $alias)
  129. {
  130. static::$aliases[$alias] = $class;
  131. }
  132. /**
  133. * Register directories to be searched as a PSR-0 library.
  134. *
  135. * @param string|array $directory
  136. * @return void
  137. */
  138. public static function directories($directory)
  139. {
  140. $directories = static::format($directory);
  141. static::$psr = array_unique(array_merge(static::$psr, $directories));
  142. }
  143. /**
  144. * Register underscored "namespaces" to directory mappings.
  145. *
  146. * @param array $mappings
  147. * @return void
  148. */
  149. public static function underscored($mappings)
  150. {
  151. static::namespaces($mappings, '_');
  152. }
  153. /**
  154. * Map namespaces to directories.
  155. *
  156. * @param array $mappings
  157. * @param string $append
  158. * @return void
  159. */
  160. public static function namespaces($mappings, $append = '\\')
  161. {
  162. foreach ($mappings as $namespace => $directory)
  163. {
  164. // When adding new namespaces to the mappings, we will unset the previously
  165. // mapped value if it existed. This allows previously registered spaces to
  166. // be mapped to new directories on the fly.
  167. $namespace = trim($namespace, $append).$append;
  168. unset(static::$namespaces[$namespace]);
  169. $namespaces[$namespace] = head(static::format($directory));
  170. }
  171. // We'll array_merge the new mappings onto the front of the array so
  172. // derivative namespaces are not always shadowed by their parents.
  173. // For instance, when mappings Laravel\Docs, we don't want the
  174. // main Laravel namespace to always override it.
  175. static::$namespaces = array_merge($namespaces, static::$namespaces);
  176. }
  177. /**
  178. * Format an array of directories with the proper trailing slashes.
  179. *
  180. * @param array $directories
  181. * @return array
  182. */
  183. protected static function format($directories)
  184. {
  185. return array_map(function($directory)
  186. {
  187. return rtrim($directory, DS).DS;
  188. }, (array) $directories);
  189. }
  190. }