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