autoloader.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php namespace Laravel;
  2. class Autoloader {
  3. /**
  4. * The PSR-0 compliant libraries registered with the auto-loader.
  5. *
  6. * @var array
  7. */
  8. protected static $libraries = array();
  9. /**
  10. * The paths to be searched by the auto-loader.
  11. *
  12. * @var array
  13. */
  14. protected static $paths = array(BASE_PATH, MODEL_PATH, LIBRARY_PATH);
  15. /**
  16. * Load the file corresponding to a given class.
  17. *
  18. * Laravel loads classes out of three directories: the core "laravel" directory,
  19. * and the application "models" and "libraries" directories. All of the file
  20. * names are lower cased and the directory structure corresponds with the
  21. * class namespaces.
  22. *
  23. * The application "libraries" directory also supports the inclusion of PSR-0
  24. * compliant libraries. These libraries will be detected automatically and
  25. * will be loaded according to the PSR-0 naming conventions.
  26. *
  27. * @param string $class
  28. * @return void
  29. */
  30. public static function load($class)
  31. {
  32. if (array_key_exists($class, Config::$items['application']['aliases']))
  33. {
  34. return class_alias(Config::$items['application']['aliases'][$class], $class);
  35. }
  36. if ( ! is_null($path = static::find($class)))
  37. {
  38. require $path;
  39. }
  40. }
  41. /**
  42. * Determine the file path associated with a given class name.
  43. *
  44. * @param string $class
  45. * @return string
  46. */
  47. protected static function find($class)
  48. {
  49. // After PHP namespaces were introduced, most libaries ditched underscores for
  50. // for namespaces to indicate the class directory hierarchy. We will check for
  51. // the presence of namespace slashes to determine the directory separator.
  52. $separator = (strpos($class, '\\') !== false) ? '\\' : '_';
  53. $library = substr($class, 0, strpos($class, $separator));
  54. $file = str_replace('\\', '/', $class);
  55. // If the namespace 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[$library]))
  59. {
  60. return LIBRARY_PATH.str_replace('_', '/', $file).EXT;
  61. }
  62. // Next we will search through the common Laravel paths for the class file.
  63. // The Laravel framework path, along with the libraries and models paths
  64. // will be searched according to the Laravel class naming standard.
  65. $lower = strtolower($file);
  66. foreach (static::$paths as $path)
  67. {
  68. if (file_exists($path = $path.$lower.EXT))
  69. {
  70. return $path;
  71. }
  72. }
  73. // If we could not find the class file in any of the auto-loaded locations
  74. // according to the Laravel naming standard, we will search the libraries
  75. // directory for the class according to the PSR-0 naming standard.
  76. if (file_exists($path = LIBRARY_PATH.str_replace('_', '/', $file).EXT))
  77. {
  78. static::$libraries[] = $library;
  79. return $path;
  80. }
  81. // Since not all controllers will be resolved by the controller resolver,
  82. // we will do a quick check in the controller directory for the class.
  83. // For instance, since base controllers would not be resolved by the
  84. // controller class, we will need to resolve them here.
  85. if (strpos($class, '_Controller') !== false)
  86. {
  87. $controller = str_replace(array('_Controller', '_'), array('', '/'), $class);
  88. if (file_exists($path = strtolower(CONTROLLER_PATH.$controller.EXT)))
  89. {
  90. return $path;
  91. }
  92. }
  93. }
  94. }