autoloader.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php namespace Laravel; defined('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. * All of the class aliases registered with the auto-loader.
  11. *
  12. * @var array
  13. */
  14. public static $aliases = array();
  15. /**
  16. * The directories that use the PSR-0 naming convention.
  17. *
  18. * @var array
  19. */
  20. public static $psr = array();
  21. /**
  22. * The mappings for namespaces to directories.
  23. *
  24. * @var array
  25. */
  26. public static $namespaces = 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 is namespaced to an existing bundle and the bundle has
  52. // not been started, we will start the bundle and attempt to load the
  53. // class file again. If that fails, an error will be thrown by PHP.
  54. //
  55. // This allows bundle classes to be loaded by the auto-loader before
  56. // their class mappings have actually been registered; however, it
  57. // is up to the bundle developer to namespace their classes to
  58. // match the name of their bundle.
  59. elseif (($slash = strpos($class, '\\')) !== false)
  60. {
  61. $namespace = substr($class, 0, $slash);
  62. // If the class namespace is mapped to a directory, we will load
  63. // the class using the PSR-0 standards from that directory by
  64. // passing the directory into the "load_psr" method.
  65. if (isset(static::$namespaces[$namespace]))
  66. {
  67. $directory = static::$namespaces[$namespace];
  68. return static::load_psr(substr($class, $slash + 1), $directory);
  69. }
  70. // It's very important that we make sure the bundle has not been
  71. // started here. If we don't, we'll end up in an infinite loop
  72. // attempting to load a bundle's class.
  73. if (Bundle::exists($namespace) and ! Bundle::started($namespace))
  74. {
  75. Bundle::start($namespace);
  76. static::load($class);
  77. }
  78. }
  79. static::load_psr($class);
  80. }
  81. /**
  82. * Attempt to resolve a class using the PSR-0 standard.
  83. *
  84. * @param string $class
  85. * @param string $directory
  86. * @return void
  87. */
  88. protected static function load_psr($class, $directory = null)
  89. {
  90. // The PSR-0 standard indicates that class namespace slashes or
  91. // underscores should be used to indicate the directory tree in
  92. // which the class resides.
  93. $file = str_replace(array('\\', '_'), '/', $class);
  94. $directories = (is_nulL($directory)) ? static::$psr : array($directory);
  95. // Once we have formatted the class name, we will simply spin
  96. // through the registered PSR-0 directories and attempt to
  97. // locate and load the class into the script.
  98. foreach ($directories as $directory)
  99. {
  100. if (file_exists($path = $directory.strtolower($file).EXT))
  101. {
  102. return require $path;
  103. }
  104. elseif (file_exists($path = $directory.$file.EXT))
  105. {
  106. return require $path;
  107. }
  108. }
  109. }
  110. /**
  111. * Register an array of class to path mappings.
  112. *
  113. * <code>
  114. * // Register a class mapping with the Autoloader
  115. * Autoloader::map(array('User' => APP_PATH.'models/user.php'));
  116. * </code>
  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 psr($directory)
  143. {
  144. $directories = static::format($directory);
  145. static::$psr = array_unique(array_merge(static::$psr, $directories));
  146. }
  147. /**
  148. * Map namespaces to directories.
  149. *
  150. * @param string $namespace
  151. * @param string $path
  152. */
  153. public static function namespaces($mappings)
  154. {
  155. $directories = static::format(array_values($mappings));
  156. $mappings = array_combine(array_keys($mappings), $directories);
  157. static::$namespaces = array_merge(static::$namespaces, $mappings);
  158. }
  159. /**
  160. * Format an array of directories with the proper trailing slashes.
  161. *
  162. * @param array $directories
  163. * @return array
  164. */
  165. protected static function format($directories)
  166. {
  167. return array_map(function($directory)
  168. {
  169. return rtrim($directory, DS).DS;
  170. }, (array) $directories);
  171. }
  172. }