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