loader.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php namespace Laravel;
  2. class Loader {
  3. /**
  4. * The paths to be searched by the loader.
  5. *
  6. * @var array
  7. */
  8. public static $paths = array(BASE_PATH);
  9. /**
  10. * All of the class aliases.
  11. *
  12. * @var array
  13. */
  14. public static $aliases = array();
  15. /**
  16. * All of the active modules.
  17. *
  18. * @var array
  19. */
  20. public static $modules = array();
  21. /**
  22. * Bootstrap the auto-loader.
  23. *
  24. * @param array $paths
  25. * @return void
  26. */
  27. public static function bootstrap($paths = array())
  28. {
  29. static::$aliases = Config::get('aliases');
  30. foreach ($paths as $path) { static::register($path); }
  31. }
  32. /**
  33. * Load a class file for a given class name.
  34. *
  35. * This function is registered on the SPL auto-loader stack by the front controller during each request.
  36. * All Laravel class names follow a namespace to directory convention.
  37. *
  38. * @param string $class
  39. * @return void
  40. */
  41. public static function load($class)
  42. {
  43. $file = strtolower(str_replace('\\', '/', $class));
  44. if (array_key_exists($class, static::$aliases)) return class_alias(static::$aliases[$class], $class);
  45. (static::load_from_registered($file)) or static::load_from_module($file);
  46. }
  47. /**
  48. * Load a class that is stored in the registered directories.
  49. *
  50. * @param string $file
  51. * @return bool
  52. */
  53. private static function load_from_registered($file)
  54. {
  55. foreach (static::$paths as $directory)
  56. {
  57. if (file_exists($path = $directory.$file.EXT))
  58. {
  59. require $path;
  60. return true;
  61. }
  62. }
  63. return false;
  64. }
  65. /**
  66. * Load a class that is stored in a module.
  67. *
  68. * @param string $file
  69. * @return void
  70. */
  71. private static function load_from_module($file)
  72. {
  73. // Since all module models and libraries must be namespaced to the
  74. // module name, we'll extract the module name from the file.
  75. $module = substr($file, 0, strpos($file, '/'));
  76. if (in_array($module, Module::$modules))
  77. {
  78. $module = MODULE_PATH.$module.'/';
  79. // Slice the module name off of the filename. Even though module libraries
  80. // and models are namespaced under the module, there will obviously not be
  81. // a folder matching that namespace in the libraries or models directories
  82. // of the module. Slicing it off will allow us to make a clean search for
  83. // the relevant class file.
  84. $file = substr($file, strpos($file, '/') + 1);
  85. foreach (array($module.'models', $module.'libraries') as $directory)
  86. {
  87. if (file_exists($path = $directory.'/'.$file.EXT)) return require $path;
  88. }
  89. }
  90. }
  91. /**
  92. * Register a path with the auto-loader. After registering the path, it will be
  93. * checked similarly to the models and libraries directories.
  94. *
  95. * @param string $path
  96. * @return void
  97. */
  98. public static function register($path)
  99. {
  100. static::$paths[] = rtrim($path, '/').'/';
  101. }
  102. }