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