loader.php 1.7 KB

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