Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • None
  • PHP

Classes

  • Annotation
  • AnnotationsParser
  • ClassType
  • Extension
  • GlobalFunction
  • Method
  • Parameter
  • Property

Interfaces

  • IAnnotation
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004, 2011 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  */
 11: 
 12: namespace Nette\Reflection;
 13: 
 14: use Nette,
 15:     Nette\ObjectMixin;
 16: 
 17: 
 18: 
 19: /**
 20:  * Reports information about a method.
 21:  *
 22:  * @author     David Grudl
 23:  */
 24: class Method extends \ReflectionMethod
 25: {
 26: 
 27:     /**
 28:      * @param  string|object
 29:      * @param  string
 30:      * @return Method
 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:      * @return array
 41:      */
 42:     public function getDefaultParameters()
 43:     {
 44:         return self::buildDefaultParameters(parent::getParameters());
 45:     }
 46: 
 47: 
 48: 
 49:     /**
 50:      * Invokes method using named parameters.
 51:      * @param  object
 52:      * @param  array
 53:      * @return mixed
 54:      */
 55:     public function invokeNamedArgs($object, $args)
 56:     {
 57:         return $this->invokeArgs($object, self::combineArgs($this->getDefaultParameters(), $args));
 58:     }
 59: 
 60: 
 61: 
 62:     /**
 63:      * @return Nette\Callback
 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:     /********************* Reflection layer ****************d*g**/
 80: 
 81: 
 82: 
 83:     /**
 84:      * @return ClassType
 85:      */
 86:     public function getDeclaringClass()
 87:     {
 88:         return new ClassType(parent::getDeclaringClass()->getName());
 89:     }
 90: 
 91: 
 92: 
 93:     /**
 94:      * @return Method
 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:      * @return Extension
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:     /********************* Nette\Annotations support ****************d*g**/
126: 
127: 
128: 
129:     /**
130:      * Has method specified annotation?
131:      * @param  string
132:      * @return bool
133:      */
134:     public function hasAnnotation($name)
135:     {
136:         $res = AnnotationsParser::getAll($this);
137:         return !empty($res[$name]);
138:     }
139: 
140: 
141: 
142:     /**
143:      * Returns an annotation value.
144:      * @param  string
145:      * @return IAnnotation
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:      * Returns all annotations.
157:      * @return array
158:      */
159:     public function getAnnotations()
160:     {
161:         return AnnotationsParser::getAll($this);
162:     }
163: 
164: 
165: 
166:     /**
167:      * Returns value of annotation 'description'.
168:      * @return string
169:      */
170:     public function getDescription()
171:     {
172:         return $this->getAnnotation('description');
173:     }
174: 
175: 
176: 
177:     /********************* Nette\Object behaviour ****************d*g**/
178: 
179: 
180: 
181:     /**
182:      * @return ClassType
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:     /********************* helpers ****************d*g**/
227: 
228: 
229: 
230:     /** @internal */
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:     /** @internal */
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])) { // NULL treats as none value
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: 
Nette Framework 2.0beta1 API API documentation generated by ApiGen 2.3.0