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