1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48:
49: class ClassReflection extends ReflectionClass
50: {
51:
52:
53: private static $extMethods;
54:
55:
56: 57: 58: 59:
60: public static function from($class)
61: {
62: return new self($class);
63: }
64:
65:
66: public function __toString()
67: {
68: return 'Class ' . $this->getName();
69: }
70:
71:
72: 73: 74:
75: public function hasEventProperty($name)
76: {
77: if (preg_match('#^on[A-Z]#', $name) && $this->hasProperty($name)) {
78: $rp = $this->getProperty($name);
79: return $rp->isPublic() && !$rp->isStatic();
80: }
81: return FALSE;
82: }
83:
84:
85: 86: 87: 88: 89: 90:
91: public function setExtensionMethod($name, $callback)
92: {
93: $l = & self::$extMethods[strtolower($name)];
94: $l[strtolower($this->getName())] = new Callback($callback);
95: $l[''] = NULL;
96: return $this;
97: }
98:
99:
100: 101: 102: 103: 104:
105: public function getExtensionMethod($name)
106: {
107: if (self::$extMethods === NULL || $name === NULL) {
108: $list = get_defined_functions();
109: foreach ($list['user'] as $fce) {
110: $pair = explode('_prototype_', $fce);
111: if (count($pair) === 2) {
112: self::$extMethods[$pair[1]][$pair[0]] = new Callback($fce);
113: self::$extMethods[$pair[1]][''] = NULL;
114: }
115: }
116: if ($name === NULL) {
117: return NULL;
118: }
119: }
120:
121: $class = strtolower($this->getName());
122: $l = & self::$extMethods[strtolower($name)];
123:
124: if (empty($l)) {
125: return FALSE;
126:
127: } elseif (isset($l[''][$class])) {
128: return $l[''][$class];
129: }
130:
131: $cl = $class;
132: do {
133: if (isset($l[$cl])) {
134: return $l[''][$class] = $l[$cl];
135: }
136: } while (($cl = strtolower(get_parent_class($cl))) !== '');
137:
138: foreach (class_implements($class) as $cl) {
139: $cl = strtolower($cl);
140: if (isset($l[$cl])) {
141: return $l[''][$class] = $l[$cl];
142: }
143: }
144: return $l[''][$class] = FALSE;
145: }
146:
147:
148: 149: 150: 151:
152: public function is($type)
153: {
154: return $this->isSubclassOf($type) || strcasecmp($this->getName(), ltrim($type, '\\')) === 0;
155: }
156:
157:
158:
159:
160:
161: 162: 163:
164: public function getConstructor()
165: {
166: return ($ref = parent::getConstructor()) ? MethodReflection::from($this->getName(), $ref->getName()) : NULL;
167: }
168:
169:
170: 171: 172:
173: public function getExtension()
174: {
175: return ($name = $this->getExtensionName()) ? new ExtensionReflection($name) : NULL;
176: }
177:
178:
179: 180: 181:
182: public function getInterfaces()
183: {
184: $res = array();
185: foreach (parent::getInterfaceNames() as $val) {
186: $res[$val] = new self($val);
187: }
188: return $res;
189: }
190:
191:
192: 193: 194:
195: public function getMethod($name)
196: {
197: return new MethodReflection($this->getName(), $name);
198: }
199:
200:
201: 202: 203:
204: public function getMethods($filter = -1)
205: {
206: foreach ($res = parent::getMethods($filter) as $key => $val) {
207: $res[$key] = new MethodReflection($this->getName(), $val->getName());
208: }
209: return $res;
210: }
211:
212:
213: 214: 215:
216: public function getParentClass()
217: {
218: return ($ref = parent::getParentClass()) ? new self($ref->getName()) : NULL;
219: }
220:
221:
222: 223: 224:
225: public function getProperties($filter = -1)
226: {
227: foreach ($res = parent::getProperties($filter) as $key => $val) {
228: $res[$key] = new PropertyReflection($this->getName(), $val->getName());
229: }
230: return $res;
231: }
232:
233:
234: 235: 236:
237: public function getProperty($name)
238: {
239: return new PropertyReflection($this->getName(), $name);
240: }
241:
242:
243:
244:
245:
246: 247: 248: 249: 250:
251: public function hasAnnotation($name)
252: {
253: $res = AnnotationsParser::getAll($this);
254: return !empty($res[$name]);
255: }
256:
257:
258: 259: 260: 261: 262:
263: public function getAnnotation($name)
264: {
265: $res = AnnotationsParser::getAll($this);
266: return isset($res[$name]) ? end($res[$name]) : NULL;
267: }
268:
269:
270: 271: 272: 273:
274: public function getAnnotations()
275: {
276: return AnnotationsParser::getAll($this);
277: }
278:
279:
280: 281: 282: 283:
284: public function getDescription()
285: {
286: return $this->getAnnotation('description');
287: }
288:
289:
290:
291:
292:
293: 294: 295:
296: public function getReflection()
297: {
298: return new ClassReflection($this);
299: }
300:
301:
302: public function __call($name, $args)
303: {
304: return ObjectMixin::call($this, $name, $args);
305: }
306:
307:
308: public function &__get($name)
309: {
310: return ObjectMixin::get($this, $name);
311: }
312:
313:
314: public function __set($name, $value)
315: {
316: return ObjectMixin::set($this, $name, $value);
317: }
318:
319:
320: public function __isset($name)
321: {
322: return ObjectMixin::has($this, $name);
323: }
324:
325:
326: public function __unset($name)
327: {
328: ObjectMixin::remove($this, $name);
329: }
330:
331: }
332: