|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\orm\parser; |
|
4
|
|
|
|
|
5
|
|
|
use mindplay\annotations\Annotations; |
|
6
|
|
|
use Ubiquity\orm\OrmUtils; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Utilitaires de Reflexion |
|
10
|
|
|
* @author jc |
|
11
|
|
|
* @version 1.0.0.2 |
|
12
|
|
|
*/ |
|
13
|
|
|
class Reflexion { |
|
14
|
|
|
|
|
15
|
|
|
public static function getProperties($instance) { |
|
16
|
|
|
if (\is_string($instance)) { |
|
17
|
|
|
$instance=new $instance(); |
|
18
|
|
|
} |
|
19
|
|
|
$reflect=new \ReflectionClass($instance); |
|
20
|
|
|
$props=$reflect->getProperties(); |
|
21
|
|
|
return $props; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public static function getMethods($instance, $filter=null) { |
|
25
|
|
|
$reflect=new \ReflectionClass($instance); |
|
26
|
|
|
$methods=$reflect->getMethods($filter); |
|
27
|
|
|
return $methods; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public static function getKeyFields($instance) { |
|
31
|
|
|
return Reflexion::getMembersNameWithAnnotation(get_class($instance), "@id"); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public static function getMemberValue($instance, $member) { |
|
35
|
|
|
$prop=self::getProperty($instance, $member); |
|
36
|
|
|
$prop->setAccessible(true); |
|
37
|
|
|
return $prop->getValue($instance); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public static function setMemberValue($instance, $member,$value) { |
|
41
|
|
|
$prop=self::getProperty($instance, $member); |
|
42
|
|
|
if($prop){ |
|
43
|
|
|
$prop->setAccessible(true); |
|
44
|
|
|
$prop->setValue($instance,$value); |
|
45
|
|
|
return true; |
|
46
|
|
|
} |
|
47
|
|
|
return false; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public static function getProperty($instance, $member) { |
|
51
|
|
|
$reflect=new \ReflectionClass($instance); |
|
52
|
|
|
$prop=false; |
|
53
|
|
|
if($reflect->hasProperty($member)) |
|
54
|
|
|
$prop=$reflect->getProperty($member); |
|
55
|
|
|
return $prop; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public static function getPropertyType($class,$property){ |
|
59
|
|
|
return self::getMetadata($class, $property, "@var", "type"); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public static function getMetadata($class,$property, $type, $name){ |
|
63
|
|
|
$a = Annotations::ofProperty($class, $property, $type); |
|
64
|
|
|
if (!count($a)){ |
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
return trim($a[0]->$name,";"); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
public static function getPropertiesAndValues($instance, $props=NULL) { |
|
72
|
|
|
$ret=array (); |
|
73
|
|
|
$className=get_class($instance); |
|
74
|
|
|
if (is_null($props)) |
|
75
|
|
|
$props=self::getProperties($instance); |
|
76
|
|
|
foreach ( $props as $prop ) { |
|
77
|
|
|
$prop->setAccessible(true); |
|
78
|
|
|
$v=$prop->getValue($instance); |
|
79
|
|
|
if (OrmUtils::isSerializable($className, $prop->getName())) { |
|
80
|
|
|
if (OrmUtils::isNotNullOrNullAccepted($v, $className, $prop->getName())) { |
|
81
|
|
|
$name=OrmUtils::getFieldName($className, $prop->getName()); |
|
82
|
|
|
$ret[$name]=$v; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
return $ret; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public static function getAnnotationClass($class, $annotation) { |
|
90
|
|
|
$annot=Annotations::ofClass($class, $annotation); |
|
91
|
|
|
return $annot; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public static function getAnnotationMember($class, $member, $annotation) { |
|
95
|
|
|
$annot=Annotations::ofProperty($class, $member, $annotation); |
|
96
|
|
|
if (\sizeof($annot) > 0) |
|
97
|
|
|
return $annot[0]; |
|
98
|
|
|
return false; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public static function getAnnotationsMember($class, $member, $annotation) { |
|
102
|
|
|
return Annotations::ofProperty($class, $member, $annotation); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public static function getAnnotationsMethod($class, $method, $annotation) { |
|
106
|
|
|
if(is_array($annotation)){ |
|
107
|
|
|
$result=[]; |
|
108
|
|
|
foreach($annotation as $annot){ |
|
109
|
|
|
$annots=Annotations::ofMethod($class, $method, $annot); |
|
110
|
|
|
if(sizeof($annots)>0){ |
|
111
|
|
|
$result=array_merge($result,$annots); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
return $result; |
|
115
|
|
|
} |
|
116
|
|
|
$annots=Annotations::ofMethod($class, $method, $annotation); |
|
117
|
|
|
if (\sizeof($annots) > 0) |
|
118
|
|
|
return $annots; |
|
119
|
|
|
return false; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public static function getMembersAnnotationWithAnnotation($class, $annotation) { |
|
123
|
|
|
$props=self::getProperties($class); |
|
124
|
|
|
$ret=array (); |
|
125
|
|
|
foreach ( $props as $prop ) { |
|
126
|
|
|
$annot=self::getAnnotationMember($class, $prop->getName(), $annotation); |
|
127
|
|
|
if ($annot !== false) |
|
128
|
|
|
$ret[$prop->getName()]=$annot; |
|
129
|
|
|
} |
|
130
|
|
|
return $ret; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public static function getMembersWithAnnotation($class, $annotation) { |
|
134
|
|
|
$props=self::getProperties($class); |
|
135
|
|
|
$ret=array (); |
|
136
|
|
|
foreach ( $props as $prop ) { |
|
137
|
|
|
$annot=self::getAnnotationMember($class, $prop->getName(), $annotation); |
|
138
|
|
|
if ($annot !== false) |
|
139
|
|
|
$ret[]=$prop; |
|
140
|
|
|
} |
|
141
|
|
|
return $ret; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public static function getMembersNameWithAnnotation($class, $annotation) { |
|
145
|
|
|
$props=self::getProperties($class); |
|
146
|
|
|
$ret=array (); |
|
147
|
|
|
foreach ( $props as $prop ) { |
|
148
|
|
|
$annot=self::getAnnotationMember($class, $prop->getName(), $annotation); |
|
149
|
|
|
if ($annot !== false) |
|
150
|
|
|
$ret[]=$prop->getName(); |
|
151
|
|
|
} |
|
152
|
|
|
return $ret; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public static function isNullable($class, $member) { |
|
156
|
|
|
$ret=self::getAnnotationMember($class, $member, "@column"); |
|
157
|
|
|
if (!$ret) |
|
158
|
|
|
return false; |
|
159
|
|
|
else |
|
160
|
|
|
return $ret->nullable; |
|
|
|
|
|
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public static function getDbType($class, $member) { |
|
164
|
|
|
$ret=self::getAnnotationMember($class, $member, "@column"); |
|
165
|
|
|
if (!$ret) |
|
166
|
|
|
return false; |
|
167
|
|
|
else |
|
168
|
|
|
return $ret->dbType; |
|
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public static function isSerializable($class, $member) { |
|
172
|
|
|
if (self::getAnnotationMember($class, $member, "@transient") !== false || self::getAnnotationMember($class, $member, "@manyToOne") !== false || self::getAnnotationMember($class, $member, "@manyToMany") !== false || self::getAnnotationMember($class, $member, "@oneToMany") !== false) |
|
173
|
|
|
return false; |
|
174
|
|
|
else |
|
175
|
|
|
return true; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
public static function getFieldName($class, $member) { |
|
179
|
|
|
$ret=self::getAnnotationMember($class, $member, "@column"); |
|
180
|
|
|
if ($ret === false || !isset($ret->name)) |
|
|
|
|
|
|
181
|
|
|
$ret=$member; |
|
182
|
|
|
else |
|
183
|
|
|
$ret=$ret->name; |
|
184
|
|
|
return $ret; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public static function getTableName($class) { |
|
188
|
|
|
$ret=Reflexion::getAnnotationClass($class, "@table"); |
|
189
|
|
|
if (\sizeof($ret) === 0) { |
|
190
|
|
|
$posSlash=strrpos($class, '\\'); |
|
191
|
|
|
if ($posSlash !== false) |
|
192
|
|
|
$class=substr($class, $posSlash + 1); |
|
193
|
|
|
$ret=$class; |
|
194
|
|
|
} else { |
|
195
|
|
|
$ret=$ret[0]->name; |
|
196
|
|
|
} |
|
197
|
|
|
return $ret; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
public static function getMethodParameters(\ReflectionMethod $method) { |
|
201
|
|
|
$result=array (); |
|
202
|
|
|
foreach ( $method->getParameters() as $param ) { |
|
203
|
|
|
$result[]=$param->name; |
|
204
|
|
|
} |
|
205
|
|
|
return $result; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
public static function getJoinTables($class){ |
|
209
|
|
|
$result=[]; |
|
210
|
|
|
$annots=self::getMembersAnnotationWithAnnotation($class, "@joinTable"); |
|
211
|
|
|
foreach ($annots as $annot){ |
|
212
|
|
|
$result[]=$annot->name; |
|
213
|
|
|
} |
|
214
|
|
|
return $result; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
public static function getAllJoinTables($models){ |
|
218
|
|
|
$result=[]; |
|
219
|
|
|
foreach ($models as $model){ |
|
220
|
|
|
$result=array_merge($result,self::getJoinTables($model)); |
|
221
|
|
|
} |
|
222
|
|
|
return $result; |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|