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