forked from eturino/PseudoArray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFactory.php
283 lines (242 loc) · 9.41 KB
/
Factory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
class EtuDev_PseudoArray_Factory
{
/**
* @var array static warehouse for all properties from docblock
*/
static public $allDocBlockAliases = array();
static public $allDocBlockProperties = array();
static public $allClassProperties = array();
static public $allClassStaticProperties = array();
static public $allGetters = array();
static public $allSetters = array();
static public $allUnsetters = array();
static public $keySetBy = array();
static public $keySetByDirect = array();
static public $keyGetBy = array();
static public $keyGetByDirect = array();
static public $info = array();
static public function getInfo($class)
{
if (@static::$info[$class]) {
return static::$info[$class];
}
//load from cache
$info = EtuDev_PseudoArray_Cache::getInstance()->get($class);
if ($info) {
static::$info[$class] = $info;
return $info;
}
$info = static::loadInfo($class);
if ($info) {
EtuDev_PseudoArray_Cache::getInstance()->write($class, $info);
static::$info[$class] = $info;
return $info;
}
return array();
}
static public function deleteAllCached()
{
EtuDev_PseudoArray_Cache::getInstance()->deleteAll();
}
static public function loadInfo($class)
{
$ref = new ReflectionClass($class);
$info = static::calculateAttributeDocBlock($ref);
$info['aliases'] = ($info['properties'] || $info['aliases_different']) ? array_merge(
array_combine((array) $info['properties'], (array) $info['properties']),
(array) $info['aliases_different']
) : array();
$setters = array();
$getters = array();
foreach ((array) $info['properties'] as $prop) {
$aliases = $info['aliases'] ? array_filter(
$info['aliases'],
function ($v) use ($prop) {
return $v == $prop;
}
) : array();
$posGetters = static::calculatePossibleGetterNames($prop);
foreach ($posGetters as $getter) {
if ($ref->hasMethod($getter)) {
$getter = $ref->getMethod($getter)->getName();
foreach ($aliases as $al) {
$getters[$al] = $getter;
}
}
}
$posSetters = static::calculatePossibleSetterNames($prop);
foreach ($posSetters as $setter) {
if ($ref->hasMethod($setter)) {
$setter = $ref->getMethod($setter)->getName();
foreach ($aliases as $al) {
$setters[$al] = $setter;
}
}
}
}
$info['getters'] = $getters;
$info['setters'] = $setters;
return $info;
}
static protected function calculatePossibleGetterNames($key)
{
$setKey = 'get' . $key;
$k = str_replace('_', '', $key);
$setK = 'get' . $k;
$res = array($setKey);
if ($setKey != $setK) {
$res[] = $setK;
}
return $res;
}
static protected function calculatePossibleSetterNames($key)
{
$setKey = 'set' . $key;
$k = str_replace('_', '', $key);
$setK = 'set' . $k;
$res = array($setKey);
if ($setKey != $setK) {
$res[] = $setK;
}
return $res;
}
/**
* @param ReflectionClass $ref
*
* @return string
*/
static protected function getDocBlock($ref)
{
$docblocks = array();
$docblocks[] = static::calculateDocBlockOfSingleClassAndInterfaces($ref);
/** @var $ref ReflectionClass */
while (($ref = $ref->getParentClass()) && ($ref->getName() != __CLASS__) && ($ref->getName() != 'EtuDev_PseudoArray_Object')) {
$d = static::calculateDocBlockOfSingleClassAndInterfaces($ref);
if ($d) {
$docblocks[] = $d;
}
}
$docblocks = array_reverse($docblocks);
return implode("\n", $docblocks);
}
/**
* @param ReflectionClass $ref
*
* @return string
*/
static protected function calculateDocBlockOfSingleClassAndInterfaces($ref)
{
$docblocks = array();
$docblocks[] = $ref->getDocComment();
$a = $ref->getInterfaces();
foreach ($a as $int) {
$d = $int->getDocComment();
if ($d) {
$docblocks[] = $d;
}
}
if (method_exists($ref, 'getTraits')) {
$traits = $ref->getTraits();
foreach ($traits as $t) {
$d = static::calculateDocBlockOfSingleClassAndInterfaces($t);
if ($d) {
$docblocks[] = $d;
}
}
}
$docblocks = array_reverse($docblocks);
return implode("\n", $docblocks);
}
static protected function calculateAttributeDocBlock($ref)
{
//docblock used to parse
$dc = static::getDocBlock($ref);
$lines = explode("\n", $dc);
$active_levels = array();
$aliases = array();
$toIgnore = array();
$forall = array();
$ats_by_level = array(EtuDev_PseudoArray_Object::LEVEL_ALL);
foreach ($lines as $line) {
// check new active level
if (preg_match('/@propertieslevel[\s\t]+[a-zA-Z_0-9,]+/', $line, $matches)) {
foreach ($matches as $key => $m) {
$res = trim(str_replace('@propertieslevel', '', $m));
if (!$res) {
$active_levels = array();
} else {
$active_levels = explode(',', $res);
}
}
} elseif (trim($line) == '/**') { //new class docblock => reset property level
$active_levels = array();
} else {
// check attribute
if (preg_match('/@propertyalias[\s\t]+[$a-zA-Z_0-9]+[\s\t]+[$a-zA-Z_0-9]+/', $line, $matches)) {
foreach ($matches as $key => $m) {
$res = trim(str_replace('@propertyalias', '', $m));
$res = str_replace('$', '', $res);
$res = trim(preg_replace('/[\s\t]+/', ' ', $res));
$res = explode(' ', $res);
$aliases[$res[0]] = $res[1];
}
}
// check attribute
if (preg_match('/@propertyignore[d]?[\s\t]+[$a-zA-Z_0-9]+/', $line, $matches)) {
foreach ($matches as $key => $m) {
$res = trim(str_replace('@propertyignored', '', $m));
$res = str_replace('@propertyignore', '', $res);
$res = str_replace('$', '', $res);
$res = trim(preg_replace('/[\s\t]+/', ' ', $res));
$res = explode(' ', $res);
$toIgnore[] = $res[0];
}
}
//con tipo
if (preg_match('/@property[\s\t]+[a-zA-Z_\|]+[\s\t]+[\$][a-zA-Z_0-9]+/', $line, $matches)) {
foreach ($matches as $key => $m) {
$at = preg_replace('/^@[\t\sa-zA-Z_0-9\|]+\$/', '', $m);
$ats_by_level[EtuDev_PseudoArray_Object::LEVEL_ALL][] = $at;
if ($active_levels) {
foreach ($active_levels as $al) {
$ats_by_level[$al][] = $at;
}
} else {
$forall[] = $at;
}
}
}
//sin tipo
if (preg_match('/@property[\s\t]+[\$][a-zA-Z_]+/', $line, $matches)) {
foreach ($matches as $key => $m) {
$at = preg_replace('/^@[\t\sa-zA-Z_0-9]+\$/', '', $m);
if (!in_array($at, $forall)) {
$ats_by_level[EtuDev_PseudoArray_Object::LEVEL_ALL][] = $at;
if ($active_levels) {
foreach ($active_levels as $al) {
$ats_by_level[$al][] = $at;
}
} else {
$forall[] = $at;
}
}
}
}
}
}
$forall = array_unique($forall);
$auxlevels = array();
//clean
foreach ($ats_by_level as $al => $allist) {
if ($allist) { //add the ats for all levels + the ats for this level
$auxlevels[$al] = array_values(array_unique(array_merge($forall, $allist)));
}
}
$info['properties'] = (array) @$auxlevels[EtuDev_PseudoArray_Object::LEVEL_ALL];
$info['levels'] = (array) $auxlevels;
$info['aliases_different'] = (array) $aliases;
$info['ignore_to_array'] = (array) $toIgnore;
return $info;
}
}