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