Source for file MethodReflection.php

Documentation is available at MethodReflection.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 method.
  17. 17:  *
  18. 18:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  19. 19:  * @package    Nette\Reflection
  20. 20:  */
  21. 21: class MethodReflection extends ReflectionMethod
  22. 22: {
  23. 23:  
  24. 24:     /**
  25. 25:      * @param  string|object 
  26. 26:      * @param  string 
  27. 27:      * @return MethodReflection 
  28. 28:      */
  29. 29:     public static function from($class$method)
  30. 30:     {
  31. 31:         return new self(is_object($classget_class($class$class$method);
  32. 32:     }
  33. 33:  
  34. 34:  
  35. 35:  
  36. 36:     /**
  37. 37:      * @return array 
  38. 38:      */
  39. 39:     public function getDefaultParameters()
  40. 40:     {
  41. 41:         $res array();
  42. 42:         foreach (parent::getParameters(as $param{
  43. 43:             $res[$param->getName()$param->isDefaultValueAvailable()
  44. 44:                 ? $param->getDefaultValue()
  45. 45:                 : NULL;
  46. 46:  
  47. 47:             if ($param->isArray()) {
  48. 48:                 settype($res[$param->getName()]'array');
  49. 49:             }
  50. 50:         }
  51. 51:         return $res;
  52. 52:     }
  53. 53:  
  54. 54:  
  55. 55:  
  56. 56:     /**
  57. 57:      * Invokes method using named parameters.
  58. 58:      * @param  object 
  59. 59:      * @param  array 
  60. 60:      * @return mixed 
  61. 61:      */
  62. 62:     public function invokeNamedArgs($object$args)
  63. 63:     {
  64. 64:         $res array();
  65. 65:         $i 0;
  66. 66:         foreach ($this->getDefaultParameters(as $name => $def{
  67. 67:             if (isset($args[$name])) // NULL treats as none value
  68. 68:                 $val $args[$name];
  69. 69:                 if ($def !== NULL{
  70. 70:                     settype($valgettype($def));
  71. 71:                 }
  72. 72:                 $res[$i++$val;
  73. 73:             else {
  74. 74:                 $res[$i++$def;
  75. 75:             }
  76. 76:         }
  77. 77:         return $this->invokeArgs($object$res);
  78. 78:     }
  79. 79:  
  80. 80:  
  81. 81:  
  82. 82:     /**
  83. 83:      * @return Callback 
  84. 84:      */
  85. 85:     public function getCallback()
  86. 86:     {
  87. 87:         return new Callback(parent::getDeclaringClass()->getName()$this->getName());
  88. 88:     }
  89. 89:  
  90. 90:  
  91. 91:  
  92. 92:     public function __toString()
  93. 93:     {
  94. 94:         return 'Method ' parent::getDeclaringClass()->getName('::' $this->getName('()';
  95. 95:     }
  96. 96:  
  97. 97:  
  98. 98:  
  99. 99:     /********************* Reflection layer ****************d*g**/
  100. 100:  
  101. 101:  
  102. 102:  
  103. 103:     /**
  104. 104:      * @return MethodReflection 
  105. 105:      * @ignore internal
  106. 106:      */
  107. 107:     public static function import(ReflectionMethod $ref)
  108. 108:     {
  109. 109:         return new self($ref->getDeclaringClass()->getName()$ref->getName());
  110. 110:     }
  111. 111:  
  112. 112:  
  113. 113:  
  114. 114:     /**
  115. 115:      * @return ClassReflection 
  116. 116:      */
  117. 117:     public function getDeclaringClass()
  118. 118:     {
  119. 119:         return ClassReflection::import(parent::getDeclaringClass());
  120. 120:     }
  121. 121:  
  122. 122:  
  123. 123:  
  124. 124:     /**
  125. 125:      * @return ExtensionReflection 
  126. 126:      */
  127. 127:     public function getExtension()
  128. 128:     {
  129. 129:         return ($ref parent::getExtension()) ExtensionReflection::import($refNULL;
  130. 130:     }
  131. 131:  
  132. 132:  
  133. 133:  
  134. 134:     public function getParameters()
  135. 135:     {
  136. 136:         return array_map(array('MethodParameterReflection''import')parent::getParameters());
  137. 137:     }
  138. 138:  
  139. 139:  
  140. 140:  
  141. 141:     /********************* Nette\Annotations support ****************d*g**/
  142. 142:  
  143. 143:  
  144. 144:  
  145. 145:     /**
  146. 146:      * Has method specified annotation?
  147. 147:      * @param  string 
  148. 148:      * @return bool 
  149. 149:      */
  150. 150:     public function hasAnnotation($name)
  151. 151:     {
  152. 152:         $res AnnotationsParser::getAll($this);
  153. 153:         return !empty($res[$name]);
  154. 154:     }
  155. 155:  
  156. 156:  
  157. 157:  
  158. 158:     /**
  159. 159:      * Returns an annotation value.
  160. 160:      * @param  string 
  161. 161:      * @return IAnnotation 
  162. 162:      */
  163. 163:     public function getAnnotation($name)
  164. 164:     {
  165. 165:         $res AnnotationsParser::getAll($this);
  166. 166:         return isset($res[$name]end($res[$name]NULL;
  167. 167:     }
  168. 168:  
  169. 169:  
  170. 170:  
  171. 171:     /**
  172. 172:      * Returns all annotations.
  173. 173:      * @return array 
  174. 174:      */
  175. 175:     public function getAnnotations()
  176. 176:     {
  177. 177:         return AnnotationsParser::getAll($this);
  178. 178:     }
  179. 179:  
  180. 180:  
  181. 181:  
  182. 182:     /********************* Nette\Object behaviour ****************d*g**/
  183. 183:  
  184. 184:  
  185. 185:  
  186. 186:     /**
  187. 187:      * @return ClassReflection 
  188. 188:      */
  189. 189:     public function getReflection()
  190. 190:     {
  191. 191:         return new ClassReflection($this);
  192. 192:     }
  193. 193:  
  194. 194:  
  195. 195:  
  196. 196:     public function __call($name$args)
  197. 197:     {
  198. 198:         return ObjectMixin::call($this$name$args);
  199. 199:     }
  200. 200:  
  201. 201:  
  202. 202:  
  203. 203:     public function &__get($name)
  204. 204:     {
  205. 205:         return ObjectMixin::get($this$name);
  206. 206:     }
  207. 207:  
  208. 208:  
  209. 209:  
  210. 210:     public function __set($name$value)
  211. 211:     {
  212. 212:         return ObjectMixin::set($this$name$value);
  213. 213:     }
  214. 214:  
  215. 215:  
  216. 216:  
  217. 217:     public function __isset($name)
  218. 218:     {
  219. 219:         return ObjectMixin::has($this$name);
  220. 220:     }
  221. 221:  
  222. 222:  
  223. 223:  
  224. 224:     public function __unset($name)
  225. 225:     {
  226. 226:         throw new MemberAccessException("Cannot unset the property {$this->reflection->name}::\$$name.");
  227. 227:     }
  228. 228: