-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoload.php
38 lines (29 loc) · 882 Bytes
/
Autoload.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
<?php
declare(strict_types=1);
final class Autoload
{
private const CLASS_MAP = [
'App' => __DIR__ . '/src/',
];
static function register(): void
{
spl_autoload_register(function (string $className) {
$parts = explode('\\', $className);
$namespace = array_shift($parts);
$classFile = array_pop($parts) . '.php';
if (!array_key_exists($namespace, self::CLASS_MAP)) {
return;
}
$path = implode(DIRECTORY_SEPARATOR, $parts);
$file = self::CLASS_MAP[$namespace] . $path . DIRECTORY_SEPARATOR . $classFile;
if (!file_exists($file) && !class_exists($className)) {
return;
}
require_once $file;
});
}
private function __construct()
{
// This is a static class
}
}