|
1
|
|
|
<?php |
|
2
|
|
|
namespace CodeReview; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Very simple class autoloader for code_review plugin |
|
6
|
|
|
*/ |
|
7
|
|
|
class Autoloader { |
|
8
|
|
|
|
|
9
|
|
|
private $classMap = array(); |
|
10
|
|
|
|
|
11
|
1 |
|
public function __construct($basePath = null) { |
|
12
|
1 |
|
if ($basePath === null) { |
|
13
|
1 |
|
$basePath = dirname(dirname(__FILE__)); |
|
14
|
1 |
|
} |
|
15
|
1 |
|
$this->registerDirectory($basePath); |
|
16
|
1 |
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Not fully PSR-0 compatible, but good enough for this particular plugin |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $basePath |
|
22
|
|
|
* @param string $prefix |
|
23
|
|
|
*/ |
|
24
|
1 |
|
private function registerDirectory($basePath, $prefix = '') { |
|
25
|
1 |
|
$basePath = str_replace('\\', '/', $basePath); |
|
26
|
1 |
|
$basePath = rtrim($basePath, '/') . '/'; |
|
27
|
1 |
|
$prefix = ($prefix ? $prefix . '_' : '' ); |
|
28
|
1 |
|
$files = scandir($basePath); |
|
29
|
1 |
|
foreach ($files as $file) { |
|
30
|
1 |
|
if ($file[0] == '.') { |
|
31
|
1 |
|
continue; |
|
32
|
|
|
} |
|
33
|
1 |
|
$path = $basePath . $file; |
|
34
|
1 |
|
if (is_dir($path)) { |
|
35
|
1 |
|
$this->registerDirectory($path, $prefix . pathinfo($path, PATHINFO_FILENAME)); |
|
36
|
1 |
|
} elseif (strtolower(pathinfo($path, PATHINFO_EXTENSION)) == 'php') { |
|
37
|
1 |
|
$name = $prefix . pathinfo($path, PATHINFO_FILENAME); |
|
38
|
1 |
|
$this->classMap[$name] = $path; |
|
39
|
1 |
|
$name = str_replace('_', '\\', $name); // register again in case it was namespaced |
|
40
|
1 |
|
$this->classMap[$name] = $path; |
|
41
|
1 |
|
} |
|
42
|
1 |
|
} |
|
43
|
1 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $className |
|
47
|
|
|
* @return bool |
|
48
|
|
|
*/ |
|
49
|
7 |
|
public function load($className) { |
|
50
|
7 |
|
if (isset($this->classMap[$className]) && file_exists($this->classMap[$className])) { |
|
51
|
6 |
|
return include($this->classMap[$className]); |
|
52
|
|
|
} |
|
53
|
2 |
|
return false; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return bool |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public function register() { |
|
60
|
1 |
|
return spl_autoload_register(array($this, 'load')); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return bool |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function unregister() { |
|
67
|
1 |
|
return spl_autoload_unregister(array($this, 'load')); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
} |