Packages

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

Classes

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

Interfaces

  • IAnnotation
  • Overview
  • Package
  • 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:  * @package Nette\Reflection
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * Reports information about a class.
 17:  *
 18:  * @author     David Grudl
 19:  * @package Nette\Reflection
 20:  */
 21: class ClassReflection extends ReflectionClass
 22: {
 23: 
 24:     /** @var array (method => array(type => callback)) */
 25:     private static $extMethods;
 26: 
 27: 
 28: 
 29:     /**
 30:      * @param  string|object
 31:      * @return ClassReflection
 32:      */
 33:     public static function from($class)
 34:     {
 35:         return new self($class);
 36:     }
 37: 
 38: 
 39: 
 40:     public function __toString()
 41:     {
 42:         return 'Class ' . $this->getName();
 43:     }
 44: 
 45: 
 46: 
 47:     /**
 48:      * @return bool
 49:      */
 50:     public function hasEventProperty($name)
 51:     {
 52:         if (preg_match('#^on[A-Z]#', $name) && $this->hasProperty($name)) {
 53:             $rp = $this->getProperty($name);
 54:             return $rp->isPublic() && !$rp->isStatic();
 55:         }
 56:         return FALSE;
 57:     }
 58: 
 59: 
 60: 
 61:     /**
 62:      * Adds a method to class.
 63:      * @param  string  method name
 64:      * @param  mixed   callback or closure
 65:      * @return ClassReflection  provides a fluent interface
 66:      */
 67:     public function setExtensionMethod($name, $callback)
 68:     {
 69:         $l = & self::$extMethods[strtolower($name)];
 70:         $l[strtolower($this->getName())] = callback($callback);
 71:         $l[''] = NULL;
 72:         return $this;
 73:     }
 74: 
 75: 
 76: 
 77:     /**
 78:      * Returns extension method.
 79:      * @param  string  method name
 80:      * @return mixed
 81:      */
 82:     public function getExtensionMethod($name)
 83:     {
 84:         if (self::$extMethods === NULL || $name === NULL) { // for backwards compatibility
 85:             $list = get_defined_functions(); // names are lowercase!
 86:             foreach ($list['user'] as $fce) {
 87:                 $pair = explode('_prototype_', $fce);
 88:                 if (count($pair) === 2) {
 89:                     self::$extMethods[$pair[1]][$pair[0]] = callback($fce);
 90:                     self::$extMethods[$pair[1]][''] = NULL;
 91:                 }
 92:             }
 93:             if ($name === NULL) {
 94:                 return NULL;
 95:             }
 96:         }
 97: 
 98:         $class = strtolower($this->getName());
 99:         $l = & self::$extMethods[strtolower($name)];
100: 
101:         if (empty($l)) {
102:             return FALSE;
103: 
104:         } elseif (isset($l[''][$class])) { // cached value
105:             return $l[''][$class];
106:         }
107: 
108:         $cl = $class;
109:         do {
110:             if (isset($l[$cl])) {
111:                 return $l[''][$class] = $l[$cl];
112:             }
113:         } while (($cl = strtolower(get_parent_class($cl))) !== '');
114: 
115:         foreach (class_implements($class) as $cl) {
116:             $cl = strtolower($cl);
117:             if (isset($l[$cl])) {
118:                 return $l[''][$class] = $l[$cl];
119:             }
120:         }
121:         return $l[''][$class] = FALSE;
122:     }
123: 
124: 
125: 
126:     /********************* Reflection layer ****************d*g**/
127: 
128: 
129: 
130:     /**
131:      * @return MethodReflection
132:      */
133:     public function getConstructor()
134:     {
135:         return ($ref = parent::getConstructor()) ? MethodReflection::from($this->getName(), $ref->getName()) : NULL;
136:     }
137: 
138: 
139: 
140:     /**
141:      * @return ExtensionReflection
142:      */
143:     public function getExtension()
144:     {
145:         return ($name = $this->getExtensionName()) ? new ExtensionReflection($name) : NULL;
146:     }
147: 
148: 
149: 
150:     public function getInterfaces()
151:     {
152:         $res = array();
153:         foreach (parent::getInterfaceNames() as $val) {
154:             $res[$val] = new self($val);
155:         }
156:         return $res;
157:     }
158: 
159: 
160: 
161:     /**
162:      * @return MethodReflection
163:      */
164:     public function getMethod($name)
165:     {
166:         return new MethodReflection($this->getName(), $name);
167:     }
168: 
169: 
170: 
171:     public function getMethods($filter = -1)
172:     {
173:         foreach ($res = parent::getMethods($filter) as $key => $val) {
174:             $res[$key] = new MethodReflection($this->getName(), $val->getName());
175:         }
176:         return $res;
177:     }
178: 
179: 
180: 
181:     /**
182:      * @return ClassReflection
183:      */
184:     public function getParentClass()
185:     {
186:         return ($ref = parent::getParentClass()) ? new self($ref->getName()) : NULL;
187:     }
188: 
189: 
190: 
191:     public function getProperties($filter = -1)
192:     {
193:         foreach ($res = parent::getProperties($filter) as $key => $val) {
194:             $res[$key] = new PropertyReflection($this->getName(), $val->getName());
195:         }
196:         return $res;
197:     }
198: 
199: 
200: 
201:     /**
202:      * @return PropertyReflection
203:      */
204:     public function getProperty($name)
205:     {
206:         return new PropertyReflection($this->getName(), $name);
207:     }
208: 
209: 
210: 
211:     /********************* Annotations support ****************d*g**/
212: 
213: 
214: 
215:     /**
216:      * Has class specified annotation?
217:      * @param  string
218:      * @return bool
219:      */
220:     public function hasAnnotation($name)
221:     {
222:         $res = AnnotationsParser::getAll($this);
223:         return !empty($res[$name]);
224:     }
225: 
226: 
227: 
228:     /**
229:      * Returns an annotation value.
230:      * @param  string
231:      * @return IAnnotation
232:      */
233:     public function getAnnotation($name)
234:     {
235:         $res = AnnotationsParser::getAll($this);
236:         return isset($res[$name]) ? end($res[$name]) : NULL;
237:     }
238: 
239: 
240: 
241:     /**
242:      * Returns all annotations.
243:      * @return array
244:      */
245:     public function getAnnotations()
246:     {
247:         return AnnotationsParser::getAll($this);
248:     }
249: 
250: 
251: 
252:     /**
253:      * Returns value of annotation 'description'.
254:      * @return string
255:      */
256:     public function getDescription()
257:     {
258:         return $this->getAnnotation('description');
259:     }
260: 
261: 
262: 
263:     /********************* Object behaviour ****************d*g**/
264: 
265: 
266: 
267:     /**
268:      * @return ClassReflection
269:      */
270:     public function getReflection()
271:     {
272:         return new ClassReflection($this);
273:     }
274: 
275: 
276: 
277:     public function __call($name, $args)
278:     {
279:         return ObjectMixin::call($this, $name, $args);
280:     }
281: 
282: 
283: 
284:     public function &__get($name)
285:     {
286:         return ObjectMixin::get($this, $name);
287:     }
288: 
289: 
290: 
291:     public function __set($name, $value)
292:     {
293:         return ObjectMixin::set($this, $name, $value);
294:     }
295: 
296: 
297: 
298:     public function __isset($name)
299:     {
300:         return ObjectMixin::has($this, $name);
301:     }
302: 
303: 
304: 
305:     public function __unset($name)
306:     {
307:         ObjectMixin::remove($this, $name);
308:     }
309: 
310: }
311: 
Nette Framework 2.0beta1 (for PHP 5.2) API API documentation generated by ApiGen 2.3.0