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 getExtension()
120: {
121: return ($name = $this->getExtensionName()) ? new ExtensionReflection($name) : NULL;
122: }
123:
124:
125:
126: public function getParameters()
127: {
128: $me = array(parent::getDeclaringClass()->getName(), $this->getName());
129: foreach ($res = parent::getParameters() as $key => $val) {
130: $res[$key] = new ParameterReflection($me, $val->getName());
131: }
132: return $res;
133: }
134:
135:
136:
137:
138:
139:
140:
141: 142: 143: 144: 145:
146: public function hasAnnotation($name)
147: {
148: $res = AnnotationsParser::getAll($this);
149: return !empty($res[$name]);
150: }
151:
152:
153:
154: 155: 156: 157: 158:
159: public function getAnnotation($name)
160: {
161: $res = AnnotationsParser::getAll($this);
162: return isset($res[$name]) ? end($res[$name]) : NULL;
163: }
164:
165:
166:
167: 168: 169: 170:
171: public function getAnnotations()
172: {
173: return AnnotationsParser::getAll($this);
174: }
175:
176:
177:
178:
179:
180:
181:
182: 183: 184:
185: public static function getReflection()
186: {
187: return new Nette\Reflection\ClassReflection(get_called_class());
188: }
189:
190:
191:
192: public function __call($name, $args)
193: {
194: return ObjectMixin::call($this, $name, $args);
195: }
196:
197:
198:
199: public function &__get($name)
200: {
201: return ObjectMixin::get($this, $name);
202: }
203:
204:
205:
206: public function __set($name, $value)
207: {
208: return ObjectMixin::set($this, $name, $value);
209: }
210:
211:
212:
213: public function __isset($name)
214: {
215: return ObjectMixin::has($this, $name);
216: }
217:
218:
219:
220: public function __unset($name)
221: {
222: throw new \MemberAccessException("Cannot unset the property {$this->reflection->name}::\$$name.");
223: }
224:
225: }
226: