-
-
Notifications
You must be signed in to change notification settings - Fork 237
/
RobotLoader.php
351 lines (278 loc) · 6.88 KB
/
RobotLoader.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
/**
* This file is part of the Nette Framework.
*
* Copyright (c) 2004, 2010 David Grudl (http://davidgrudl.com)
*
* This source file is subject to the "Nette license", and/or
* GPL license. For more information please see http://nette.org
*/
namespace Nette\Loaders;
use Nette,
Nette\String;
/**
* Nette auto loader is responsible for loading classes and interfaces.
*
* @author David Grudl
*/
class RobotLoader extends AutoLoader
{
/** @var array */
public $scanDirs;
/** @var string comma separated wildcards */
public $ignoreDirs = '.*, *.old, *.bak, *.tmp, temp';
/** @var string comma separated wildcards */
public $acceptFiles = '*.php, *.php5';
/** @var bool */
public $autoRebuild;
/** @var array */
private $list = array();
/** @var array */
private $files;
/** @var bool */
private $rebuilt = FALSE;
/**
*/
public function __construct()
{
if (!extension_loaded('tokenizer')) {
throw new \Exception("PHP extension Tokenizer is not loaded.");
}
}
/**
* Register autoloader.
* @return void
*/
public function register()
{
$cache = $this->getCache();
$key = $this->getKey();
if (isset($cache[$key])) {
$this->list = $cache[$key];
} else {
$this->rebuild();
}
if (isset($this->list[strtolower(__CLASS__)]) && class_exists('Nette\Loaders\NetteLoader', FALSE)) {
NetteLoader::getInstance()->unregister();
}
parent::register();
}
/**
* Handles autoloading of classes or interfaces.
* @param string
* @return void
*/
public function tryLoad($type)
{
$type = ltrim(strtolower($type), '\\'); // PHP namespace bug #49143
if (!isset($this->list[$type]) || ($this->list[$type] !== FALSE && !is_file($this->list[$type][0]))) {
$this->list[$type] = FALSE;
if ($this->autoRebuild === NULL) {
$this->autoRebuild = !$this->isProduction();
}
if ($this->autoRebuild) {
if ($this->rebuilt) {
$this->getCache()->save($this->getKey(), $this->list);
} else {
$this->rebuild();
}
}
if ($this->list[$type] !== FALSE) {
LimitedScope::load($this->list[$type][0]);
self::$count++;
}
} elseif ($this->list[$type] !== FALSE) {
LimitedScope::load($this->list[$type][0]);
self::$count++;
}
}
/**
* Rebuilds class list cache.
* @return void
*/
public function rebuild()
{
$this->getCache()->save($this->getKey(), callback($this, '_rebuildCallback'));
$this->rebuilt = TRUE;
}
/**
* @internal
*/
public function _rebuildCallback()
{
foreach ($this->list as $pair) {
if ($pair) $this->files[$pair[0]] = $pair[1];
}
foreach (array_unique($this->scanDirs) as $dir) {
$this->scanDirectory($dir);
}
$this->files = NULL;
return $this->list;
}
/**
* @return array of class => filename
*/
public function getIndexedClasses()
{
$res = array();
foreach ($this->list as $class => $pair) {
if ($pair) $res[$class] = $pair[0];
}
return $res;
}
/**
* Add directory (or directories) to list.
* @param string|array
* @return void
* @throws \DirectoryNotFoundException if path is not found
*/
public function addDirectory($path)
{
foreach ((array) $path as $val) {
$real = realpath($val);
if ($real === FALSE) {
throw new \DirectoryNotFoundException("Directory '$val' not found.");
}
$this->scanDirs[] = $real;
}
}
/**
* Add class and file name to the list.
* @param string
* @param string
* @param int
* @return void
*/
private function addClass($class, $file, $time)
{
$class = strtolower($class);
if (!empty($this->list[$class]) && $this->list[$class][0] !== $file && is_file($this->list[$class][0])) {
spl_autoload_call($class); // hack: enables exceptions
throw new \InvalidStateException("Ambiguous class '$class' resolution; defined in $file and in " . $this->list[$class][0] . ".");
}
$this->list[$class] = array($file, $time);
}
/**
* Scan a directory for PHP files, subdirectories and 'netterobots.txt' file.
* @param string
* @return void
*/
private function scanDirectory($dir)
{
$disallow = array();
$iterator = new \AppendIterator;
$iterator->append(new \ArrayIterator(array(new \SplFileInfo($dir))));
if (is_dir($dir)) {
$iterator->append(Nette\Finder::find(String::split($this->acceptFiles, '#[,\s]+#'))
->from($dir)
->exclude(String::split($this->ignoreDirs, '#[,\s]+#'))
->filter(function($file) use (&$disallow){
return !isset($disallow[$file->getPathname()]);
})->getIterator()
);
};
foreach ($iterator as $entry) {
$path = (string) $entry;
if ($entry->isDir() && is_file("$path/netterobots.txt")) {
foreach (file("$path/netterobots.txt") as $s) {
if ($matches = String::match($s, '#^disallow\\s*:\\s*(\\S+)#i')) {
$disallow[$path . str_replace('/', DIRECTORY_SEPARATOR, '/' . trim($matches[1], '/'))] = TRUE;
}
}
}
if ($entry->isFile()) {
if (!isset($this->files[$path]) || $this->files[$path] !== filemtime($path)) {
$this->scanScript($path);
}
}
}
}
/**
* Analyse PHP file.
* @param string
* @return void
*/
private function scanScript($file)
{
$T_NAMESPACE = PHP_VERSION_ID < 50300 ? -1 : T_NAMESPACE;
$T_NS_SEPARATOR = PHP_VERSION_ID < 50300 ? -1 : T_NS_SEPARATOR;
$expected = FALSE;
$namespace = '';
$level = $minLevel = 0;
$time = filemtime($file);
$s = file_get_contents($file);
if ($matches = String::match($s, '#//nette'.'loader=(\S*)#')) {
foreach (explode(',', $matches[1]) as $name) {
$this->addClass($name, $file, $time);
}
return;
}
foreach (token_get_all($s) as $token)
{
if (is_array($token)) {
switch ($token[0]) {
case T_COMMENT:
case T_DOC_COMMENT:
case T_WHITESPACE:
continue 2;
case $T_NS_SEPARATOR:
case T_STRING:
if ($expected) {
$name .= $token[1];
}
continue 2;
case $T_NAMESPACE:
case T_CLASS:
case T_INTERFACE:
$expected = $token[0];
$name = '';
continue 2;
case T_CURLY_OPEN:
case T_DOLLAR_OPEN_CURLY_BRACES:
$level++;
}
}
if ($expected) {
switch ($expected) {
case T_CLASS:
case T_INTERFACE:
if ($level === $minLevel) {
$this->addClass($namespace . $name, $file, $time);
}
break;
case $T_NAMESPACE:
$namespace = $name ? $name . '\\' : '';
$minLevel = $token === '{' ? 1 : 0;
}
$expected = NULL;
}
if ($token === '{') {
$level++;
} elseif ($token === '}') {
$level--;
}
}
}
/********************* backend ****************d*g**/
/**
* @return Nette\Caching\Cache
*/
protected function getCache()
{
return Nette\Environment::getCache('Nette.RobotLoader');
}
/**
* @return string
*/
protected function getKey()
{
return md5("v2|$this->ignoreDirs|$this->acceptFiles|" . implode('|', $this->scanDirs));
}
/**
* @return bool
*/
protected function isProduction()
{
return Nette\Environment::isProduction();
}
}