-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFromAttributes.class.php
More file actions
executable file
·145 lines (126 loc) · 4.29 KB
/
FromAttributes.class.php
File metadata and controls
executable file
·145 lines (126 loc) · 4.29 KB
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
<?php namespace lang\meta;
use PhpToken;
use lang\reflection\Annotation;
/**
* PHP 8.0 attributes API
*
* @see https://wiki.php.net/rfc/shorter_attribute_syntax
*/
class FromAttributes {
private function annotations($reflect, $context) {
$r= [];
foreach ($reflect->getAttributes() as $attribute) {
$args= $attribute->getArguments();
$ptr= &$r[$attribute->getName()];
if (!isset($args['eval'])) {
$ptr= $args;
} else if (is_array($args['eval'])) {
$ptr= [];
foreach ($args['eval'] as $key => $value) {
$ptr[$key]= $this->evaluate($context, $value);
}
} else {
$ptr= [$this->evaluate($context, $args['eval'])];
}
}
return $r;
}
/** @return iterable */
public function ofType($reflect) {
return $this->annotations($reflect, $reflect);
}
/** @return iterable */
public function ofConstant($reflect) {
return $this->annotations($reflect, $reflect->getDeclaringClass());
}
/** @return iterable */
public function ofProperty($reflect) {
return $this->annotations($reflect, $reflect->getDeclaringClass());
}
/** @return iterable */
public function ofMethod($reflect) {
return $this->annotations($reflect, $reflect->getDeclaringClass());
}
/** @return iterable */
public function ofParameter($method, $reflect) {
return $this->annotations($reflect, $method->getDeclaringClass());
}
/**
* Returns imports used in the class file the given class was declared in
*
* @param \ReflectionClass $reflect
* @return [:string]
*/
public function imports($reflect) {
static $break= [T_CLASS => true, T_INTERFACE => true, T_TRAIT => true, 372 /* T_ENUM */ => true];
static $types= [T_WHITESPACE => true, 44 => true, 59 => true, 123 => true];
$tokens= PhpToken::tokenize(file_get_contents($reflect->getFileName()));
$imports= ['class' => [], 'function' => [], 'const' => []];
for ($i= 0, $s= sizeof($tokens); $i < $s; $i++) {
if (isset($break[$tokens[$i]->id])) break;
if (T_USE !== $tokens[$i]->id) continue;
do {
$i+= 2;
if (T_FUNCTION === $tokens[$i]->id) {
$kind= 'function';
$i+= 2;
} else if (T_CONST === $tokens[$i]->id) {
$kind= 'const';
$i+= 2;
} else {
$kind= 'class';
}
for ($type= ''; $i < $s, !isset($types[$tokens[$i]->id]); $i++) {
$type.= $tokens[$i]->text;
}
// Skip over whitespace
if (T_WHITESPACE === $tokens[$i]->id) $i++;
// use `lang\{Type, Primitive as P}` vs. `use lang\Primitive as P;` vs. `use lang\Primitive`
if (123 === $tokens[$i]->id) {
$alias= null;
$group= '';
for ($i+= 1; $i < $s; $i++) {
if (44 === $tokens[$i]->id) {
$imports[$kind][$alias ?? $group]= $type.$group;
$alias= null;
$group= '';
} else if (125 === $tokens[$i]->id) {
$imports[$kind][$alias ?? $group]= $type.$group;
break;
} else if (T_AS === $tokens[$i]->id) {
$i+= 2;
$alias= $tokens[$i]->text;
} else if (T_WHITESPACE !== $tokens[$i]->id) {
$group.= $tokens[$i]->text;
}
}
} else if (T_AS === $tokens[$i]->id) {
$i+= 2;
$imports[$kind][$tokens[$i]->text]= $type;
} else if (false === ($p= strrpos($type, '\\'))) {
$imports[$kind][$type]= $type;
} else {
$imports[$kind][substr($type, strrpos($type, '\\') + 1)]= $type;
}
// Skip over whitespace
if (T_WHITESPACE === $tokens[$i]->id) $i++;
} while (44 === $tokens[$i]->id);
}
return $imports;
}
public function evaluate($reflect, $code) {
$header= '';
if ($namespace= $reflect->getNamespaceName()) {
$header.= 'namespace '.$namespace.';';
}
// Recreate all imports
foreach ($this->imports($reflect) as $kind => $list) {
$use= 'class' === $kind ? 'use' : 'use '.$kind;
foreach ($list as $import => $type) {
$header.= $type ? "{$use} {$type} as {$import};" : "{$use} {$import};";
}
}
$f= eval($header.' return static function() { return '.$code.'; };');
return $f->bindTo(null, $reflect->name)();
}
}