Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • 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 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 class.
 21:  *
 22:  * @author     David Grudl
 23:  * @property-read Method $constructor
 24:  * @property-read Extension $extension
 25:  * @property-read ClassType[] $interfaces
 26:  * @property-read Method[] $methods
 27:  * @property-read ClassType $parentClass
 28:  * @property-read Property[] $properties
 29:  * @property-read IAnnotation[][] $annotations
 30:  * @property-read string $description
 31:  * @property-read string $name
 32:  * @property-read bool $internal
 33:  * @property-read bool $userDefined
 34:  * @property-read bool $instantiable
 35:  * @property-read string $fileName
 36:  * @property-read int $startLine
 37:  * @property-read int $endLine
 38:  * @property-read string $docComment
 39:  * @property-read mixed[] $constants
 40:  * @property-read string[] $interfaceNames
 41:  * @property-read bool $interface
 42:  * @property-read bool $abstract
 43:  * @property-read bool $final
 44:  * @property-read int $modifiers
 45:  * @property-read array $staticProperties
 46:  * @property-read array $defaultProperties
 47:  * @property-read bool $iterateable
 48:  * @property-read string $extensionName
 49:  * @property-read string $namespaceName
 50:  * @property-read string $shortName
 51:  */
 52: class ClassType extends \ReflectionClass
 53: {
 54: 
 55:     /** @var array (method => array(type => callable)) */
 56:     private static $extMethods;
 57: 
 58: 
 59: 
 60:     /**
 61:      * @param  string|object
 62:      * @return ClassType
 63:      */
 64:     public static function from($class)
 65:     {
 66:         return new static($class);
 67:     }
 68: 
 69: 
 70: 
 71:     public function __toString()
 72:     {
 73:         return 'Class ' . $this->getName();
 74:     }
 75: 
 76: 
 77: 
 78:     /**
 79:      * @return bool
 80:      */
 81:     public function hasEventProperty($name)
 82:     {
 83:         if (preg_match('#^on[A-Z]#', $name) && $this->hasProperty($name)) {
 84:             $rp = $this->getProperty($name);
 85:             return $rp->isPublic() && !$rp->isStatic();
 86:         }
 87:         return FALSE;
 88:     }
 89: 
 90: 
 91: 
 92:     /**
 93:      * Adds a method to class.
 94:      * @param  string  method name
 95:      * @param  mixed   callable
 96:      * @return ClassType  provides a fluent interface
 97:      */
 98:     public function setExtensionMethod($name, $callback)
 99:     {
100:         $l = & self::$extMethods[strtolower($name)];
101:         $l[strtolower($this->getName())] = callback($callback);
102:         $l[''] = NULL;
103:         return $this;
104:     }
105: 
106: 
107: 
108:     /**
109:      * Returns extension method.
110:      * @param  string  method name
111:      * @return mixed
112:      */
113:     public function getExtensionMethod($name)
114:     {
115:         $class = strtolower($this->getName());
116:         $l = & self::$extMethods[strtolower($name)];
117: 
118:         if (empty($l)) {
119:             return FALSE;
120: 
121:         } elseif (isset($l[''][$class])) { // cached value
122:             return $l[''][$class];
123:         }
124: 
125:         $cl = $class;
126:         do {
127:             if (isset($l[$cl])) {
128:                 return $l[''][$class] = $l[$cl];
129:             }
130:         } while (($cl = strtolower(get_parent_class($cl))) !== '');
131: 
132:         foreach (class_implements($class) as $cl) {
133:             $cl = strtolower($cl);
134:             if (isset($l[$cl])) {
135:                 return $l[''][$class] = $l[$cl];
136:             }
137:         }
138:         return $l[''][$class] = FALSE;
139:     }
140: 
141: 
142: 
143:     /**
144:      * @param  string
145:      * @return bool
146:      */
147:     public function is($type)
148:     {
149:         return $this->isSubclassOf($type) || strcasecmp($this->getName(), ltrim($type, '\\')) === 0;
150:     }
151: 
152: 
153: 
154:     /********************* Reflection layer ****************d*g**/
155: 
156: 
157: 
158:     /**
159:      * @return Method|NULL
160:      */
161:     public function getConstructor()
162:     {
163:         return ($ref = parent::getConstructor()) ? Method::from($this->getName(), $ref->getName()) : NULL;
164:     }
165: 
166: 
167: 
168:     /**
169:      * @return Extension|NULL
170:      */
171:     public function getExtension()
172:     {
173:         return ($name = $this->getExtensionName()) ? new Extension($name) : NULL;
174:     }
175: 
176: 
177: 
178:     /**
179:      * @return ClassType[]
180:      */
181:     public function getInterfaces()
182:     {
183:         $res = array();
184:         foreach (parent::getInterfaceNames() as $val) {
185:             $res[$val] = new static($val);
186:         }
187:         return $res;
188:     }
189: 
190: 
191: 
192:     /**
193:      * @return Method
194:      */
195:     public function getMethod($name)
196:     {
197:         return new Method($this->getName(), $name);
198:     }
199: 
200: 
201:     /**
202:      * @return Method[]
203:      */
204:     public function getMethods($filter = -1)
205:     {
206:         foreach ($res = parent::getMethods($filter) as $key => $val) {
207:             $res[$key] = new Method($this->getName(), $val->getName());
208:         }
209:         return $res;
210:     }
211: 
212: 
213: 
214:     /**
215:      * @return ClassType|NULL
216:      */
217:     public function getParentClass()
218:     {
219:         return ($ref = parent::getParentClass()) ? new static($ref->getName()) : NULL;
220:     }
221: 
222: 
223:     /**
224:      * @return Property[]
225:      */
226:     public function getProperties($filter = -1)
227:     {
228:         foreach ($res = parent::getProperties($filter) as $key => $val) {
229:             $res[$key] = new Property($this->getName(), $val->getName());
230:         }
231:         return $res;
232:     }
233: 
234: 
235: 
236:     /**
237:      * @return Property
238:      */
239:     public function getProperty($name)
240:     {
241:         return new Property($this->getName(), $name);
242:     }
243: 
244: 
245: 
246:     /********************* Nette\Annotations support ****************d*g**/
247: 
248: 
249: 
250:     /**
251:      * Has class specified annotation?
252:      * @param  string
253:      * @return bool
254:      */
255:     public function hasAnnotation($name)
256:     {
257:         $res = AnnotationsParser::getAll($this);
258:         return !empty($res[$name]);
259:     }
260: 
261: 
262: 
263:     /**
264:      * Returns an annotation value.
265:      * @param  string
266:      * @return IAnnotation
267:      */
268:     public function getAnnotation($name)
269:     {
270:         $res = AnnotationsParser::getAll($this);
271:         return isset($res[$name]) ? end($res[$name]) : NULL;
272:     }
273: 
274: 
275: 
276:     /**
277:      * Returns all annotations.
278:      * @return IAnnotation[][]
279:      */
280:     public function getAnnotations()
281:     {
282:         return AnnotationsParser::getAll($this);
283:     }
284: 
285: 
286: 
287:     /**
288:      * Returns value of annotation 'description'.
289:      * @return string
290:      */
291:     public function getDescription()
292:     {
293:         return $this->getAnnotation('description');
294:     }
295: 
296: 
297: 
298:     /********************* Nette\Object behaviour ****************d*g**/
299: 
300: 
301: 
302:     /**
303:      * @return ClassType
304:      */
305:     public static function getReflection()
306:     {
307:         return new ClassType(get_called_class());
308:     }
309: 
310: 
311: 
312:     public function __call($name, $args)
313:     {
314:         return ObjectMixin::call($this, $name, $args);
315:     }
316: 
317: 
318: 
319:     public function &__get($name)
320:     {
321:         return ObjectMixin::get($this, $name);
322:     }
323: 
324: 
325: 
326:     public function __set($name, $value)
327:     {
328:         return ObjectMixin::set($this, $name, $value);
329:     }
330: 
331: 
332: 
333:     public function __isset($name)
334:     {
335:         return ObjectMixin::has($this, $name);
336:     }
337: 
338: 
339: 
340:     public function __unset($name)
341:     {
342:         ObjectMixin::remove($this, $name);
343:     }
344: 
345: }
346: 
Nette Framework 2.0.4 API API documentation generated by ApiGen 2.7.0