loader.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php namespace System;
  2. class Loader {
  3. /**
  4. * The paths to be searched by the loader.
  5. *
  6. * @var array
  7. */
  8. private static $paths = array(BASE_PATH, MODEL_PATH, LIBRARY_PATH);
  9. /**
  10. * All of the class aliases.
  11. *
  12. * @var array
  13. */
  14. private static $aliases = array();
  15. /**
  16. * Bootstrap the auto-loader.
  17. *
  18. * @return void
  19. */
  20. public static function bootstrap()
  21. {
  22. static::$aliases = require CONFIG_PATH.'aliases'.EXT;
  23. }
  24. /**
  25. * Load a class file for a given class name.
  26. *
  27. * This function is registered on the SPL auto-loader stack by the front controller during each request.
  28. *
  29. * All Laravel class names follow a namespace to directory convention. So, if a class exists in
  30. * application/libraries/user, it shouold be placed in the "User" namespace.
  31. *
  32. * @param string $class
  33. * @return void
  34. */
  35. public static function load($class)
  36. {
  37. $file = strtolower(str_replace('\\', '/', $class));
  38. if (array_key_exists($class, static::$aliases))
  39. {
  40. return class_alias(static::$aliases[$class], $class);
  41. }
  42. foreach (static::$paths as $directory)
  43. {
  44. if (file_exists($path = $directory.$file.EXT))
  45. {
  46. require $path;
  47. return;
  48. }
  49. }
  50. }
  51. /**
  52. * Register a path with the auto-loader.
  53. *
  54. * @param string $path
  55. * @return void
  56. */
  57. public static function register($path)
  58. {
  59. static::$paths[] = $path;
  60. }
  61. }