autoloader.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php namespace Laravel;
  2. class Autoloader {
  3. /**
  4. * The PSR-0 compliant libraries registered with the auto-loader.
  5. *
  6. * @var array
  7. */
  8. protected static $libraries = array();
  9. /**
  10. * The paths to be searched by the auto-loader.
  11. *
  12. * @var array
  13. */
  14. protected static $paths = array(BASE_PATH, MODEL_PATH, LIBRARY_PATH);
  15. /**
  16. * Load the file corresponding to a given class.
  17. *
  18. * @param string $class
  19. * @return void
  20. */
  21. public static function load($class)
  22. {
  23. // Most of the core classes are aliases for convenient access in spite of
  24. // the namespace. If an alias is defined for the class, we will load the
  25. // alias and bail out of the auto-load method.
  26. if (array_key_exists($class, Config::$items['application']['aliases']))
  27. {
  28. return class_alias(Config::$items['application']['aliases'][$class], $class);
  29. }
  30. $file = str_replace('\\', '/', $class);
  31. $namespace = substr($class, 0, strpos($class, '\\'));
  32. // If the class namespace exists in the libraries array, it means that the
  33. // library is PSR-0 compliant, and we will load it following those standards.
  34. // This allows us to add many third-party libraries to an application and be
  35. // able to auto-load them automatically.
  36. if (array_key_exists($namespace, static::$libraries))
  37. {
  38. require LIBRARY_PATH.str_replace('_', '/', $file);
  39. }
  40. foreach (static::$paths as $path)
  41. {
  42. if (file_exists($path = $path.strtolower($file).EXT))
  43. {
  44. require $path;
  45. return;
  46. }
  47. }
  48. // If the namespace exists in the libraries directory, we will assume the
  49. // library is PSR-0 compliant, and will add the namespace to the array of
  50. // libraries and load the class accordingly.
  51. if (is_dir(LIBRARY_PATH.$namespace))
  52. {
  53. static::$libraries[] = $namespace;
  54. require LIBRARY_PATH.str_replace('_', '/', $file);
  55. }
  56. }
  57. }