autoloader.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php namespace Laravel;
  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 PSR-0 compliant libraries registered with the loader.
  11. *
  12. * @var array
  13. */
  14. public static $libraries = array();
  15. /**
  16. * The paths to be searched by the loader.
  17. *
  18. * @var array
  19. */
  20. protected static $paths = array(MODEL_PATH, LIBRARY_PATH);
  21. /**
  22. * Load the file corresponding to a given class.
  23. *
  24. * This method is registerd in the core bootstrap file as an SPL Autoloader.
  25. *
  26. * @param string $class
  27. * @return void
  28. */
  29. public static function load($class)
  30. {
  31. if (isset(Config::$items['application']['aliases'][$class]))
  32. {
  33. return class_alias(Config::$items['application']['aliases'][$class], $class);
  34. }
  35. if ( ! is_null($path = static::find($class)))
  36. {
  37. require $path;
  38. }
  39. }
  40. /**
  41. * Determine the file path associated with a given class name.
  42. *
  43. * @param string $class
  44. * @return string
  45. */
  46. protected static function find($class)
  47. {
  48. // First we will look for the class in the hard-coded class mappings, since
  49. // this is the fastest way to resolve a class name to its associated file.
  50. // This saves us from having to search through the file system manually.
  51. if (isset(static::$mappings[$class]))
  52. {
  53. return static::$mappings[$class];
  54. }
  55. // If the library has been registered as a PSR-0 compliant library, we will
  56. // load the library according to the PSR-0 naming standards, which state that
  57. // namespaces and underscores indicate the directory hierarchy of the class.
  58. if (isset(static::$libraries[static::library($class)]))
  59. {
  60. return LIBRARY_PATH.str_replace(array('\\', '_'), '/', $class).EXT;
  61. }
  62. // Next we will search through the common Laravel paths for the class file.
  63. // The Laravel libraries and models directories will be searched according
  64. // to the Laravel class naming standards.
  65. $file = strtolower(str_replace('\\', '/', $class));
  66. foreach (static::$paths as $path)
  67. {
  68. if (file_exists($path = $path.$file.EXT))
  69. {
  70. return $path;
  71. }
  72. }
  73. // Since not all controllers will be resolved by the controller resolver,
  74. // we will do a quick check in the controller directory for the class.
  75. // For instance, since base controllers would not be resolved by the
  76. // controller class, we will need to resolve them here.
  77. if (file_exists($path = static::controller($class)))
  78. {
  79. return $path;
  80. }
  81. }
  82. /**
  83. * Extract the "library" name from the given class.
  84. *
  85. * The library name is essentially the namespace, or the string that preceeds
  86. * the first PSR-0 separator. PSR-0 states that namespaces or undescores may
  87. * be used to indicate the directory structure in which the file resides.
  88. *
  89. * @param string $class
  90. * @return string
  91. */
  92. protected static function library($class)
  93. {
  94. if (($separator = strpos($class, '\\')) !== false)
  95. {
  96. return substr($class, 0, $separator);
  97. }
  98. elseif (($separator = strpos($class, '_')) !== false)
  99. {
  100. return substr($class, 0, $separator);
  101. }
  102. }
  103. /**
  104. * Translate a given controller class name into the corresponding file name.
  105. *
  106. * The controller suffix will be removed, and the underscores will be translated
  107. * into directory slashes. Of course, the entire class name will be converted to
  108. * lower case as well.
  109. *
  110. * <code>
  111. * // Returns "user/profile"...
  112. * $file = static::controller('User_Profile_Controller');
  113. * </code>
  114. *
  115. * @param string $class
  116. * @return string
  117. */
  118. protected static function controller($class)
  119. {
  120. $controller = str_replace(array('_', '_Controller'), array('/', ''), $class);
  121. return CONTROLLER_PATH.strtolower($controller).EXT;
  122. }
  123. /**
  124. * Register an array of class to path mappings.
  125. *
  126. * The mappings will be used to resolve file paths from class names when
  127. * a class is lazy loaded through the Autoloader, providing a faster way
  128. * of resolving file paths than the typical file_exists method.
  129. *
  130. * <code>
  131. * // Register a class mapping with the Autoloader
  132. * Autoloader::maps(array('User' => MODEL_PATH.'user'.EXT));
  133. * </code>
  134. *
  135. * @param array $mappings
  136. * @return void
  137. */
  138. public static function maps($mappings)
  139. {
  140. foreach ($mappings as $class => $path)
  141. {
  142. static::$mappings[$class] = $path;
  143. }
  144. }
  145. /**
  146. * Register PSR-0 libraries with the Autoloader.
  147. *
  148. * The library names given to this method should match directories within
  149. * the application libraries directory. This method provides an easy way
  150. * to indicate that some libraries should be loaded using the PSR-0
  151. * naming conventions instead of the Laravel conventions.
  152. *
  153. * <code>
  154. * // Register the "Assetic" library with the Autoloader
  155. * Autoloader::libraries('Assetic');
  156. *
  157. * // Register several libraries with the Autoloader
  158. * Autoloader::libraries(array('Assetic', 'Twig'));
  159. * </code>
  160. *
  161. * @param array $libraries
  162. * @return void
  163. */
  164. public static function libraries($libraries)
  165. {
  166. static::$libraries = array_merge(static::$libraries, (array) $libraries);
  167. }
  168. }