autoloader.php 5.5 KB

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