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