1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6: */
7:
8: namespace Nette\Reflection;
9:
10: use Nette;
11:
12:
13: /**
14: * Reports information about a class.
15: * @property-read Method $constructor
16: * @property-read Extension $extension
17: * @property-read ClassType[] $interfaces
18: * @property-read Method[] $methods
19: * @property-read ClassType $parentClass
20: * @property-read Property[] $properties
21: * @property-read IAnnotation[][] $annotations
22: * @property-read string $description
23: * @property-read string $name
24: * @property-read bool $internal
25: * @property-read bool $userDefined
26: * @property-read bool $instantiable
27: * @property-read string $fileName
28: * @property-read int $startLine
29: * @property-read int $endLine
30: * @property-read string $docComment
31: * @property-read mixed[] $constants
32: * @property-read string[] $interfaceNames
33: * @property-read bool $interface
34: * @property-read bool $abstract
35: * @property-read bool $final
36: * @property-read int $modifiers
37: * @property-read array $staticProperties
38: * @property-read array $defaultProperties
39: * @property-read bool $iterateable
40: * @property-read string $extensionName
41: * @property-read string $namespaceName
42: * @property-read string $shortName
43: */
44: class ClassType extends \ReflectionClass
45: {
46: use Nette\SmartObject;
47:
48: /**
49: * @param string|object
50: * @return static
51: */
52: public static function from($class)
53: {
54: return new static($class);
55: }
56:
57:
58: public function __toString()
59: {
60: return $this->getName();
61: }
62:
63:
64: /**
65: * @param string
66: * @return bool
67: */
68: public function is($type)
69: {
70: return is_a($this->getName(), $type, true);
71: }
72:
73:
74: /********************* Reflection layer ****************d*g**/
75:
76:
77: /**
78: * @return Method|null
79: */
80: public function getConstructor()
81: {
82: return ($ref = parent::getConstructor()) ? Method::from($this->getName(), $ref->getName()) : null;
83: }
84:
85:
86: /**
87: * @return Extension|null
88: */
89: public function getExtension()
90: {
91: return ($name = $this->getExtensionName()) ? new Extension($name) : null;
92: }
93:
94:
95: /**
96: * @return static[]
97: */
98: public function getInterfaces()
99: {
100: $res = [];
101: foreach (parent::getInterfaceNames() as $val) {
102: $res[$val] = new static($val);
103: }
104: return $res;
105: }
106:
107:
108: /**
109: * @return Method
110: */
111: public function getMethod($name)
112: {
113: return new Method($this->getName(), $name);
114: }
115:
116:
117: /**
118: * @return Method[]
119: */
120: public function getMethods($filter = -1)
121: {
122: foreach ($res = parent::getMethods($filter) as $key => $val) {
123: $res[$key] = new Method($this->getName(), $val->getName());
124: }
125: return $res;
126: }
127:
128:
129: /**
130: * @return static|null
131: */
132: public function getParentClass()
133: {
134: return ($ref = parent::getParentClass()) ? new static($ref->getName()) : null;
135: }
136:
137:
138: /**
139: * @return Property[]
140: */
141: public function getProperties($filter = -1)
142: {
143: foreach ($res = parent::getProperties($filter) as $key => $val) {
144: $res[$key] = new Property($this->getName(), $val->getName());
145: }
146: return $res;
147: }
148:
149:
150: /**
151: * @return Property
152: */
153: public function getProperty($name)
154: {
155: return new Property($this->getName(), $name);
156: }
157:
158:
159: /********************* Nette\Annotations support ****************d*g**/
160:
161:
162: /**
163: * Has class specified annotation?
164: * @param string
165: * @return bool
166: */
167: public function hasAnnotation($name)
168: {
169: $res = AnnotationsParser::getAll($this);
170: return !empty($res[$name]);
171: }
172:
173:
174: /**
175: * Returns an annotation value.
176: * @param string
177: * @return IAnnotation
178: */
179: public function getAnnotation($name)
180: {
181: $res = AnnotationsParser::getAll($this);
182: return isset($res[$name]) ? end($res[$name]) : null;
183: }
184:
185:
186: /**
187: * Returns all annotations.
188: * @return IAnnotation[][]
189: */
190: public function getAnnotations()
191: {
192: return AnnotationsParser::getAll($this);
193: }
194:
195:
196: /**
197: * Returns value of annotation 'description'.
198: * @return string
199: */
200: public function getDescription()
201: {
202: return $this->getAnnotation('description');
203: }
204: }
205: