-
Notifications
You must be signed in to change notification settings - Fork 0
/
aLoad.class.php
84 lines (69 loc) · 2.06 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
78
79
80
81
82
83
84
<?php
/*
aLoad v1.4
(c) 2016 by THIELICIOUS
thielicious.github.com
----------------------
USAGE:
aLoad::register(array [modules] || aLoad::ALL, folderpath);
EXAMPLE:
aLoad::register(["class", "inc"], "scripts/");
This example above will register all PHP files containing "class" and "inc" in the folder "scripts".
Use the constant "aLoad::ALL" as the first parameter to register PHP scripts without modules. Feel free
to remove the namespace here.
*/
namespace Thielicious\utils\aLoad;
class aLoad {
private
$dir, $mod;
const
ERR = "[!]Error ".__CLASS__.": ",
ALL = 1;
function __construct($mod, string $dir = null) {
if (is_array($mod)) {
foreach ($mod as $each) {
$this->mod[] = $each;
}
} elseif ($mod == self::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 = null) {
new aLoad($modules, $dir);
}
private function load_class(string $class_name) {
$scripts = function($module = null) use ($class_name) {
$class_name = preg_match("/(?!\S+\/)[^\/\s]\S+\\\/", $class_name, $m) ?
str_replace($m[0], "", $class_name) : $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);
}
} elseif ($this->mod == self::ALL) {
$scripts();
} else {
die(self::ERR."Unknown parameter for module.");
}
} else {
die(self::ERR."Please set a module first.");
}
}
}
?>