Namespaces

  • Nette
    • Application
    • Caching
    • Collections
    • Config
    • Forms
    • IO
    • Loaders
    • Mail
    • Reflection
    • Security
    • Templates
    • Web
  • None
  • PHP

Classes

  • Annotation
  • AnnotationsParser
  • ClassReflection
  • ExtensionReflection
  • FunctionReflection
  • MethodReflection
  • ParameterReflection
  • PropertyReflection

Interfaces

  • IAnnotation
  • Overview
  • Namespace
  • Class
  • Tree
  • Other releases
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  *
  6:  * Copyright (c) 2004 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 MethodReflection extends \ReflectionMethod
 25: {
 26: 
 27:     /**
 28:      * @param  string|object
 29:      * @param  string
 30:      * @return Nette\Reflection\MethodReflection
 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:         $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:      * Invokes method using named parameters.
 61:      * @param  object
 62:      * @param  array
 63:      * @return mixed
 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])) { // NULL treats as none value
 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:      * @return Nette\Callback
 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:     /********************* Reflection layer ****************d*g**/
103: 
104: 
105: 
106:     /**
107:      * @return Nette\Reflection\ClassReflection
108:      */
109:     public function getDeclaringClass()
110:     {
111:         return new ClassReflection(parent::getDeclaringClass()->getName());
112:     }
113: 
114: 
115: 
116:     /**
117:      * @return Nette\Reflection\ExtensionReflection
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:     /********************* Nette\Annotations support ****************d*g**/
138: 
139: 
140: 
141:     /**
142:      * Has method specified annotation?
143:      * @param  string
144:      * @return bool
145:      */
146:     public function hasAnnotation($name)
147:     {
148:         $res = AnnotationsParser::getAll($this);
149:         return !empty($res[$name]);
150:     }
151: 
152: 
153: 
154:     /**
155:      * Returns an annotation value.
156:      * @param  string
157:      * @return IAnnotation
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:      * Returns all annotations.
169:      * @return array
170:      */
171:     public function getAnnotations()
172:     {
173:         return AnnotationsParser::getAll($this);
174:     }
175: 
176: 
177: 
178:     /********************* Nette\Object behaviour ****************d*g**/
179: 
180: 
181: 
182:     /**
183:      * @return Nette\Reflection\ClassReflection
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: 
Nette Framework 0.9.7 API documentation generated by ApiGen 2.3.0