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