1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Jean-François Lépine <https://twitter.com/Halleck45> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Hal\Component\OOP\Reflected; |
11
|
|
|
use Hal\Component\OOP\Resolver\NameResolver; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Result (class) |
16
|
|
|
* |
17
|
|
|
* @author Jean-François Lépine <https://twitter.com/Halleck45> |
18
|
|
|
*/ |
19
|
|
|
class ReflectedClass { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $namespace; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $name; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Methods |
33
|
|
|
* |
34
|
|
|
* @var \SplObjectStorage[] |
35
|
|
|
*/ |
36
|
|
|
protected $methods; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Resolver for names |
40
|
|
|
* |
41
|
|
|
* @var NameResolver |
42
|
|
|
*/ |
43
|
|
|
protected $nameResolver; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Does the class is abstract ? |
47
|
|
|
* |
48
|
|
|
* @var bool |
49
|
|
|
*/ |
50
|
|
|
protected $isAbstract = false; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected $interfaces = array(); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Parent's name |
59
|
|
|
* |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
private $parent; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Constructor |
66
|
|
|
* |
67
|
|
|
* @param string $namespace |
68
|
|
|
* @param string $name |
69
|
|
|
*/ |
70
|
|
|
public function __construct($namespace, $name) |
71
|
|
|
{ |
72
|
|
|
$this->name = (string) $name; |
73
|
|
|
$this->namespace = (string) $namespace; |
74
|
|
|
$this->methods = array(); |
75
|
|
|
$this->nameResolver = new NameResolver(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get fullname (namespace + name) |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getFullname() { |
84
|
|
|
return $this->getNamespace().'\\'.$this->getName(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getName() |
91
|
|
|
{ |
92
|
|
|
return $this->name; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function getNamespace() |
99
|
|
|
{ |
100
|
|
|
return rtrim($this->namespace, '\\'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $namespace |
105
|
|
|
* @return ReflectedClass |
106
|
|
|
*/ |
107
|
|
|
protected function setNamespace($namespace) |
108
|
|
|
{ |
109
|
|
|
$this->namespace = (string) $namespace; |
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return \SplObjectStorage[] |
115
|
|
|
*/ |
116
|
|
|
public function getMethods() |
117
|
|
|
{ |
118
|
|
|
return $this->methods; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Attach method |
123
|
|
|
* This method consolidated dependencies |
124
|
|
|
* |
125
|
|
|
* @param ReflectedMethod $method |
126
|
|
|
* @return $this |
127
|
|
|
*/ |
128
|
|
|
public function pushMethod(ReflectedMethod $method) { |
129
|
|
|
$this->methods[$method->getName()] = $method; |
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
public function getDependencies() |
137
|
|
|
{ |
138
|
|
|
$dependencies = array(); |
139
|
|
|
foreach($this->getMethods() as $method) { |
140
|
|
|
$dependencies = array_merge($dependencies, $method->getDependencies()); |
141
|
|
|
} |
142
|
|
|
foreach($dependencies as &$name) { |
143
|
|
|
if(in_array($name, array('self', 'static'))) { |
144
|
|
|
$name = $this->getFullname(); |
145
|
|
|
} |
146
|
|
|
$name = $this->nameResolver->resolve($name, $this->namespace); |
147
|
|
|
} |
148
|
|
|
return array_unique($dependencies); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param NameResolver $resolver |
153
|
|
|
* @return $this |
154
|
|
|
*/ |
155
|
|
|
public function setNameResolver(NameResolver $resolver) |
156
|
|
|
{ |
157
|
|
|
$this->nameResolver = $resolver; |
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Set abstractness of method |
163
|
|
|
* |
164
|
|
|
* @param boolean $bool |
165
|
|
|
* @return $this |
166
|
|
|
*/ |
167
|
|
|
public function setAbstract($bool) { |
168
|
|
|
$this->isAbstract = (bool) $bool; |
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Is Abstract ? |
174
|
|
|
* |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
|
|
public function isAbstract() { |
178
|
|
|
return $this->isAbstract; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Set the parent name |
183
|
|
|
* |
184
|
|
|
* @param $parent |
185
|
|
|
* @return $this |
186
|
|
|
*/ |
187
|
|
|
public function setParent($parent) { |
188
|
|
|
$this->parent = $parent; |
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Get the parent name |
194
|
|
|
* |
195
|
|
|
* @return string|null null when no parent class exists |
196
|
|
|
*/ |
197
|
|
|
public function getParent() { |
198
|
|
|
if ($this->parent === null) { |
199
|
|
|
return null; |
200
|
|
|
} |
201
|
|
|
return $this->nameResolver->resolve($this->parent, $this->namespace); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @return array |
206
|
|
|
*/ |
207
|
|
|
public function getInterfaces() |
208
|
|
|
{ |
209
|
|
|
$resolvedInterfaces = array(); |
210
|
|
|
foreach($this->interfaces as $interface) { |
211
|
|
|
array_push($resolvedInterfaces, $this->nameResolver->resolve($interface, $this->namespace)); |
212
|
|
|
} |
213
|
|
|
return $resolvedInterfaces; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param array $interfaces |
218
|
|
|
* @return $this |
219
|
|
|
*/ |
220
|
|
|
public function setInterfaces($interfaces) |
221
|
|
|
{ |
222
|
|
|
$this->interfaces = $interfaces; |
223
|
|
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Get anonymous classes contained in this class |
228
|
|
|
* |
229
|
|
|
* @return mixed |
230
|
|
|
*/ |
231
|
|
|
public function getAnonymousClasses() { |
232
|
|
|
$result = array(); |
233
|
|
|
foreach($this->methods as $method) { |
234
|
|
|
$result = array_merge($result, $method->getAnonymousClasses()); |
235
|
|
|
} |
236
|
|
|
return $result; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
}; |