autoloader.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. * Laravel loads classes out of three directories: the core "laravel" directory,
  19. * and the application "models" and "libraries" directories. All of the file
  20. * names are lower cased and the directory structure corresponds with the
  21. * class namespaces.
  22. *
  23. * The application "libraries" directory also supports the inclusion of PSR-0
  24. * compliant libraries. These libraries will be detected automatically and
  25. * will be loaded according to the PSR-0 naming conventions.
  26. *
  27. * @param string $class
  28. * @return void
  29. */
  30. public static function load($class)
  31. {
  32. if (array_key_exists($class, Config::$items['application']['aliases']))
  33. {
  34. return class_alias(Config::$items['application']['aliases'][$class], $class);
  35. }
  36. if ( ! is_null($path = static::find($class)))
  37. {
  38. require $path;
  39. }
  40. }
  41. /**
  42. * Determine the file path associated with a given class name.
  43. *
  44. * @param string $class
  45. * @return string
  46. */
  47. protected static function find($class)
  48. {
  49. $file = str_replace('\\', '/', $class);
  50. $namespace = substr($class, 0, strpos($class, '\\'));
  51. // If the namespace has been registered as a PSR-0 compliant library, we will
  52. // load the library according to the PSR-0 naming standards, which state that
  53. // namespaces and underscores indicate the directory hierarchy of the class.
  54. //
  55. // The PSR-0 standard is exactly like the typical Laravel standard, the only
  56. // difference being that Laravel files are all lowercase, while PSR-0 states
  57. // that the file name should match the class name.
  58. if (isset(static::$libraries[$namespace]))
  59. {
  60. return str_replace('_', '/', $file).EXT;
  61. }
  62. foreach (static::$paths as $path)
  63. {
  64. if (file_exists($path = $path.strtolower($file).EXT))
  65. {
  66. return $path;
  67. }
  68. }
  69. // If we could not find the class file in any of the auto-loaded locations
  70. // according to the Laravel naming standard, we will search the libraries
  71. // directory for the class according to the PSR-0 naming standard.
  72. if (file_exists($path = LIBRARY_PATH.str_replace('_', '/', $file).EXT))
  73. {
  74. static::$libraries[] = $namespace;
  75. return $path;
  76. }
  77. }
  78. }