forked from PHPCSStandards/PHP_CodeSniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoload.php
71 lines (54 loc) · 2 KB
/
autoload.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
<?php
namespace PHP_CodeSniffer;
class Autoload {
private static $loadedClasses = array();
private static $loadedFiles = array();
public static function load($class)
{
$ds = DIRECTORY_SEPARATOR;
$path = null;
if (substr($class, 0, 16) === 'PHP_CodeSniffer\\') {
if (substr($class, 0, 22) === 'PHP_CodeSniffer\Tests\\') {
$path = __DIR__.$ds.'tests'.$ds.substr(str_replace('\\', $ds, $class), 22).'.php';
} else {
$path = __DIR__.$ds.'src'.$ds.substr(str_replace('\\', $ds, $class), 16).'.php';
}
}
if ($path !== null && is_file($path) === true) {
self::loadFile($path);
return true;
}
return false;
}
public static function loadFile($path)
{
if (isset(self::$loadedClasses[$path]) === true) {
return self::$loadedClasses[$path];
}
$classes = get_declared_classes();
$interfaces = get_declared_interfaces();
include $path;
$className = array_pop(array_diff(get_declared_classes(), $classes));
if ($className === null) {
$className = array_pop(array_diff(get_declared_interfaces(), $interfaces));
}
self::$loadedClasses[$path] = $className;
self::$loadedFiles[$className] = $path;
return self::$loadedClasses[$path];
}
public static function getLoadedClassName($file)
{
if (isset(self::$loadedClasses[$file]) === false) {
throw new \Exception("Cannot get class name for $file; file has not been included");
}
return self::$loadedClasses[$file];
}
public static function getLoadedFileName($class)
{
if (isset(self::$loadedFiles[$class]) === false) {
throw new \Exception("Cannot get file name for $class; class has not been included");
}
return self::$loadedFiles[$class];
}
}
spl_autoload_register(__NAMESPACE__.'\Autoload::load', true, true);