loader.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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($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. * Search the active modules for a given file.
  67. *
  68. * @param string $file
  69. * @return void
  70. */
  71. private static function load_from_module($file)
  72. {
  73. if (is_null($module = static::module_path($file))) return;
  74. // Slice the module name off of the filename. Even though module libraries
  75. // and models are namespaced under the module, there will obviously not be
  76. // a folder matching that namespace in the libraries or models directory.
  77. $file = substr($file, strlen($module));
  78. foreach (array(MODULE_PATH.$module.'/models', MODULE_PATH.$module.'/libraries') as $directory)
  79. {
  80. if (file_exists($path = $directory.'/'.$file.EXT)) return require $path;
  81. }
  82. }
  83. /**
  84. * Search the module paths for a match on the file.
  85. *
  86. * The file namespace should correspond to a directory within the module directory.
  87. *
  88. * @param string $file
  89. * @return string
  90. */
  91. private static function module_path($file)
  92. {
  93. foreach (Module::$modules as $key => $module)
  94. {
  95. if (strpos($file, $module) === 0) return $module;
  96. }
  97. }
  98. /**
  99. * Register a path with the auto-loader.
  100. *
  101. * After registering the path, it will be checked similarly to the models and libraries directories.
  102. *
  103. * @param string $path
  104. * @return void
  105. */
  106. public static function register_path($path)
  107. {
  108. static::$paths[] = rtrim($path, '/').'/';
  109. }
  110. /**
  111. * Register an alias with the auto-loader.
  112. *
  113. * @param array $alias
  114. * @return void
  115. */
  116. public static function register_alias($alias)
  117. {
  118. static::$aliases = array_merge(static::$aliases, $alias);
  119. }
  120. /**
  121. * Remove an alias from the auto-loader's list of aliases.
  122. *
  123. * @param string $alias
  124. * @return void
  125. */
  126. public static function forget_alias($alias)
  127. {
  128. unset(static::$aliases[$alias]);
  129. }
  130. }