-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathautoload.php
More file actions
64 lines (50 loc) · 1.45 KB
/
Copy pathautoload.php
File metadata and controls
64 lines (50 loc) · 1.45 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
<?php
class PhpParserLoader
{
public static function register()
{
if (!static::isAlreadyRegistered()) {
\spl_autoload_register(array(__CLASS__, "autoLoad"), true);
}
}
public static function autoLoad($className)
{
$className = ltrim($className, "\\"); // fix web env
$className = str_replace("\\", '/', $className);
if (preg_match("#[^\\\\/a-zA-Z0-9_]#", $className)) {
return;
}
$fileParts = explode("/", $className);
if (count($fileParts) < 2) {
return;
}
$firstNamespace = mb_strtolower($fileParts[0]);
$secondNamespace = mb_strtolower($fileParts[1]);
if (
$firstNamespace === "phpparser"
) {
$filePath = __DIR__ . "/lib/" . implode("/", $fileParts) . ".php";
if (file_exists($filePath)) {
require_once($filePath);
}
}
}
private static function isAlreadyRegistered()
{
$autoLoaders = spl_autoload_functions();
if (!$autoLoaders) {
return false;
}
foreach ($autoLoaders as $autoLoader) {
if (!is_array($autoLoader)) {
continue;
}
list($className, $method) = $autoLoader;
if ($className === __CLASS__) {
return true;
}
}
return false;
}
}
PhpParserLoader::register();