autoloader.php 5.4 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 $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. elseif (($slash = strpos($class, '\\')) !== false)
  52. {
  53. $namespace = substr($class, 0, $slash);
  54. // If the class namespace is mapped to a directory, we will load the class
  55. // using the PSR-0 standards from that directory; however, we will trim
  56. // off the beginning of the namespace to account for files in the root
  57. // of the mapped directory.
  58. if (isset(static::$namespaces[$namespace]))
  59. {
  60. $directory = static::$namespaces[$namespace];
  61. return static::load_psr(substr($class, $slash + 1), $directory);
  62. }
  63. // If the class is namespaced to an existing bundle and the bundle has
  64. // not been started, we will start the bundle and attempt to load the
  65. // class file again. If that fails, an error will be thrown by PHP.
  66. //
  67. // This allows bundle classes to be loaded by the auto-loader before
  68. // their class mappings have actually been registered; however, it
  69. // is up to the bundle developer to namespace their classes to
  70. // match the name of their bundle.
  71. if (Bundle::exists($namespace) and ! Bundle::started($namespace))
  72. {
  73. Bundle::start(strtolower($namespace));
  74. static::load($class);
  75. }
  76. }
  77. // If the class is not maped and is not part of a bundle or a mapped
  78. // namespace, we'll make a last ditch effort to load the class via
  79. // the PSR-0 from one of the registered directories.
  80. static::load_psr($class);
  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 namespace slashes or
  92. // underscores should be used to indicate the directory tree in
  93. // which the class resides, so we'll convert the namespace
  94. // slashes to directory slashes.
  95. $file = str_replace(array('\\', '_'), '/', $class);
  96. if (is_null($directory))
  97. {
  98. $directories = static::$psr;
  99. }
  100. else
  101. {
  102. $directories = array($directory);
  103. }
  104. // Once we have formatted the class name, we will simply spin
  105. // through the registered PSR-0 directories and attempt to
  106. // locate and load the class into the script.
  107. //
  108. // We will check for both lowercase and CamelCase files as
  109. // Laravel uses a lowercase version of PSR-0, while true
  110. // PSR-0 uses CamelCase for all file names.
  111. $lower = strtolower($file);
  112. foreach ($directories as $directory)
  113. {
  114. if (file_exists($path = $directory.$lower.EXT))
  115. {
  116. return require $path;
  117. }
  118. elseif (file_exists($path = $directory.$file.EXT))
  119. {
  120. return require $path;
  121. }
  122. }
  123. }
  124. /**
  125. * Register an array of class to path mappings.
  126. *
  127. * <code>
  128. * // Register a class mapping with the Autoloader
  129. * Autoloader::map(array('User' => path('app').'models/user.php'));
  130. * </code>
  131. *
  132. * @param array $mappings
  133. * @return void
  134. */
  135. public static function map($mappings)
  136. {
  137. static::$mappings = array_merge(static::$mappings, $mappings);
  138. }
  139. /**
  140. * Register a class alias with the auto-loader.
  141. *
  142. * @param string $class
  143. * @param string $alias
  144. * @return void
  145. */
  146. public static function alias($class, $alias)
  147. {
  148. static::$aliases[$alias] = $class;
  149. }
  150. /**
  151. * Register directories to be searched as a PSR-0 library.
  152. *
  153. * @param string|array $directory
  154. * @return void
  155. */
  156. public static function psr($directory)
  157. {
  158. $directories = static::format($directory);
  159. static::$psr = array_unique(array_merge(static::$psr, $directories));
  160. }
  161. /**
  162. * Map namespaces to directories.
  163. *
  164. * @param string $namespace
  165. * @param string $path
  166. */
  167. public static function namespaces($mappings)
  168. {
  169. $directories = static::format(array_values($mappings));
  170. $mappings = array_combine(array_keys($mappings), $directories);
  171. static::$namespaces = array_merge(static::$namespaces, $mappings);
  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. }