-
Notifications
You must be signed in to change notification settings - Fork 1
/
autoloader.php
58 lines (43 loc) · 1.32 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
<?php
namespace TemporaryLogin;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Autoloader {
private static $default_path;
private static $default_namespace;
public static function run( $default_path = '', $default_namespace = '' ) : void {
if ( '' === $default_path ) {
$default_path = TEMPORARY_LOGIN_PATH;
}
if ( '' === $default_namespace ) {
$default_namespace = __NAMESPACE__;
}
self::$default_path = $default_path;
self::$default_namespace = $default_namespace;
spl_autoload_register( [ __CLASS__, 'autoload' ] );
}
private static function load_class( $relative_class_name ) : void {
$filename = strtolower(
preg_replace(
[ '/([a-z])([A-Z])/', '/_/', '/\\\/' ],
[ '$1-$2', '-', DIRECTORY_SEPARATOR ],
$relative_class_name
)
);
$filename = self::$default_path . $filename . '.php';
if ( is_readable( $filename ) ) {
require $filename;
}
}
private static function autoload( $class ) : void {
if ( 0 !== strpos( $class, self::$default_namespace . '\\' ) ) {
return;
}
$relative_class_name = preg_replace( '/^' . self::$default_namespace . '\\\/', '', $class );
$final_class_name = self::$default_namespace . '\\' . $relative_class_name;
if ( ! class_exists( $final_class_name ) ) {
self::load_class( $relative_class_name );
}
}
}