loader.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php namespace Laravel;
  2. class Loader {
  3. /**
  4. * The paths that will be searched by the loader.
  5. *
  6. * @var array
  7. */
  8. public static $paths = array(BASE_PATH, MODEL_PATH, LIBRARY_PATH, APP_PATH);
  9. /**
  10. * The class aliases defined for the application.
  11. *
  12. * @var array
  13. */
  14. public static $aliases = array();
  15. /**
  16. * Load the file for a given class.
  17. *
  18. * <code>
  19. * // Load the file for the "User" class
  20. * Loader::load('User');
  21. *
  22. * // Load the file for the "Repositories\User" class
  23. * Loader::load('Repositories\\User');
  24. * </code>
  25. *
  26. * @param string $class
  27. * @return void
  28. */
  29. public static function load($class)
  30. {
  31. // All Laravel core classes follow a namespace to directory convention.
  32. // We will replace all of the namespace slashes with directory slashes.
  33. $file = strtolower(str_replace('\\', '/', $class));
  34. // Check to determine if an alias exists. If it does, we will define the
  35. // alias and bail out. Aliases are defined for most used core classes.
  36. if (array_key_exists($class, static::$aliases))
  37. {
  38. return class_alias(static::$aliases[$class], $class);
  39. }
  40. foreach (static::$paths as $path)
  41. {
  42. if (file_exists($path = $path.$file.EXT))
  43. {
  44. require_once $path;
  45. return;
  46. }
  47. }
  48. }
  49. /**
  50. * Register a class alias with the auto-loader.
  51. *
  52. * @param string $alias
  53. * @param string $class
  54. * @return void
  55. */
  56. public static function alias($alias, $class)
  57. {
  58. static::$aliases[$alias] = $class;
  59. }
  60. /**
  61. * Register a path with the auto-loader.
  62. *
  63. * @param string $path
  64. * @return void
  65. */
  66. public static function path($path)
  67. {
  68. static::$paths[] = rtrim($path, '/').'/';
  69. }
  70. /**
  71. * Remove an alias from the auto-loader's alias registrations.
  72. *
  73. * @param string $alias
  74. * @return void
  75. */
  76. public static function forget_alias($alias)
  77. {
  78. unset(static::$aliases[$alias]);
  79. }
  80. }