-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathautoloader.php
179 lines (146 loc) · 4.29 KB
/
autoloader.php
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
namespace Brisko;
use Exception;
class Autoloader
{
/**
* @var array Namespace to directory mappings
*/
private static array $prefixes = [];
/**
* @var array Cached file paths to improve performance
*/
private static array $classMap = [];
/**
* @var array Files that need to be included manually
*/
private static array $fileMap = [];
/**
* @var null|Autoloader Singleton instance
*/
private static ?Autoloader $instance = null;
/**
* Private constructor to enforce singleton.
*/
private function __construct()
{
spl_autoload_register([$this, 'autoload'], true, true);
}
/**
* Prevents cloning the instance.
*/
private function __clone()
{
}
/**
* Prevents unserialization.
*/
public function __wakeup()
{
throw new Exception("Cannot unserialize singleton class.");
}
/**
* Get the singleton instance.
*
* @return Autoloader
*/
public static function init(): self
{
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Adds a namespace prefix with the corresponding base directory.
*
* @param string $prefix The namespace prefix
* @param string $baseDir The base directory for class files
* @param bool $prepend If true, prepends the base directory (higher priority)
*
* @throws Exception
*
* @return self
*/
public function addNamespace(string $prefix, ?string $baseDir = null, bool $prepend = false): self
{
$prefix = trim($prefix, '\\') . '\\';
$baseDir = realpath(rtrim( __DIR__ . DIRECTORY_SEPARATOR . $baseDir, DIRECTORY_SEPARATOR));
if ( ! $baseDir || ! is_dir($baseDir)) {
throw new Exception("Invalid base directory: $baseDir");
}
if ( ! isset(self::$prefixes[$prefix])) {
self::$prefixes[$prefix] = [];
}
if ($prepend) {
array_unshift(self::$prefixes[$prefix], $baseDir . DIRECTORY_SEPARATOR);
} else {
self::$prefixes[$prefix][] = $baseDir . DIRECTORY_SEPARATOR;
}
return $this;
}
/**
* Includes a single non-class file safely.
*
* @param string $filePath Absolute or relative path to the file
*
* @return self
*/
public function addFile(string $filePath): self
{
$realPath = realpath(__DIR__ . DIRECTORY_SEPARATOR . $filePath);
if ( ! $realPath || ! is_file($realPath) || ! is_readable($realPath)) {
error_log("[Autoloader] ERROR: Unable to include file $filePath");
return $this;
}
if ( ! \in_array($realPath, self::$fileMap, true)) {
self::$fileMap[] = $realPath;
require_once $realPath;
}
return $this;
}
/**
* Includes multiple non-class files at once.
*
* @param array $files Array of file paths
*
* @return self
*/
public function addFiles(array $files): self
{
foreach ($files as $file) {
$this->addFile($file);
}
return $this;
}
/**
* Autoloads a class file based on its namespace.
*
* @param string $class Fully qualified class name
*/
private function autoload(string $class): void
{
$class = ltrim($class, '\\');
// Check cache first
if (isset(self::$classMap[$class])) {
require_once self::$classMap[$class];
return;
}
foreach (self::$prefixes as $prefix => $baseDirs) {
if (0 === strpos($class, $prefix)) {
$relativeClass = substr($class, \strlen($prefix));
$relativeClass = str_replace('\\', DIRECTORY_SEPARATOR, $relativeClass) . '.php';
foreach ($baseDirs as $baseDir) {
$file = $baseDir . $relativeClass;
if (is_readable($file)) {
self::$classMap[$class] = $file;
require_once $file;
return;
}
}
}
}
error_log("[Autoloader] ERROR: Unable to load class $class");
}
}
Autoloader::init();