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: class Method extends \ReflectionMethod
25: {
26:
27: 28: 29: 30: 31:
32: public static function from($class, $method)
33: {
34: return new static(is_object($class) ? get_class($class) : $class, $method);
35: }
36:
37:
38:
39: 40: 41:
42: public function getDefaultParameters()
43: {
44: return self::buildDefaultParameters(parent::getParameters());
45: }
46:
47:
48:
49: 50: 51: 52: 53: 54:
55: public function invokeNamedArgs($object, $args)
56: {
57: return $this->invokeArgs($object, self::combineArgs($this->getDefaultParameters(), $args));
58: }
59:
60:
61:
62: 63: 64:
65: public function toCallback()
66: {
67: return new Nette\Callback(parent::getDeclaringClass()->getName(), $this->getName());
68: }
69:
70:
71:
72: public function __toString()
73: {
74: return 'Method ' . parent::getDeclaringClass()->getName() . '::' . $this->getName() . '()';
75: }
76:
77:
78:
79:
80:
81:
82:
83: 84: 85:
86: public function getDeclaringClass()
87: {
88: return new ClassType(parent::getDeclaringClass()->getName());
89: }
90:
91:
92:
93: 94: 95:
96: public function getPrototype()
97: {
98: $prototype = parent::getPrototype();
99: return new Method($prototype->getDeclaringClass()->getName(), $prototype->getName());
100: }
101:
102:
103:
104: 105: 106:
107: public function getExtension()
108: {
109: return ($name = $this->getExtensionName()) ? new Extension($name) : NULL;
110: }
111:
112:
113:
114: public function getParameters()
115: {
116: $me = array(parent::getDeclaringClass()->getName(), $this->getName());
117: foreach ($res = parent::getParameters() as $key => $val) {
118: $res[$key] = new Parameter($me, $val->getName());
119: }
120: return $res;
121: }
122:
123:
124:
125:
126:
127:
128:
129: 130: 131: 132: 133:
134: public function hasAnnotation($name)
135: {
136: $res = AnnotationsParser::getAll($this);
137: return !empty($res[$name]);
138: }
139:
140:
141:
142: 143: 144: 145: 146:
147: public function getAnnotation($name)
148: {
149: $res = AnnotationsParser::getAll($this);
150: return isset($res[$name]) ? end($res[$name]) : NULL;
151: }
152:
153:
154:
155: 156: 157: 158:
159: public function getAnnotations()
160: {
161: return AnnotationsParser::getAll($this);
162: }
163:
164:
165:
166: 167: 168: 169:
170: public function getDescription()
171: {
172: return $this->getAnnotation('description');
173: }
174:
175:
176:
177:
178:
179:
180:
181: 182: 183:
184: public static function getReflection()
185: {
186: return new ClassType(get_called_class());
187: }
188:
189:
190:
191: public function __call($name, $args)
192: {
193: return ObjectMixin::call($this, $name, $args);
194: }
195:
196:
197:
198: public function &__get($name)
199: {
200: return ObjectMixin::get($this, $name);
201: }
202:
203:
204:
205: public function __set($name, $value)
206: {
207: return ObjectMixin::set($this, $name, $value);
208: }
209:
210:
211:
212: public function __isset($name)
213: {
214: return ObjectMixin::has($this, $name);
215: }
216:
217:
218:
219: public function __unset($name)
220: {
221: ObjectMixin::remove($this, $name);
222: }
223:
224:
225:
226:
227:
228:
229:
230:
231: public static function buildDefaultParameters($params)
232: {
233: $res = array();
234: foreach ($params as $param) {
235: $res[$param->getName()] = $param->isDefaultValueAvailable()
236: ? $param->getDefaultValue()
237: : NULL;
238:
239: if ($param->isArray()) {
240: settype($res[$param->getName()], 'array');
241: }
242: }
243: return $res;
244: }
245:
246:
247:
248:
249: public static function combineArgs($params, $args)
250: {
251: $res = array();
252: $i = 0;
253: foreach ($params as $name => $def) {
254: if (isset($args[$name])) {
255: $val = $args[$name];
256: if ($def !== NULL) {
257: settype($val, gettype($def));
258: }
259: $res[$i++] = $val;
260: } else {
261: $res[$i++] = $def;
262: }
263: }
264: return $res;
265: }
266:
267: }
268: