1
|
|
|
<?php namespace BuildR\ClassLoader\Modules\PEAR; |
2
|
|
|
|
3
|
|
|
use BuildR\ClassLoader\Modules\AbstractClassLoaderModule; |
4
|
|
|
use BuildR\ClassLoader\Modules\PEAR\PEARModuleException; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* PEAR compatible class loader module. |
8
|
|
|
* |
9
|
|
|
* BuildR PHP Framework |
10
|
|
|
* |
11
|
|
|
* @author Zoltán Borsos <[email protected]> |
12
|
|
|
* @package ClassLoader |
13
|
|
|
* @subpackage Modules\PEAR |
14
|
|
|
* |
15
|
|
|
* @copyright Copyright 2015, Zoltán Borsos. |
16
|
|
|
* @license https://github.com/Zolli/BuildR/blob/master/LICENSE.md |
17
|
|
|
* @link https://github.com/Zolli/BuildR |
18
|
|
|
*/ |
19
|
|
|
class PEARClassLoaderModule extends AbstractClassLoaderModule { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @type int |
23
|
|
|
*/ |
24
|
|
|
protected $priority = 50; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @type array |
28
|
|
|
*/ |
29
|
|
|
private $registeredPrefixes = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritDoc |
33
|
|
|
* @codeCoverageIgnore |
34
|
|
|
*/ |
35
|
|
|
public static function getName() { |
36
|
|
|
return 'PEARClassLoaderModule'; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritDoc |
41
|
|
|
* @codeCoverageIgnore |
42
|
|
|
*/ |
43
|
|
|
public function getPriority() { |
44
|
|
|
return $this->priority; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritDoc |
49
|
|
|
* @codeCoverageIgnore |
50
|
|
|
*/ |
51
|
|
|
public function onRegistered() {} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritDoc |
55
|
|
|
*/ |
56
|
2 |
|
public function load($className) { |
57
|
2 |
|
if(count($this->registeredPrefixes) < 1) { |
58
|
1 |
|
return FALSE; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
foreach($this->registeredPrefixes as $singlePrefix) { |
62
|
1 |
|
$prefix = $singlePrefix[0]; |
63
|
1 |
|
$basePath = $singlePrefix[1]; |
64
|
|
|
|
65
|
1 |
|
$pos = strpos($className, $prefix); |
66
|
1 |
|
if($pos === FALSE || $pos > 0) { |
67
|
1 |
|
continue; |
68
|
|
|
} |
69
|
1 |
|
$pathNamespace = ltrim(substr($className, strlen($prefix)), '_'); |
70
|
1 |
|
$file = $basePath . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $pathNamespace) . '.php'; |
71
|
|
|
|
72
|
1 |
|
if(file_exists($file)) { |
73
|
1 |
|
include_once $file; |
74
|
|
|
|
75
|
1 |
|
return TRUE; |
76
|
|
|
} |
77
|
1 |
|
} |
78
|
|
|
|
79
|
1 |
|
return FALSE; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Register a new prefix in this module |
84
|
|
|
* |
85
|
|
|
* @param string $prefix |
86
|
|
|
* @param string $basePath |
87
|
|
|
* |
88
|
|
|
* @throws \BuildR\ClassLoader\Modules\PEAR\PEARModuleException |
89
|
|
|
*/ |
90
|
4 |
|
public function registerPrefix($prefix, $basePath) { |
91
|
4 |
|
if($this->prefixIsRegistered($prefix)) { |
92
|
1 |
|
throw PEARModuleException::prefixOccupied($prefix); |
93
|
|
|
} |
94
|
|
|
|
95
|
4 |
|
$this->registeredPrefixes[] = [ |
96
|
4 |
|
$prefix, |
97
|
4 |
|
realpath($basePath) |
98
|
4 |
|
]; |
99
|
4 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Remove a registered prefix from the module |
103
|
|
|
* |
104
|
|
|
* @param string $prefix The prefix name |
105
|
|
|
*/ |
106
|
1 |
|
public function unRegisterPrefix($prefix) { |
107
|
1 |
|
foreach($this->registeredPrefixes as $key => $registeredPrefix) { |
108
|
1 |
|
if($registeredPrefix[0] == $prefix) { |
109
|
1 |
|
unset($this->registeredPrefixes[$key]); |
110
|
1 |
|
} |
111
|
1 |
|
} |
112
|
1 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Determines that the given prefix is registered in this module |
116
|
|
|
* |
117
|
|
|
* @param string $prefix The prefix name |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
4 |
|
public function prefixIsRegistered($prefix) { |
122
|
4 |
|
foreach($this->registeredPrefixes as $key => $registeredPrefix) { |
123
|
2 |
|
if($registeredPrefix[0] == $prefix) { |
124
|
2 |
|
return TRUE; |
125
|
|
|
} |
126
|
4 |
|
} |
127
|
|
|
|
128
|
4 |
|
return FALSE; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
} |
132
|
|
|
|