loader.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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();
  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. * @param string $class
  19. * @return void
  20. */
  21. public static function load($class)
  22. {
  23. // All Laravel core classes follow a namespace to directory convention. So, we will
  24. // replace all of the namespace slashes with directory slashes.
  25. $file = strtolower(str_replace('\\', '/', $class));
  26. // First, we'll check to determine if an alias exists. If it does, we will define the
  27. // alias and bail out. Aliases are defined for most developer used core classes.
  28. if (array_key_exists($class, static::$aliases)) return class_alias(static::$aliases[$class], $class);
  29. foreach (static::$paths as $path)
  30. {
  31. if (file_exists($path = $path.$file.EXT))
  32. {
  33. require_once $path;
  34. return;
  35. }
  36. }
  37. }
  38. /**
  39. * Register a class alias with the auto-loader.
  40. *
  41. * Note: Aliases are lazy-loaded, so the aliased class will not be included until it is needed.
  42. *
  43. * @param string $alias
  44. * @param string $class
  45. * @return void
  46. */
  47. public static function alias($alias, $class)
  48. {
  49. static::$aliases[$alias] = $class;
  50. }
  51. /**
  52. * Register a path with the auto-loader.
  53. *
  54. * @param string $path
  55. * @return void
  56. */
  57. public static function path($path)
  58. {
  59. static::$paths[] = rtrim($path, '/').'/';
  60. }
  61. /**
  62. * Remove an alias from the auto-loader's alias registrations.
  63. *
  64. * @param string $alias
  65. * @return void
  66. */
  67. public static function forget_alias($alias)
  68. {
  69. unset(static::$aliases[$alias]);
  70. }
  71. }