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