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