autoload.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. class ClassLoader {
  3. private $searchPath = [];
  4. /**
  5. * Register path in which to look for a class.
  6. */
  7. public function registerPath($path) {
  8. if (is_string($path)) {
  9. $this->searchPath[] = $path;
  10. }
  11. if (is_array($path)) {
  12. array_push($this->searchPath, ...$path);
  13. }
  14. }
  15. /**
  16. * Load class file if found.
  17. */
  18. public function loadClass($class)
  19. {
  20. if ($file = $this->findFile($class)) {
  21. require $file;
  22. }
  23. }
  24. /**
  25. * Find the file containing the class definition.
  26. */
  27. public function findFile($class) {
  28. // This match most of classes directly
  29. foreach ($this->searchPath as $path) {
  30. $file = $path . DIRECTORY_SEPARATOR . str_replace(['\\', '_'], DIRECTORY_SEPARATOR, $class) . '.php';
  31. if (file_exists($file)) {
  32. return $file;
  33. }
  34. }
  35. // This match FRSS model classes
  36. $freshrssClass = str_replace('FreshRSS_', '', $class);
  37. foreach ($this->searchPath as $path) {
  38. $file = $path . DIRECTORY_SEPARATOR . str_replace(['\\', '_'], DIRECTORY_SEPARATOR, $freshrssClass) . '.php';
  39. if (file_exists($file)) {
  40. return $file;
  41. }
  42. }
  43. // This match FRSS other classes
  44. list(, $classType) = explode('_', $freshrssClass);
  45. foreach ($this->searchPath as $path) {
  46. $file = $path . DIRECTORY_SEPARATOR . $classType . 's' . DIRECTORY_SEPARATOR . str_replace('_', '', $freshrssClass) . '.php';
  47. if (file_exists($file)) {
  48. return $file;
  49. }
  50. }
  51. }
  52. /**
  53. * Register the current loader in the autoload queue.
  54. */
  55. public function register($prepend = false) {
  56. spl_autoload_register([$this, 'loadClass'], true, $prepend);
  57. }
  58. }
  59. $loader = new ClassLoader();
  60. $loader->registerPath([
  61. APP_PATH,
  62. APP_PATH . DIRECTORY_SEPARATOR . 'Models',
  63. LIB_PATH,
  64. LIB_PATH . DIRECTORY_SEPARATOR . 'SimplePie',
  65. ]);
  66. $loader->register();