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