forked from yakamara/redaxo_prozer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoload.php
306 lines (273 loc) · 9.48 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
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
<?php
/**
* REDAXO Autoloader.
*
* This class was originally copied from the Symfony Framework:
* Fabien Potencier <fabien.potencier@symfony-project.com>
*
* Adjusted in very many places
*
* @package redaxo\core
*/
class rex_autoload
{
/**
* @var Composer\Autoload\ClassLoader
*/
protected static $composerLoader;
protected static $registered = false;
protected static $cacheFile = null;
protected static $cacheChanged = false;
protected static $reloaded = false;
protected static $dirs = [];
protected static $addedDirs = [];
protected static $classes = [];
/**
* Register rex_autoload in spl autoloader.
*/
public static function register()
{
if (self::$registered) {
return;
}
ini_set('unserialize_callback_func', 'spl_autoload_call');
if (!self::$composerLoader) {
self::$composerLoader = require rex_path::addon('prozer', 'vendor/autoload.php');
// Unregister Composer Autoloader because we call self::$composerLoader->loadClass() manually
self::$composerLoader->unregister();
}
if (false === spl_autoload_register([__CLASS__, 'autoload'])) {
throw new Exception(sprintf('Unable to register %s::autoload as an autoloading method.', __CLASS__));
}
self::$cacheFile = rex_path::cache('autoload.cache');
self::loadCache();
register_shutdown_function([__CLASS__, 'saveCache']);
self::$registered = true;
}
/**
* Unregister rex_autoload from spl autoloader.
*/
public static function unregister()
{
spl_autoload_unregister([__CLASS__, 'autoload']);
self::$registered = false;
}
/**
* Handles autoloading of classes.
*
* @param string $class A class name.
*
* @return bool Returns true if the class has been loaded
*/
public static function autoload($class)
{
// class already exists
if (self::classExists($class)) {
return true;
}
$force = false;
$lowerClass = strtolower($class);
if (isset(self::$classes[$lowerClass])) {
// we have a class path for the class, let's include it
if (is_readable(self::$classes[$lowerClass])) {
require_once self::$classes[$lowerClass];
if (self::classExists($class)) {
return true;
}
}
// there is a class path in cache, but the file does not exist or does not contain the class any more
// but maybe the class exists in another already known file now
// so all files have to be analysed again => $force reload
$force = true;
unset(self::$classes[$lowerClass]);
self::$cacheChanged = true;
}
// Return true if class exists after calling $composerLoader
if (self::$composerLoader->loadClass($class) && self::classExists($class)) {
return true;
}
// Class not found, so reanalyse all directories if not already done or if $force==true
// but only if an admin is logged in
/*
if ((!self::$reloaded || $force) && ($user = rex_backend_login::createUser()) && $user->isAdmin()) {
self::reload($force);
return self::autoload($class);
}
*/
return false;
}
/**
* Returns whether the given class/interface/trait exists.
*
* @param string $class
*
* @return bool
*/
private static function classExists($class)
{
return class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false);
}
/**
* Loads the cache.
*/
private static function loadCache()
{
if (!self::$cacheFile || !is_readable(self::$cacheFile)) {
return;
}
list(self::$classes, self::$dirs) = json_decode(file_get_contents(self::$cacheFile), true);
}
/**
* Saves the cache.
*/
public static function saveCache()
{
if (!self::$cacheChanged) {
return;
}
if (!is_writable(dirname(self::$cacheFile))) {
throw new Exception("Unable to write autoload cachefile '" . self::$cacheFile . "'!");
}
// remove obsolete dirs from cache
foreach (self::$dirs as $dir => $files) {
if (!in_array($dir, self::$addedDirs)) {
unset(self::$dirs[$dir]);
}
}
file_put_contents(self::$cacheFile, json_encode([self::$classes, self::$dirs]));
self::$cacheChanged = false;
}
/**
* Reanalyses all added directories.
*
* @param bool $force If true, all files are reanalysed, otherwise only new and changed files
*/
public static function reload($force = false)
{
if ($force) {
self::$classes = [];
self::$dirs = [];
}
foreach (self::$addedDirs as $dir) {
self::_addDirectory($dir);
}
self::$reloaded = true;
}
/**
* Removes the cache.
*/
public static function removeCache()
{
rex_file::delete(self::$cacheFile);
}
/**
* Adds a directory to the autoloading system if not yet present.
*
* @param string $dir The directory to look for classes
*/
public static function addDirectory($dir)
{
$dir = rtrim($dir, '/\\') . DIRECTORY_SEPARATOR;
if (in_array($dir, self::$addedDirs)) {
return;
}
self::$addedDirs[] = $dir;
if (!isset(self::$dirs[$dir])) {
self::_addDirectory($dir);
self::$cacheChanged = true;
}
}
/**
* @param string $dir
*/
private static function _addDirectory($dir)
{
if (!is_dir($dir)) {
return;
}
if (!isset(self::$dirs[$dir])) {
self::$dirs[$dir] = [];
}
$files = self::$dirs[$dir];
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS));
foreach ($iterator as $path => $file) {
/** @var SplFileInfo $file */
if (!$file->isFile() || !in_array($file->getExtension(), ['php', 'inc'])) {
continue;
}
unset($files[$path]);
$checksum = md5_file($path);
if (isset(self::$dirs[$dir][$path]) && self::$dirs[$dir][$path] === $checksum) {
continue;
}
self::$dirs[$dir][$path] = $checksum;
self::$cacheChanged = true;
$classes = self::findClasses($path);
foreach ($classes as $class) {
$class = strtolower($class);
if (!isset(self::$classes[$class])) {
self::$classes[$class] = $path;
}
}
}
foreach ($files as $path) {
unset(self::$dirs[$path]);
self::$cacheChanged = true;
}
}
/**
* Extract the classes in the given file.
*
* The method is copied from Composer (with little changes):
* https://github.com/composer/composer/blob/a2a70380c14a20b3f611d849eae7342f2e35c763/src/Composer/Autoload/ClassMapGenerator.php#L89-L146
*
* @param string $path The file to check
*
* @throws \RuntimeException
*
* @return array The found classes
*/
private static function findClasses($path)
{
try {
$contents = php_strip_whitespace($path);
} catch (\Exception $e) {
throw new \RuntimeException('Could not scan for classes inside ' . $path . ": \n" . $e->getMessage(), 0, $e);
}
// return early if there is no chance of matching anything in this file
if (!preg_match('{\b(?:class|interface|trait)\s}i', $contents)) {
return [];
}
// strip heredocs/nowdocs
$contents = preg_replace('{<<<\'?(\w+)\'?(?:\r\n|\n|\r)(?:.*?)(?:\r\n|\n|\r)\\1(?=\r\n|\n|\r|;)}s', 'null', $contents);
// strip strings
$contents = preg_replace('{"[^"\\\\]*(\\\\.[^"\\\\]*)*"|\'[^\'\\\\]*(\\\\.[^\'\\\\]*)*\'}s', 'null', $contents);
// strip leading non-php code if needed
if (substr($contents, 0, 2) !== '<?') {
$contents = preg_replace('{^.+?<\?}s', '<?', $contents);
}
// strip non-php blocks in the file
$contents = preg_replace('{\?>.+<\?}s', '?><?', $contents);
// strip trailing non-php code if needed
$pos = strrpos($contents, '?>');
if (false !== $pos && false === strpos(substr($contents, $pos), '<?')) {
$contents = substr($contents, 0, $pos);
}
preg_match_all('{
(?:
\b(?<![\$:>])(?P<type>class|interface|trait) \s+ (?P<name>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)
| \b(?<![\$:>])(?P<ns>namespace) (?P<nsname>\s+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(?:\s*\\\\\s*[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*)? \s*[\{;]
)
}ix', $contents, $matches);
$classes = [];
$namespace = '';
for ($i = 0, $len = count($matches['type']); $i < $len; $i++) {
if (!empty($matches['ns'][$i])) {
$namespace = str_replace([' ', "\t", "\r", "\n"], '', $matches['nsname'][$i]) . '\\';
} else {
$classes[] = ltrim($namespace . $matches['name'][$i], '\\');
}
}
return $classes;
}
}