-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.php
More file actions
310 lines (228 loc) · 7.44 KB
/
index.php
File metadata and controls
310 lines (228 loc) · 7.44 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
namespace php_require;
/*
A Class that provides a nodejs style module loader so PHP can use npm.
(This may not be a good idea!).
*/
class Module {
/*
Used as a semaphore while loading core modules required by php-require.
*/
public static $coreModulesLoaded = false;
/*
List of core modules to load before php-require can be used.
*/
public static $coreModules = array("php-path");
/*
Cache of the loaded modules.
*/
private static $cache = array();
/*
Map of supported extension loaders.
*/
public static $extensions = array();
/*
Paths
*/
private $paths = array();
/*
The actual module.
*/
public $id = null;
/*
The actual module.
*/
public $exports = array();
/*
Absolute path to the file.
*/
public $filename = null;
/*
Modules parent module.
*/
public $parent = null;
/*
Has this module been loaded.
*/
public $loaded = false;
/*
Array of children;
*/
public $children = array();
/*
Construct.
*/
function __construct($filename, $parent) {
if (Module::$coreModulesLoaded === false) {
/*
This code block is only called once.
*/
Module::$coreModulesLoaded = "loading";
$this->loadNativeModules();
Module::$coreModulesLoaded = true;
}
if (!$filename) {
return;
}
$this->id = $filename;
$this->parent = $parent;
$this->filename = $filename;
if (Module::$coreModulesLoaded === true) {
/*
We can only call Module::nodeModulePaths() once all core modules have loaded.
*/
$this->paths = Module::nodeModulePaths(dirname($filename));
}
if ($parent) {
array_push($parent->children, $this);
}
}
/*
Loads core modules.
*/
private function loadNativeModules() {
foreach (Module::$coreModules as $request) {
$filename = __DIR__ . DIRECTORY_SEPARATOR . "lib" . DIRECTORY_SEPARATOR . $request . ".php";
$module = new Module($filename, $this);
Module::$cache[$filename] = $module;
$module->compile();
$module->loaded = true;
}
}
/*
Resolve the module filename.
Refactor required.
*/
private static function resolveFilename($request, $parent) {
$pathlib = Module::loadModule("php-path");
$paths = array();
if ($request[0] == DIRECTORY_SEPARATOR) {
// If $request starts with a "/" then do nothing.
$paths = array($request);
} else if ($request[0] == ".") {
// If $request starts with a "." then resolve it.
$paths = array($pathlib->join($pathlib->dirname($parent->filename), $request));
} else {
// If request starts with neither then check the parent.
foreach ($parent->paths as $path) {
array_push($paths, $pathlib->join($path, $request));
}
}
foreach ($paths as $root) {
$abspath = $pathlib->normalize($root . ".php");
if (file_exists($abspath)) {
return $abspath;
}
$abspath = $pathlib->join($root, "index.php");
if (file_exists($abspath)) {
return $abspath;
}
$abspath = $pathlib->normalize($root);
if (file_exists($abspath)) {
return $abspath;
}
}
// If a file cannot be found return the original $request.
return $request;
}
/*
Generate a list of all possible paths modules could be found at.
*/
private static function nodeModulePaths($from) {
$pathlib = Module::loadModule("php-path");
// guarantee that 'from' is absolute.
$from = $pathlib->normalize($from);
// note: this approach *only* works when the path is guaranteed
// to be absolute. Doing a fully-edge-case-correct path.split
// that works on both Windows and Posix is non-trivial.
$paths = array();
$parts = explode(DIRECTORY_SEPARATOR, $from);
$pos = 0;
foreach ($parts as $tip) {
if ($tip != "node_modules") {
$dir = implode(DIRECTORY_SEPARATOR, array_merge(array_slice($parts, 0, $pos + 1), array("node_modules")));
array_push($paths, $dir);
}
$pos = $pos + 1;
}
return array_reverse($paths);
}
/*
Load a module (this is the require function).
*/
public static function loadModule($request, $parent=null, $isMain=false) {
if (in_array($request, Module::$coreModules)) {
$filename = __DIR__ . DIRECTORY_SEPARATOR . "lib" . DIRECTORY_SEPARATOR . $request . ".php";
} else {
$filename = Module::resolveFilename($request, $parent);
}
if (isset(Module::$cache[$filename])) {
return Module::$cache[$filename]->exports;
}
$module = new Module($filename, $parent);
if ($isMain) {
$module->id = ".";
}
Module::$cache[$filename] = $module;
try {
$module->load();
} catch (\Exception $e) {
unset(Module::$cache[$filename]);
error_log("The module at " . $filename . " threw an Exception:\n" . $e);
}
return $module->exports;
}
/*
Find the module file.
*/
private function load() {
$pathlib = Module::loadModule("php-path");
if ($this->loaded) {
throw new \Exception("the module " . $this->filename . " has already been loaded.");
}
$extension = $pathlib->extname($this->filename);
if (!isset(Module::$extensions[$extension])) {
$extension = '.php';
}
call_user_func(Module::$extensions[$extension], $this, $this->filename);
$this->loaded = true;
}
/*
Compile the module file by requiring it in a closed scope.
*/
public function compile() {
$require = function ($request) {
return Module::loadModule($request, $this, false);
};
$__filename = $this->filename;
$__dirname = dirname($this->filename);
$fn = function ($__filename, $__dirname, &$exports, &$module, $require) {
if (is_file($__filename)) {
include($__filename);
} else {
// throw new \Exception("Module not found at: " . $__filename);
error_log("php-require: Module not found at: " . $__filename);
}
};
$fn($__filename, $__dirname, $this->exports, $this, $require);
}
}
/*
Extensions loader implementations.
*/
Module::$extensions[".php"] = function ($module, $filename) {
$module->compile();
};
Module::$extensions[".json"] = function ($module, $filename) {
$content = file_get_contents($filename);
$module->exports = json_decode($content, true);
};
/*
Setup the first $require() function.
*/
$__filename = $_SERVER["DOCUMENT_ROOT"] . $_SERVER["SCRIPT_NAME"];
$__dirname = dirname($__filename);
$require = function ($request) use($__filename) {
$parent = new Module($__filename, null);
return Module::loadModule($request, $parent, true);
};