-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaLoad.class.php
77 lines (64 loc) · 1.73 KB
/
aLoad.class.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
<?php
/*
aLoad
(c) 2016 by THIELICIOUS
thielicious.github.com
----------------------
USAGE:
aload::register([modules], path);
EXAMPLE:
aload::register(["class", "inc"], "scripts/");
This example above will register all PHP files containing class and inc in the folder scripts.
Use "all" as the first parameter to register all PHP scripts in the specified folder.
*/
class aLoad {
private
$dir, $mod;
const
ERR = "[!]Error ".__CLASS__.": ",
ALL = "all";
function __construct($mod, string $dir = null) {
if (is_array($mod)) {
foreach ($mod as $each) {
$this->mod[] = $each;
}
} else if ($mod == "all") {
$this->mod = self::ALL;
}
if (!is_null($dir)) {
if (@scandir($dir)) {
$this->dir($dir);
} else {
die(self::ERR."Directory name <u>".str_replace("/", "", $dir)."</u> not found.");
}
}
spl_autoload_register(array($this, "load_class"));
}
private function dir(string $dir) {
$this->dir = $dir;
}
public static function register($modules, string $dir) {
new aLoad($modules, $dir);
}
private function load_class(string $class_name) {
$scripts = function($module = null) use ($class_name) {
$mod = $module ? ".".$module : null;
$file = $this->dir.strtolower(str_replace("\\", "/", $class_name)).$mod.".php";
return file_exists($file) ? require_once($file) : null;
};
if ($this->mod != null) {
if (is_array($this->mod)) {
foreach ($this->mod as $mod) {
$scripts($mod);
}
} else if ($this->mod == self::ALL) {
$scripts();
} else {
die(self::ERR."Unknown parameter for module.");
}
} else {
die(self::ERR."No module exists, please set a module first.");
}
}
}
?>