autoloader.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. // The PSR-0 standard is exactly like the typical Laravel standard, the only
  55. // difference being that Laravel files are all lowercase, while PSR-0 states
  56. // that the file name should match the class name.
  57. if (isset(static::$libraries[$namespace]))
  58. {
  59. return str_replace('_', '/', $file).EXT;
  60. }
  61. foreach (static::$paths as $path)
  62. {
  63. if (file_exists($path = $path.strtolower($file).EXT))
  64. {
  65. return $path;
  66. }
  67. }
  68. // If we could not find the class file in any of the auto-loaded locations
  69. // according to the Laravel naming standard, we will search the libraries
  70. // directory for the class according to the PSR-0 naming standard. If the
  71. // file exists, we will add the class namespace to the array of registered
  72. // libraries that are loaded following the PSR-0 standard.
  73. if (file_exists($path = LIBRARY_PATH.str_replace('_', '/', $file).EXT))
  74. {
  75. static::$libraries[] = $namespace;
  76. return $path;
  77. }
  78. }
  79. }