Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
Reflexion.php
Go to the documentation of this file.
1<?php
2
3namespace Ubiquity\orm\parser;
4
8
18
19 protected static $classProperties = [];
20
21 public static function getMethods($instance, $filter = null) {
22 $reflect = new \ReflectionClass ($instance);
23 return $reflect->getMethods($filter);
24 }
25
26 public static function getKeyFields($instance) {
27 return self::getMembersNameWithAnnotation(\get_class($instance), 'id');
28 }
29
30 public static function getMemberValue($instance, $member) {
31 $prop = self::getProperty($instance, $member);
32 $prop->setAccessible(true);
33 return $prop->getValue($instance);
34 }
35
36 public static function getPropValue($instance, $prop) {
37 return $prop->getValue($instance);
38 }
39
40 public static function setMemberValue($instance, $member, $value): bool {
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 getPropertiesAndValues($instance, $props = null) {
51 $ret = [];
52 $className = \get_class($instance);
53 $modelMetas = OrmUtils::getModelMetadata($className);
54 if (isset (self::$classProperties [$className])) {
55 foreach (self::$classProperties [$className] as $name => $prop) {
56 $ret [$name] = $prop->getValue($instance);
57 }
58 return $ret;
59 }
60 if (\is_null($props)) {
61 $props = self::getProperties($instance);
62 }
63 foreach ($props as $prop) {
64 $prop->setAccessible(true);
65 $v = $prop->getValue($instance);
66 if (\array_search($prop->getName(), $modelMetas ['#notSerializable']) === false && OrmUtils::isNotNullOrNullAccepted($v, $className, $prop->getName())) {
67 $name = $modelMetas ['#fieldNames'] [$prop->getName()] ?? $prop->getName();
68 $ret [$name] = $v;
69 self::$classProperties [$className] [$name] = $prop;
70 }
71 }
72 return $ret;
73 }
74
81 public static function getAnnotsEngine() {
82 return CacheManager::getAnnotationsEngineInstance();
83 }
84
85 public static function getAnnotationClass($class, $annotation) {
86 return self::getAnnotsEngine()->getAnnotsOfClass($class, $annotation);
87 }
88
89 public static function getAnnotationMember($class, $member, $annotation) {
90 $annot = self::getAnnotsEngine()->getAnnotsOfProperty($class, $member, $annotation);
91 return \current($annot);
92 }
93
94 public static function getAnnotationMethod($class, $method, $annotation) {
95 $annot = self::getAnnotsEngine()->getAnnotsOfMethod($class, $method, $annotation);
96 return \current($annot);
97 }
98
99 public static function getAnnotationsMember($class, $member, $annotation) {
100 return self::getAnnotsEngine()->getAnnotsOfProperty($class, $member, $annotation);
101 }
102
103 public static function getAnnotationsMethod($class, $method, $annotation) {
104 $annotsEngine = self::getAnnotsEngine();
105 if (\is_array($annotation)) {
106 $result = [];
107 foreach ($annotation as $annot) {
108 $annots = $annotsEngine->getAnnotsOfMethod($class, $method, $annot);
109 if (\count($annots) > 0) {
110 $result = \array_merge($result, $annots);
111 }
112 }
113 return $result;
114 }
115 $annots = $annotsEngine->getAnnotsOfMethod($class, $method, $annotation);
116 if (\count($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 $ret [$prop->getName()] = $annot;
125 });
126 }
127
128 public static function getMembersWithAnnotation($class, $annotation) {
129 return self::getMembersWithAnnotation_($class, $annotation, function (&$ret, $prop) {
130 $ret [] = $prop;
131 });
132 }
133
134 public static function getMembersNameWithAnnotation($class, $annotation) {
135 return self::getMembersWithAnnotation_($class, $annotation, function (&$ret, $prop) {
136 $ret [] = $prop->getName();
137 });
138 }
139
140 protected static function getMembersWithAnnotation_($class, $annotation, $callback) {
141 $props = self::getProperties($class);
142 $ret = [];
143 foreach ($props as $prop) {
144 $annot = self::getAnnotationMember($class, $prop->getName(), $annotation);
145 if ($annot !== false) {
146 $callback ($ret, $prop, $annot);
147 }
148 }
149 return $ret;
150 }
151
152 public static function getTableName($class) {
153 $ret = self::getAnnotationClass($class, 'table');
154 if (\count($ret) === 0) {
155 $posSlash = \strrpos($class, '\\');
156 if ($posSlash !== false) {
157 $class = \substr($class, $posSlash + 1);
158 }
159 $ret = $class;
160 } else {
161 $ret = $ret [0]->name;
162 }
163 return $ret;
164 }
165
166 public static function getMethodParameters(\ReflectionFunctionAbstract $method) {
167 $result = [];
168 foreach ($method->getParameters() as $param) {
169 $result [] = $param->name;
170 }
171 return $result;
172 }
173
174 public static function getJoinTables($class) {
175 $result = [];
176 $annots = self::getMembersAnnotationWithAnnotation($class, 'joinTable');
177 foreach ($annots as $annot) {
178 $result [] = $annot->name;
179 }
180 return $result;
181 }
182
183 public static function getAllJoinTables($models) {
184 $result = [];
185 foreach ($models as $model) {
186 $result = \array_merge($result, self::getJoinTables($model));
187 }
188 return $result;
189 }
190}
Manager for caches (Router, Rest, models).
Object/relational mapping utilities.
Definition OrmUtils.php:17
static isNotNullOrNullAccepted($v, $className, $member)
Definition OrmUtils.php:102
static getModelMetadata($className)
Definition OrmUtils.php:21
Reflection utilities in dev environment only.
Definition Reflexion.php:17
static getMembersAnnotationWithAnnotation($class, $annotation)
static getAnnotationClass($class, $annotation)
Definition Reflexion.php:85
static getPropValue($instance, $prop)
Definition Reflexion.php:36
static setMemberValue($instance, $member, $value)
Definition Reflexion.php:40
static getAnnotsEngine()
Returns the annotation engine (php8 attributes or php annotations).
Definition Reflexion.php:81
static getKeyFields($instance)
Definition Reflexion.php:26
static getAnnotationMethod($class, $method, $annotation)
Definition Reflexion.php:94
static getPropertiesAndValues($instance, $props=null)
Definition Reflexion.php:50
static getMemberValue($instance, $member)
Definition Reflexion.php:30
static getMethods($instance, $filter=null)
Definition Reflexion.php:21
static getMethodParameters(\ReflectionFunctionAbstract $method)
static getMembersWithAnnotation($class, $annotation)
static getAllJoinTables($models)
static getAnnotationsMember($class, $member, $annotation)
Definition Reflexion.php:99
static getAnnotationMember($class, $member, $annotation)
Definition Reflexion.php:89
static getMembersNameWithAnnotation($class, $annotation)
static getAnnotationsMethod($class, $method, $annotation)
static getMembersWithAnnotation_($class, $annotation, $callback)
Ubiquity\annotations$AnnotationsInterface This class is part of Ubiquity.