1
|
|
|
<?php namespace BuildR\ClassLoader\Modules\Map; |
2
|
|
|
|
3
|
|
|
use BuildR\ClassLoader\Modules\AbstractClassLoaderModule; |
4
|
|
|
use BuildR\ClassLoader\Modules\Map\MapModuleException; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class map class loader module |
8
|
|
|
* |
9
|
|
|
* BuildR PHP Framework |
10
|
|
|
* |
11
|
|
|
* @author Zoltán Borsos <[email protected]> |
12
|
|
|
* @package ClassLoader |
13
|
|
|
* @subpackage Modules\Map |
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 MapClassLoaderModule extends AbstractClassLoaderModule { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @type int |
23
|
|
|
*/ |
24
|
|
|
protected $priority = 10; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @type array |
28
|
|
|
*/ |
29
|
|
|
private $registeredMaps = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritDoc |
33
|
|
|
* @codeCoverageIgnore |
34
|
|
|
*/ |
35
|
|
|
public static function getName() { |
36
|
|
|
return 'MapClassLoaderModule'; |
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
|
4 |
|
public function load($className) { |
57
|
4 |
|
if(count($this->registeredMaps) < 1) { |
58
|
1 |
|
return FALSE; |
59
|
|
|
} |
60
|
|
|
|
61
|
3 |
|
foreach($this->registeredMaps as $mapName => $map) { |
62
|
3 |
|
if(isset($map[$className])) { |
63
|
2 |
|
$classFile = $map[$className]; |
64
|
|
|
|
65
|
2 |
|
if(file_exists($classFile)) { |
66
|
1 |
|
include_once $classFile; |
67
|
|
|
|
68
|
1 |
|
return TRUE; |
69
|
|
|
} |
70
|
2 |
|
} |
71
|
3 |
|
} |
72
|
|
|
|
73
|
3 |
|
return FALSE; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Register a new map in the module |
78
|
|
|
* |
79
|
|
|
* @param string $mapName The map unique name |
80
|
|
|
* @param array $map The actual class map |
81
|
|
|
* |
82
|
|
|
* @throws \BuildR\ClassLoader\Modules\Map\MapModuleException |
83
|
|
|
*/ |
84
|
5 |
|
public function registerMap($mapName, $map) { |
85
|
5 |
|
if($this->mapIsRegistered($mapName)) { |
86
|
1 |
|
throw MapModuleException::mapNameOccupied($mapName); |
87
|
|
|
} |
88
|
|
|
|
89
|
5 |
|
$this->registeredMaps[$mapName] = (array) $map; |
90
|
5 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Remove the given map from the loader |
94
|
|
|
* |
95
|
|
|
* @param string $mapName The map name |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
1 |
|
public function removeMap($mapName) { |
100
|
1 |
|
if(!$this->mapIsRegistered($mapName)) { |
101
|
1 |
|
return FALSE; |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
unset($this->registeredMaps[$mapName]); |
105
|
|
|
|
106
|
1 |
|
return TRUE; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Determines that the given map is registered or not |
111
|
|
|
* |
112
|
|
|
* @param string $map The map name |
113
|
|
|
* |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
5 |
|
public function mapIsRegistered($map) { |
117
|
5 |
|
return isset($this->registeredMaps[$map]); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|