1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
6: */
7:
8: namespace Nette\Reflection;
9:
10: use Nette;
11: use Nette\Utils\ObjectMixin;
12:
13:
14: /**
15: * Reports information about a method.
16: *
17: * @author David Grudl
18: * @property-read array $defaultParameters
19: * @property-read ClassType $declaringClass
20: * @property-read Method $prototype
21: * @property-read Extension $extension
22: * @property-read Parameter[] $parameters
23: * @property-read IAnnotation[][] $annotations
24: * @property-read string $description
25: * @property-read bool $public
26: * @property-read bool $private
27: * @property-read bool $protected
28: * @property-read bool $abstract
29: * @property-read bool $final
30: * @property-read bool $static
31: * @property-read bool $constructor
32: * @property-read bool $destructor
33: * @property-read int $modifiers
34: * @property-write bool $accessible
35: * @property-read bool $closure
36: * @property-read bool $deprecated
37: * @property-read bool $internal
38: * @property-read bool $userDefined
39: * @property-read string $docComment
40: * @property-read int $endLine
41: * @property-read string $extensionName
42: * @property-read string $fileName
43: * @property-read string $name
44: * @property-read string $namespaceName
45: * @property-read int $numberOfParameters
46: * @property-read int $numberOfRequiredParameters
47: * @property-read string $shortName
48: * @property-read int $startLine
49: * @property-read array $staticVariables
50: */
51: class Method extends \ReflectionMethod
52: {
53:
54: /**
55: * @param string|object
56: * @param string
57: * @return self
58: */
59: public static function from($class, $method)
60: {
61: return new static(is_object($class) ? get_class($class) : $class, $method);
62: }
63:
64:
65: /**
66: * @deprecated
67: */
68: public function toCallback()
69: {
70: return new Nette\Callback(parent::getDeclaringClass()->getName(), $this->getName());
71: }
72:
73:
74: public function __toString()
75: {
76: return parent::getDeclaringClass()->getName() . '::' . $this->getName() . '()';
77: }
78:
79:
80: public function getClosure($object = NULL)
81: {
82: return PHP_VERSION_ID < 50400
83: ? Nette\Utils\Callback::closure($object ?: parent::getDeclaringClass()->getName(), $this->getName())
84: : parent::getClosure($object);
85: }
86:
87:
88: /********************* Reflection layer ****************d*g**/
89:
90:
91: /**
92: * @return ClassType
93: */
94: public function getDeclaringClass()
95: {
96: return new ClassType(parent::getDeclaringClass()->getName());
97: }
98:
99:
100: /**
101: * @return Method
102: */
103: public function getPrototype()
104: {
105: $prototype = parent::getPrototype();
106: return new self($prototype->getDeclaringClass()->getName(), $prototype->getName());
107: }
108:
109:
110: /**
111: * @return Extension
112: */
113: public function getExtension()
114: {
115: return ($name = $this->getExtensionName()) ? new Extension($name) : NULL;
116: }
117:
118:
119: /**
120: * @return Parameter[]
121: */
122: public function getParameters()
123: {
124: $me = array(parent::getDeclaringClass()->getName(), $this->getName());
125: foreach ($res = parent::getParameters() as $key => $val) {
126: $res[$key] = new Parameter($me, $val->getName());
127: }
128: return $res;
129: }
130:
131:
132: /********************* Nette\Annotations support ****************d*g**/
133:
134:
135: /**
136: * Has method specified annotation?
137: * @param string
138: * @return bool
139: */
140: public function hasAnnotation($name)
141: {
142: $res = AnnotationsParser::getAll($this);
143: return !empty($res[$name]);
144: }
145:
146:
147: /**
148: * Returns an annotation value.
149: * @param string
150: * @return IAnnotation
151: */
152: public function getAnnotation($name)
153: {
154: $res = AnnotationsParser::getAll($this);
155: return isset($res[$name]) ? end($res[$name]) : NULL;
156: }
157:
158:
159: /**
160: * Returns all annotations.
161: * @return IAnnotation[][]
162: */
163: public function getAnnotations()
164: {
165: return AnnotationsParser::getAll($this);
166: }
167:
168:
169: /**
170: * Returns value of annotation 'description'.
171: * @return string
172: */
173: public function getDescription()
174: {
175: return $this->getAnnotation('description');
176: }
177:
178:
179: /********************* Nette\Object behaviour ****************d*g**/
180:
181:
182: public function __call($name, $args)
183: {
184: return ObjectMixin::call($this, $name, $args);
185: }
186:
187:
188: public function &__get($name)
189: {
190: return ObjectMixin::get($this, $name);
191: }
192:
193:
194: public function __set($name, $value)
195: {
196: ObjectMixin::set($this, $name, $value);
197: }
198:
199:
200: public function __isset($name)
201: {
202: return ObjectMixin::has($this, $name);
203: }
204:
205:
206: public function __unset($name)
207: {
208: ObjectMixin::remove($this, $name);
209: }
210:
211: }
212: