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 MethodReflection 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: $res = array();
45: foreach (parent::getParameters() as $param) {
46: $res[$param->getName()] = $param->isDefaultValueAvailable()
47: ? $param->getDefaultValue()
48: : NULL;
49:
50: if ($param->isArray()) {
51: settype($res[$param->getName()], 'array');
52: }
53: }
54: return $res;
55: }
56:
57:
58:
59: 60: 61: 62: 63: 64:
65: public function invokeNamedArgs($object, $args)
66: {
67: $res = array();
68: $i = 0;
69: foreach ($this->getDefaultParameters() as $name => $def) {
70: if (isset($args[$name])) { 71: $val = $args[$name];
72: if ($def !== NULL) {
73: settype($val, gettype($def));
74: }
75: $res[$i++] = $val;
76: } else {
77: $res[$i++] = $def;
78: }
79: }
80: return $this->invokeArgs($object, $res);
81: }
82:
83:
84:
85: 86: 87:
88: public function getCallback()
89: {
90: return new Nette\Callback(parent::getDeclaringClass()->getName(), $this->getName());
91: }
92:
93:
94:
95: public function __toString()
96: {
97: return 'Method ' . parent::getDeclaringClass()->getName() . '::' . $this->getName() . '()';
98: }
99:
100:
101:
102:
103:
104:
105:
106: 107: 108:
109: public function getDeclaringClass()
110: {
111: return new ClassReflection(parent::getDeclaringClass()->getName());
112: }
113:
114:
115:
116: 117: 118:
119: public function getPrototype()
120: {
121: $prototype = parent::getPrototype();
122: return new MethodReflection($prototype->getDeclaringClass()->getName(), $prototype->getName());
123: }
124:
125:
126:
127: 128: 129:
130: public function getExtension()
131: {
132: return ($name = $this->getExtensionName()) ? new ExtensionReflection($name) : NULL;
133: }
134:
135:
136:
137: public function getParameters()
138: {
139: $me = array(parent::getDeclaringClass()->getName(), $this->getName());
140: foreach ($res = parent::getParameters() as $key => $val) {
141: $res[$key] = new ParameterReflection($me, $val->getName());
142: }
143: return $res;
144: }
145:
146:
147:
148:
149:
150:
151:
152: 153: 154: 155: 156:
157: public function hasAnnotation($name)
158: {
159: $res = AnnotationsParser::getAll($this);
160: return !empty($res[$name]);
161: }
162:
163:
164:
165: 166: 167: 168: 169:
170: public function getAnnotation($name)
171: {
172: $res = AnnotationsParser::getAll($this);
173: return isset($res[$name]) ? end($res[$name]) : NULL;
174: }
175:
176:
177:
178: 179: 180: 181:
182: public function getAnnotations()
183: {
184: return AnnotationsParser::getAll($this);
185: }
186:
187:
188:
189:
190:
191:
192:
193: 194: 195:
196: public static function getReflection()
197: {
198: return new Nette\Reflection\ClassReflection(get_called_class());
199: }
200:
201:
202:
203: public function __call($name, $args)
204: {
205: return ObjectMixin::call($this, $name, $args);
206: }
207:
208:
209:
210: public function &__get($name)
211: {
212: return ObjectMixin::get($this, $name);
213: }
214:
215:
216:
217: public function __set($name, $value)
218: {
219: return ObjectMixin::set($this, $name, $value);
220: }
221:
222:
223:
224: public function __isset($name)
225: {
226: return ObjectMixin::has($this, $name);
227: }
228:
229:
230:
231: public function __unset($name)
232: {
233: throw new \MemberAccessException("Cannot unset the property {$this->reflection->name}::\$$name.");
234: }
235:
236: }
237: