forked from PHPCSStandards/PHP_CodeSniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoload.php
170 lines (135 loc) · 4.64 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
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
<?php
/**
* Autoloads files for PHP_CodeSniffer and tracks what has been loaded.
*
* Due to different namespaces being used for custom coding standards,
* the autoloader keeps track of what class is loaded after a file is included,
* even if the file is ultimately included by another autoloader (such as composer).
*
* This allows PHP_CodeSniffer to request the class name after loading a class
* when it only knows the filename, without having to parse the file to find it.
*
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer;
class Autoload
{
/**
* A mapping of file names to class names.
*
* @var array<string, string>
*/
private static $loadedClasses = array();
/**
* A mapping of class names to file names.
*
* @var array<string, string>
*/
private static $loadedFiles = array();
/**
* Loads a class.
*
* This method only loads classes that exist in the PHP_CodeSniffer namespace.
* All other classes are ignored and loaded by subsequent autoloaders.
*
* @param string $class The name of the class to load.
*
* @return bool
*/
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;
}//end load()
/**
* Includes a file and tracks what class or interface was loaded as a result.
*
* @param string $path The path of the file to load.
*
* @return string The fully qualified name of the class in the loaded file.
*/
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;
$newClasses = array_diff(get_declared_classes(), $classes);
$className = array_pop($newClasses);
if ($className === null) {
$newClasses = array_diff(get_declared_interfaces(), $classes);
$className = array_pop($newClasses);
}
self::$loadedClasses[$path] = $className;
self::$loadedFiles[$className] = $path;
return self::$loadedClasses[$path];
}//end loadFile()
/**
* Gets the class name for the given file path.
*
* @param string $path The name of the file.
*
* @throws \Exception If the file path has not been loaded.
* @return string
*/
public static function getLoadedClassName($path)
{
if (isset(self::$loadedClasses[$path]) === false) {
throw new \Exception("Cannot get class name for $path; file has not been included");
}
return self::$loadedClasses[$path];
}//end getLoadedClassName()
/**
* Gets the file path for the given class name.
*
* @param string $class The name of the class.
*
* @throws \Exception If the class name has not been loaded
* @return string
*/
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];
}//end getLoadedFileName()
/**
* Gets the mapping of file names to class names.
*
* @return array<string, string>
*/
public static function getLoadedClasses()
{
return self::$loadedClasses;
}//end getLoadedClasses()
/**
* Gets the mapping of class names to file names.
*
* @return array<string, string>
*/
public static function getLoadedFiles()
{
return self::$loadedFiles;
}//end getLoadedFiles()
}//end class
// Register the autoloader before any existing autoloaders to ensure
// it gets a chance to hear about every autoload request, and record
// the file and class name for it.
spl_autoload_register(__NAMESPACE__.'\Autoload::load', true, true);