loader.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php namespace Laravel;
  2. class Loader_Facade extends Facade { public static $resolve = 'loader'; }
  3. class Loader {
  4. /**
  5. * The paths to be searched by the loader.
  6. *
  7. * @var array
  8. */
  9. private $paths;
  10. /**
  11. * All of the class aliases.
  12. *
  13. * @var array
  14. */
  15. private $aliases;
  16. /**
  17. * Bootstrap the auto-loader.
  18. *
  19. * @return void
  20. */
  21. public function __construct($aliases, $paths)
  22. {
  23. $this->paths = $paths;
  24. $this->aliases = $aliases;
  25. }
  26. /**
  27. * Load a class file for a given class name.
  28. *
  29. * This function is registered on the SPL auto-loader stack by the front controller during each request.
  30. * All Laravel class names follow a namespace to directory convention.
  31. *
  32. * @param string $class
  33. * @return void
  34. */
  35. public function load($class)
  36. {
  37. $file = strtolower(str_replace(array('\\', '_Facade'), array('/', ''), $class));
  38. if (array_key_exists($class, $this->aliases))
  39. {
  40. return class_alias($this->aliases[$class], $class);
  41. }
  42. foreach ($this->paths as $directory)
  43. {
  44. if (file_exists($path = $directory.$file.EXT))
  45. {
  46. require_once $path;
  47. return;
  48. }
  49. }
  50. }
  51. /**
  52. * Register a path with the auto-loader.
  53. *
  54. * After registering the path, it will be checked similarly to the models and libraries directories.
  55. *
  56. * @param string $path
  57. * @return void
  58. */
  59. public function register_path($path)
  60. {
  61. $this->paths[] = rtrim($path, '/').'/';
  62. }
  63. /**
  64. * Register an alias with the auto-loader.
  65. *
  66. * @param array $alias
  67. * @return void
  68. */
  69. public function register_alias($alias)
  70. {
  71. $this->aliases = array_merge($this->aliases, $alias);
  72. }
  73. /**
  74. * Remove an alias from the auto-loader's list of aliases.
  75. *
  76. * @param string $alias
  77. * @return void
  78. */
  79. public function forget_alias($alias)
  80. {
  81. unset($this->aliases[$alias]);
  82. }
  83. }