/
autoloader.php
182 lines (133 loc) · 3.92 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
180
181
182
<?php
class Autoload
{
const CACHE_FILE = "autoload.cache";
public static $directories = array();
protected static $cached = array();
protected static $cached_diff_add = array();
protected static $cached_diff_remove = array();
public static function init ($directories) {
self::$directories = (array) $directories;
self::init_cache();
spl_autoload_register("Autoload::cache", false);
spl_autoload_register("Autoload::normal", false);
spl_autoload_register("Autoload::wrapper", false);
register_shutdown_function("Autoload::write_cache");
}
public static function write_cache () {
if (empty(self::$cached_diff_remove)) {
if (empty(self::$cached_diff_add)) {
// Нечего добавить или убавить, не трогаем файл.
return;
} else {
// Нечего убавлять, достаточно дописать новое.
$data = array_diff_key(self::$cached_diff_add, self::$cached);
$flag = FILE_APPEND;
}
} else {
// Надо удалить строки, проще пересобрать весь файл
// Что и делаем.
$data = array_diff_key(self::$cached, self::$cached_diff_remove);
$data = array_merge($data, self::$cached_diff_add);
$flag = 0;
}
if (empty($data)) {
return;
}
$text = "";
$cache_file = ROOT_DIR.SL.'files'.SL.self::CACHE_FILE;
foreach ($data as $name => $file) {
$text .= $name."=".$file.PHP_EOL;
}
file_put_contents($cache_file, $text, $flag);
}
public static function init_cache () {
$cache_file = ROOT_DIR.SL.'files'.SL.self::CACHE_FILE;
if (file_exists($cache_file)) {
$data = file($cache_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
} else {
touch($cache_file);
$data = array();
}
foreach ($data as $line) {
list($name, $file) = explode("=", $line);
self::$cached[$name] = $file;
}
}
public static function cache ($class) {
$class = strtolower($class);
if (array_key_exists($class, self::$cached)) {
$file = self::$cached[$class];
if (file_exists($file)) {
include_once($file);
return true;
} else {
self::$cached_diff_remove[$class] = $file;
}
}
return false;
}
public static function normal ($class) {
$class = strtolower($class);
$name = preg_replace("/_+/", SL, $class).".php";
if ($library = self::search_lib($name)) {
self::$cached_diff_add[$class] = $library;
include_once($library);
return true;
}
$alt_name = preg_replace("/^(.+)\\".SL."(.+?)$/", "$1_$2", $name);
while ($alt_name != $name) {
if ($library = self::search_lib($alt_name)) {
self::$cached_diff_add[$class] = $library;
include_once($library);
return true;
}
$name = $alt_name;
$alt_name = preg_replace("/^(.+)\\".SL."(.+?)$/", "$1_$2", $name);
}
return false;
}
public static function wrapper ($class) {
$class = strtolower($class);
$name = preg_replace("/_+/", SL, $class).SL."wrapper.php";
if ($library = self::search_lib($name)) {
self::$cached_diff_add[$class] = $library;
include_once($library);
return true;
}
return false;
}
protected static function search_lib ($name) {
foreach (self::$directories as $directory) {
if (file_exists($directory.SL.$name)) {
return $directory.SL.$name;
}
}
return false;
}
}
Autoload::init(array(
ROOT_DIR.SL.'libs',
ENGINE.SL.'external',
));
function autoload_old ($class_name) {
$class = ROOT_DIR.SL.'libs'.SL.str_replace('__',SL,$class_name).'.php';
if (file_exists($class)) {
include_once $class;
return true;
}
$class = ROOT_DIR.SL.'engine'.SL.str_replace('__',SL,$class_name).'.php';
if (file_exists($class)) {
include_once $class;
return true;
}
if (!preg_match('/^Read_/i', $class_name) && !preg_match('/^Api_/i', $class_name)) {
if (_TYPE_ == 'index') {
include_once TEMPLATE_DIR.SL.'404'.SL.'fatal.php';
ob_end_flush();
}
exit();
}
return false;
}
spl_autoload_register('autoload_old', false);