Source for file ClassReflection.php

Documentation is available at ClassReflection.php

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