|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\orm\parser; |
|
4
|
|
|
|
|
5
|
|
|
use mindplay\annotations\Annotations; |
|
6
|
|
|
use Ubiquity\orm\OrmUtils; |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Reflection utilities |
|
11
|
|
|
* in dev environment only |
|
12
|
|
|
* @author jcheron <[email protected]> |
|
13
|
|
|
* @version 1.0.1 |
|
14
|
|
|
*/ |
|
15
|
|
|
class Reflexion { |
|
16
|
|
|
use ReflexionFieldsTrait; |
|
17
|
|
|
|
|
18
|
2 |
|
public static function getMethods($instance, $filter=null) { |
|
19
|
2 |
|
$reflect=new \ReflectionClass($instance); |
|
20
|
2 |
|
$methods=$reflect->getMethods($filter); |
|
21
|
2 |
|
return $methods; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
1 |
|
public static function getKeyFields($instance) { |
|
25
|
1 |
|
return self::getMembersNameWithAnnotation(get_class($instance), "@id"); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
4 |
|
public static function getMemberValue($instance, $member) { |
|
29
|
4 |
|
$prop=self::getProperty($instance, $member); |
|
30
|
4 |
|
$prop->setAccessible(true); |
|
31
|
4 |
|
return $prop->getValue($instance); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
14 |
|
public static function getPropValue($instance, $prop) { |
|
35
|
14 |
|
return $prop->getValue($instance); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
1 |
|
public static function setMemberValue($instance, $member,$value) { |
|
39
|
1 |
|
$prop=self::getProperty($instance, $member); |
|
40
|
1 |
|
if($prop){ |
|
41
|
|
|
$prop->setAccessible(true); |
|
42
|
|
|
$prop->setValue($instance,$value); |
|
43
|
|
|
return true; |
|
44
|
|
|
} |
|
45
|
1 |
|
return false; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
2 |
|
public static function getPropertiesAndValues($instance, $props=NULL) { |
|
49
|
2 |
|
$ret=array (); |
|
50
|
2 |
|
$className=get_class($instance); |
|
51
|
2 |
|
if (is_null($props)) |
|
52
|
2 |
|
$props=self::getProperties($instance); |
|
53
|
2 |
|
foreach ( $props as $prop ) { |
|
54
|
2 |
|
$prop->setAccessible(true); |
|
55
|
2 |
|
$v=$prop->getValue($instance); |
|
56
|
2 |
|
if (OrmUtils::isSerializable($className, $prop->getName())) { |
|
57
|
2 |
|
if (OrmUtils::isNotNullOrNullAccepted($v, $className, $prop->getName())) { |
|
58
|
2 |
|
$name=OrmUtils::getFieldName($className, $prop->getName()); |
|
59
|
2 |
|
$ret[$name]=$v; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
2 |
|
return $ret; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
5 |
|
public static function getAnnotationClass($class, $annotation) { |
|
67
|
5 |
|
$annot=Annotations::ofClass($class, $annotation); |
|
68
|
5 |
|
return $annot; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
3 |
|
public static function getAnnotationMember($class, $member, $annotation) { |
|
72
|
3 |
|
$annot=Annotations::ofProperty($class, $member, $annotation); |
|
73
|
3 |
|
return current($annot); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
public static function getAnnotationsMember($class, $member, $annotation) { |
|
77
|
1 |
|
return Annotations::ofProperty($class, $member, $annotation); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
public static function getAnnotationsMethod($class, $method, $annotation) { |
|
81
|
2 |
|
if(is_array($annotation)){ |
|
82
|
2 |
|
$result=[]; |
|
83
|
2 |
|
foreach($annotation as $annot){ |
|
84
|
2 |
|
$annots=Annotations::ofMethod($class, $method, $annot); |
|
85
|
2 |
|
if(sizeof($annots)>0){ |
|
86
|
2 |
|
$result=array_merge($result,$annots); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
2 |
|
return $result; |
|
90
|
|
|
} |
|
91
|
1 |
|
$annots=Annotations::ofMethod($class, $method, $annotation); |
|
92
|
1 |
|
if (\sizeof($annots) > 0) |
|
93
|
1 |
|
return $annots; |
|
94
|
1 |
|
return false; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public static function getMembersAnnotationWithAnnotation($class, $annotation) { |
|
98
|
|
|
return self::getMembersWithAnnotation_($class, $annotation, function(&$ret,$prop,$annot){$ret[$prop->getName()]=$annot;}); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public static function getMembersWithAnnotation($class, $annotation) { |
|
102
|
|
|
return self::getMembersWithAnnotation_($class, $annotation, function(&$ret,$prop){$ret[]=$prop;}); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public static function getMembersNameWithAnnotation($class, $annotation) { |
|
106
|
|
|
return self::getMembersWithAnnotation_($class, $annotation, function(&$ret,$prop){ $ret[]=$prop->getName();}); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
3 |
|
protected static function getMembersWithAnnotation_($class, $annotation,$callback) { |
|
110
|
3 |
|
$props=self::getProperties($class); |
|
111
|
3 |
|
$ret=array (); |
|
112
|
3 |
|
foreach ( $props as $prop ) { |
|
113
|
3 |
|
$annot=self::getAnnotationMember($class, $prop->getName(), $annotation); |
|
114
|
3 |
|
if ($annot !== false) |
|
115
|
3 |
|
$callback($ret,$prop,$annot); |
|
116
|
|
|
} |
|
117
|
3 |
|
return $ret; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
3 |
|
public static function getTableName($class) { |
|
121
|
3 |
|
$ret=Reflexion::getAnnotationClass($class, "@table"); |
|
122
|
3 |
|
if (\sizeof($ret) === 0) { |
|
123
|
3 |
|
$posSlash=strrpos($class, '\\'); |
|
124
|
3 |
|
if ($posSlash !== false) |
|
125
|
3 |
|
$class=substr($class, $posSlash + 1); |
|
126
|
3 |
|
$ret=$class; |
|
127
|
|
|
} else { |
|
128
|
|
|
$ret=$ret[0]->name; |
|
129
|
|
|
} |
|
130
|
3 |
|
return $ret; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
2 |
|
public static function getMethodParameters(\ReflectionMethod $method) { |
|
134
|
2 |
|
$result=array (); |
|
135
|
2 |
|
foreach ( $method->getParameters() as $param ) { |
|
136
|
2 |
|
$result[]=$param->name; |
|
137
|
|
|
} |
|
138
|
2 |
|
return $result; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
2 |
|
public static function getJoinTables($class){ |
|
142
|
2 |
|
$result=[]; |
|
143
|
2 |
|
$annots=self::getMembersAnnotationWithAnnotation($class, "@joinTable"); |
|
144
|
2 |
|
foreach ($annots as $annot){ |
|
145
|
2 |
|
$result[]=$annot->name; |
|
146
|
|
|
} |
|
147
|
2 |
|
return $result; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
2 |
|
public static function getAllJoinTables($models){ |
|
151
|
2 |
|
$result=[]; |
|
152
|
2 |
|
foreach ($models as $model){ |
|
153
|
2 |
|
$result=array_merge($result,self::getJoinTables($model)); |
|
154
|
|
|
} |
|
155
|
2 |
|
return $result; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|