4
* This file is part of the Nette Framework (http://nette.org)
6
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
8
* For the full copyright and license information, please view
9
* the file license.txt that was distributed with this source code.
12
namespace Nette\Reflection;
19
* Reports information about a class.
22
* @property-read Method $constructor
23
* @property-read Extension $extension
24
* @property-read ClassType[] $interfaces
25
* @property-read Method[] $methods
26
* @property-read ClassType $parentClass
27
* @property-read Property[] $properties
28
* @property-read IAnnotation[][] $annotations
29
* @property-read string $description
30
* @property-read string $name
31
* @property-read bool $internal
32
* @property-read bool $userDefined
33
* @property-read bool $instantiable
34
* @property-read string $fileName
35
* @property-read int $startLine
36
* @property-read int $endLine
37
* @property-read string $docComment
38
* @property-read mixed[] $constants
39
* @property-read string[] $interfaceNames
40
* @property-read bool $interface
41
* @property-read bool $abstract
42
* @property-read bool $final
43
* @property-read int $modifiers
44
* @property-read array $staticProperties
45
* @property-read array $defaultProperties
46
* @property-read bool $iterateable
47
* @property-read string $extensionName
48
* @property-read string $namespaceName
49
* @property-read string $shortName
51
class ClassType extends \ReflectionClass
54
/** @var array (method => array(type => callable)) */
55
private static $extMethods;
59
* @param string|object
62
public static function from($class)
64
return new static($class);
68
public function __toString()
70
return 'Class ' . $this->getName();
77
public function hasEventProperty($name)
79
if (preg_match('#^on[A-Z]#', $name) && $this->hasProperty($name)) {
80
$rp = $this->getProperty($name);
81
return $rp->isPublic() && !$rp->isStatic();
88
* Adds a method to class.
89
* @param string method name
90
* @param mixed callable
93
public function setExtensionMethod($name, $callback)
95
$l = & self::$extMethods[strtolower($name)];
96
$l[strtolower($this->getName())] = new Nette\Callback($callback);
103
* Returns extension method.
104
* @param string method name
107
public function getExtensionMethod($name)
109
$class = strtolower($this->getName());
110
$l = & self::$extMethods[strtolower($name)];
115
} elseif (isset($l[''][$class])) { // cached value
116
return $l[''][$class];
121
if (isset($l[$cl])) {
122
return $l[''][$class] = $l[$cl];
124
} while (($cl = strtolower(get_parent_class($cl))) !== '');
126
foreach (class_implements($class) as $cl) {
127
$cl = strtolower($cl);
128
if (isset($l[$cl])) {
129
return $l[''][$class] = $l[$cl];
132
return $l[''][$class] = FALSE;
140
public function is($type)
142
return $this->isSubclassOf($type) || strcasecmp($this->getName(), ltrim($type, '\\')) === 0;
146
/********************* Reflection layer ****************d*g**/
150
* @return Method|NULL
152
public function getConstructor()
154
return ($ref = parent::getConstructor()) ? Method::from($this->getName(), $ref->getName()) : NULL;
159
* @return Extension|NULL
161
public function getExtension()
163
return ($name = $this->getExtensionName()) ? new Extension($name) : NULL;
168
* @return ClassType[]
170
public function getInterfaces()
173
foreach (parent::getInterfaceNames() as $val) {
174
$res[$val] = new static($val);
183
public function getMethod($name)
185
return new Method($this->getName(), $name);
192
public function getMethods($filter = -1)
194
foreach ($res = parent::getMethods($filter) as $key => $val) {
195
$res[$key] = new Method($this->getName(), $val->getName());
202
* @return ClassType|NULL
204
public function getParentClass()
206
return ($ref = parent::getParentClass()) ? new static($ref->getName()) : NULL;
213
public function getProperties($filter = -1)
215
foreach ($res = parent::getProperties($filter) as $key => $val) {
216
$res[$key] = new Property($this->getName(), $val->getName());
225
public function getProperty($name)
227
return new Property($this->getName(), $name);
231
/********************* Nette\Annotations support ****************d*g**/
235
* Has class specified annotation?
239
public function hasAnnotation($name)
241
$res = AnnotationsParser::getAll($this);
242
return !empty($res[$name]);
247
* Returns an annotation value.
249
* @return IAnnotation
251
public function getAnnotation($name)
253
$res = AnnotationsParser::getAll($this);
254
return isset($res[$name]) ? end($res[$name]) : NULL;
259
* Returns all annotations.
260
* @return IAnnotation[][]
262
public function getAnnotations()
264
return AnnotationsParser::getAll($this);
269
* Returns value of annotation 'description'.
272
public function getDescription()
274
return $this->getAnnotation('description');
278
/********************* Nette\Object behaviour ****************d*g**/
284
public static function getReflection()
286
return new ClassType(get_called_class());
290
public function __call($name, $args)
292
return ObjectMixin::call($this, $name, $args);
296
public function &__get($name)
298
return ObjectMixin::get($this, $name);
302
public function __set($name, $value)
304
return ObjectMixin::set($this, $name, $value);
308
public function __isset($name)
310
return ObjectMixin::has($this, $name);
314
public function __unset($name)
316
ObjectMixin::remove($this, $name);