|
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 getPropertiesAndValues($instance, $props=NULL) { |
|
59
|
|
|
$ret=array (); |
|
60
|
|
|
$className=get_class($instance); |
|
61
|
|
|
if (is_null($props)) |
|
62
|
|
|
$props=self::getProperties($instance); |
|
63
|
|
|
foreach ( $props as $prop ) { |
|
64
|
|
|
$prop->setAccessible(true); |
|
65
|
|
|
$v=$prop->getValue($instance); |
|
66
|
|
|
if (OrmUtils::isSerializable($className, $prop->getName())) { |
|
67
|
|
View Code Duplication |
if (OrmUtils::isNotNullOrNullAccepted($v, $className, $prop->getName())) { |
|
68
|
|
|
$name=OrmUtils::getFieldName($className, $prop->getName()); |
|
69
|
|
|
$ret[$name]=$v; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
return $ret; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public static function getAnnotationClass($class, $annotation) { |
|
77
|
|
|
$annot=Annotations::ofClass($class, $annotation); |
|
78
|
|
|
return $annot; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public static function getAnnotationMember($class, $member, $annotation) { |
|
82
|
|
|
$annot=Annotations::ofProperty($class, $member, $annotation); |
|
83
|
|
|
if (\sizeof($annot) > 0) |
|
84
|
|
|
return $annot[0]; |
|
85
|
|
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public static function getAnnotationsMember($class, $member, $annotation) { |
|
89
|
|
|
return Annotations::ofProperty($class, $member, $annotation); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public static function getAnnotationsMethod($class, $method, $annotation) { |
|
93
|
|
|
if(is_array($annotation)){ |
|
94
|
|
|
$result=[]; |
|
95
|
|
|
foreach($annotation as $annot){ |
|
96
|
|
|
$annots=Annotations::ofMethod($class, $method, $annot); |
|
97
|
|
|
if(sizeof($annots)>0){ |
|
98
|
|
|
$result=array_merge($result,$annots); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
return $result; |
|
102
|
|
|
} |
|
103
|
|
|
$annots=Annotations::ofMethod($class, $method, $annotation); |
|
104
|
|
|
if (\sizeof($annots) > 0) |
|
105
|
|
|
return $annots; |
|
106
|
|
|
return false; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
View Code Duplication |
public static function getMembersAnnotationWithAnnotation($class, $annotation) { |
|
110
|
|
|
$props=self::getProperties($class); |
|
111
|
|
|
$ret=array (); |
|
112
|
|
|
foreach ( $props as $prop ) { |
|
113
|
|
|
$annot=self::getAnnotationMember($class, $prop->getName(), $annotation); |
|
114
|
|
|
if ($annot !== false) |
|
115
|
|
|
$ret[$prop->getName()]=$annot; |
|
116
|
|
|
} |
|
117
|
|
|
return $ret; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public static function getMembersWithAnnotation($class, $annotation) { |
|
121
|
|
|
$props=self::getProperties($class); |
|
122
|
|
|
$ret=array (); |
|
123
|
|
|
foreach ( $props as $prop ) { |
|
124
|
|
|
$annot=self::getAnnotationMember($class, $prop->getName(), $annotation); |
|
125
|
|
|
if ($annot !== false) |
|
126
|
|
|
$ret[]=$prop; |
|
127
|
|
|
} |
|
128
|
|
|
return $ret; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
View Code Duplication |
public static function getMembersNameWithAnnotation($class, $annotation) { |
|
132
|
|
|
$props=self::getProperties($class); |
|
133
|
|
|
$ret=array (); |
|
134
|
|
|
foreach ( $props as $prop ) { |
|
135
|
|
|
$annot=self::getAnnotationMember($class, $prop->getName(), $annotation); |
|
136
|
|
|
if ($annot !== false) |
|
137
|
|
|
$ret[]=$prop->getName(); |
|
138
|
|
|
} |
|
139
|
|
|
return $ret; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public static function isNullable($class, $member) { |
|
143
|
|
|
$ret=self::getAnnotationMember($class, $member, "@column"); |
|
144
|
|
|
if (!$ret) |
|
145
|
|
|
return false; |
|
146
|
|
|
else |
|
147
|
|
|
return $ret->nullable; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public static function getDbType($class, $member) { |
|
151
|
|
|
$ret=self::getAnnotationMember($class, $member, "@column"); |
|
152
|
|
|
if (!$ret) |
|
153
|
|
|
return false; |
|
154
|
|
|
else |
|
155
|
|
|
return $ret->dbType; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public static function isSerializable($class, $member) { |
|
159
|
|
|
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) |
|
160
|
|
|
return false; |
|
161
|
|
|
else |
|
162
|
|
|
return true; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public static function getFieldName($class, $member) { |
|
166
|
|
|
$ret=self::getAnnotationMember($class, $member, "@column"); |
|
167
|
|
|
if ($ret === false || !isset($ret->name)) |
|
168
|
|
|
$ret=$member; |
|
169
|
|
|
else |
|
170
|
|
|
$ret=$ret->name; |
|
171
|
|
|
return $ret; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
public static function getTableName($class) { |
|
175
|
|
|
$ret=Reflexion::getAnnotationClass($class, "@table"); |
|
176
|
|
|
if (\sizeof($ret) === 0) { |
|
177
|
|
|
$posSlash=strrpos($class, '\\'); |
|
178
|
|
|
if ($posSlash !== false) |
|
179
|
|
|
$class=substr($class, $posSlash + 1); |
|
180
|
|
|
$ret=$class; |
|
181
|
|
|
} else { |
|
182
|
|
|
$ret=$ret[0]->name; |
|
183
|
|
|
} |
|
184
|
|
|
return $ret; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public static function getMethodParameters(\ReflectionMethod $method) { |
|
188
|
|
|
$result=array (); |
|
189
|
|
|
foreach ( $method->getParameters() as $param ) { |
|
190
|
|
|
$result[]=$param->name; |
|
191
|
|
|
} |
|
192
|
|
|
return $result; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
public static function getJoinTables($class){ |
|
196
|
|
|
$result=[]; |
|
197
|
|
|
$annots=self::getMembersAnnotationWithAnnotation($class, "@joinTable"); |
|
198
|
|
|
foreach ($annots as $annot){ |
|
199
|
|
|
$result[]=$annot->name; |
|
200
|
|
|
} |
|
201
|
|
|
return $result; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
View Code Duplication |
public static function getAllJoinTables($models){ |
|
|
|
|
|
|
205
|
|
|
$result=[]; |
|
206
|
|
|
foreach ($models as $model){ |
|
207
|
|
|
$result=array_merge($result,self::getJoinTables($model)); |
|
208
|
|
|
} |
|
209
|
|
|
return $result; |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.