autoloader.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. * 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 to load its file.
  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 accounting
  53. // for the root of the namespace by trimming it.
  54. if ( ! is_null($info = static::namespaced($class)))
  55. {
  56. $class = substr($class, strlen($info['namespace']));
  57. return static::load_psr($class, $info['directory']);
  58. }
  59. static::load_psr($class);
  60. }
  61. /**
  62. * Attempt to resolve a class using the PSR-0 standard.
  63. *
  64. * @param string $class
  65. * @param string $directory
  66. * @return void
  67. */
  68. protected static function load_psr($class, $directory = null)
  69. {
  70. // The PSR-0 standard indicates that class namespaces and underscores
  71. // shoould be used to indcate the directory tree in which the class
  72. // resides, so we'll convert them to slashes.
  73. $file = str_replace(array('\\', '_'), '/', $class);
  74. $directories = $directory ?: static::$directories;
  75. $lower = strtolower($file);
  76. // Once we have formatted the class name, we'll simply spin through
  77. // the registered PSR-0 directories and attempt to locate and load
  78. // the class file into the script.
  79. foreach ((array) $directories as $directory)
  80. {
  81. if (file_exists($path = $directory.$lower.EXT))
  82. {
  83. return require $path;
  84. }
  85. elseif (file_exists($path = $directory.$file.EXT))
  86. {
  87. return require $path;
  88. }
  89. }
  90. }
  91. /**
  92. * Get the directory for a given namespaced class.
  93. *
  94. * @param string $class
  95. * @return string
  96. */
  97. protected static function namespaced($class)
  98. {
  99. foreach (static::$namespaces as $namespace => $directory)
  100. {
  101. if (starts_with($class, $namespace))
  102. {
  103. return compact('namespace', 'directory');
  104. }
  105. }
  106. }
  107. /**
  108. * Register an array of class to path mappings.
  109. *
  110. * @param array $mappings
  111. * @return void
  112. */
  113. public static function map($mappings)
  114. {
  115. static::$mappings = array_merge(static::$mappings, $mappings);
  116. }
  117. /**
  118. * Register a class alias with the auto-loader.
  119. *
  120. * @param string $class
  121. * @param string $alias
  122. * @return void
  123. */
  124. public static function alias($class, $alias)
  125. {
  126. static::$aliases[$alias] = $class;
  127. }
  128. /**
  129. * Register directories to be searched as a PSR-0 library.
  130. *
  131. * @param string|array $directory
  132. * @return void
  133. */
  134. public static function directories($directory)
  135. {
  136. $directories = static::format($directory);
  137. static::$directories = array_unique(array_merge(static::$directories, $directories));
  138. }
  139. /**
  140. * Register underscored "namespaces" to directory mappings.
  141. *
  142. * @param array $mappings
  143. * @return void
  144. */
  145. public static function underscored($mappings)
  146. {
  147. static::namespaces($mappings, '_');
  148. }
  149. /**
  150. * Map namespaces to directories.
  151. *
  152. * @param array $mappings
  153. * @param string $append
  154. * @return void
  155. */
  156. public static function namespaces($mappings, $append = '\\')
  157. {
  158. foreach ($mappings as $namespace => $directory)
  159. {
  160. // When adding new namespaces to the mappings, we will unset the previously
  161. // mapped value if it existed. This allows previously registered spaces to
  162. // be mapped to new directories on the fly.
  163. $namespace = trim($namespace, $append).$append;
  164. unset(static::$namespaces[$namespace]);
  165. $namespaces[$namespace] = head(static::format($directory));
  166. }
  167. // We'll array_merge the new mappings onto the front of the array so
  168. // derivative namespaces are not always shadowed by their parents.
  169. // For instance, when mappings Laravel\Docs, we don't want the
  170. // main Laravel namespace to always override it.
  171. static::$namespaces = array_merge($namespaces, static::$namespaces);
  172. }
  173. /**
  174. * Format an array of directories with the proper trailing slashes.
  175. *
  176. * @param array $directories
  177. * @return array
  178. */
  179. protected static function format($directories)
  180. {
  181. return array_map(function($directory)
  182. {
  183. return rtrim($directory, DS).DS;
  184. }, (array) $directories);
  185. }
  186. }