3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Code\Reflection
;
13 use Zend\Code\Annotation\AnnotationCollection
;
14 use Zend\Code\Annotation\AnnotationManager
;
15 use Zend\Code\Reflection\FileReflection
;
16 use Zend\Code\Scanner\AnnotationScanner
;
17 use Zend\Code\Scanner\FileScanner
;
19 class ClassReflection
extends ReflectionClass
implements ReflectionInterface
22 * @var AnnotationScanner
24 protected $annotations = null;
27 * @var DocBlockReflection
29 protected $docBlock = null;
32 * Return the reflection file of the declaring file.
34 * @return FileReflection
36 public function getDeclaringFile()
38 $instance = new FileReflection($this->getFileName());
44 * Return the classes DocBlock reflection object
46 * @return DocBlockReflection
47 * @throws Exception\ExceptionInterface for missing DocBock or invalid reflection class
49 public function getDocBlock()
51 if (isset($this->docBlock
)) {
52 return $this->docBlock
;
55 if ('' == $this->getDocComment()) {
59 $this->docBlock
= new DocBlockReflection($this);
61 return $this->docBlock
;
65 * @param AnnotationManager $annotationManager
66 * @return AnnotationCollection
68 public function getAnnotations(AnnotationManager
$annotationManager)
70 $docComment = $this->getDocComment();
72 if ($docComment == '') {
76 if ($this->annotations
) {
77 return $this->annotations
;
80 $fileScanner = $this->createFileScanner($this->getFileName());
81 $nameInformation = $fileScanner->getClassNameInformation($this->getName());
83 if (!$nameInformation) {
87 $this->annotations
= new AnnotationScanner($annotationManager, $docComment, $nameInformation);
89 return $this->annotations
;
93 * Return the start line of the class
95 * @param bool $includeDocComment
98 public function getStartLine($includeDocComment = false)
100 if ($includeDocComment && $this->getDocComment() != '') {
101 return $this->getDocBlock()->getStartLine();
104 return parent
::getStartLine();
108 * Return the contents of the class
110 * @param bool $includeDocBlock
113 public function getContents($includeDocBlock = true)
115 $filename = $this->getFileName();
116 $filelines = file($filename);
117 $startnum = $this->getStartLine($includeDocBlock);
118 $endnum = $this->getEndLine() - $this->getStartLine();
120 // Ensure we get between the open and close braces
121 $lines = array_slice($filelines, $startnum, $endnum);
122 array_unshift($lines, $filelines[$startnum-1]);
124 return strstr(implode('', $lines), '{');
128 * Get all reflection objects of implemented interfaces
130 * @return ClassReflection[]
132 public function getInterfaces()
134 $phpReflections = parent
::getInterfaces();
135 $zendReflections = array();
136 while ($phpReflections && ($phpReflection = array_shift($phpReflections))) {
137 $instance = new ClassReflection($phpReflection->getName());
138 $zendReflections[] = $instance;
139 unset($phpReflection);
141 unset($phpReflections);
143 return $zendReflections;
147 * Return method reflection by name
149 * @param string $name
150 * @return MethodReflection
152 public function getMethod($name)
154 $method = new MethodReflection($this->getName(), parent
::getMethod($name)->getName());
160 * Get reflection objects of all methods
162 * @param string $filter
163 * @return MethodReflection[]
165 public function getMethods($filter = -1)
168 foreach (parent
::getMethods($filter) as $method) {
169 $instance = new MethodReflection($this->getName(), $method->getName());
170 $methods[] = $instance;
177 * Get parent reflection class of reflected class
179 * @return ClassReflection|bool
181 public function getParentClass()
183 $phpReflection = parent
::getParentClass();
184 if ($phpReflection) {
185 $zendReflection = new ClassReflection($phpReflection->getName());
186 unset($phpReflection);
188 return $zendReflection;
195 * Return reflection property of this class by name
197 * @param string $name
198 * @return PropertyReflection
200 public function getProperty($name)
202 $phpReflection = parent
::getProperty($name);
203 $zendReflection = new PropertyReflection($this->getName(), $phpReflection->getName());
204 unset($phpReflection);
206 return $zendReflection;
210 * Return reflection properties of this class
213 * @return PropertyReflection[]
215 public function getProperties($filter = -1)
217 $phpReflections = parent
::getProperties($filter);
218 $zendReflections = array();
219 while ($phpReflections && ($phpReflection = array_shift($phpReflections))) {
220 $instance = new PropertyReflection($this->getName(), $phpReflection->getName());
221 $zendReflections[] = $instance;
222 unset($phpReflection);
224 unset($phpReflections);
226 return $zendReflections;
229 public function toString()
231 return parent
::__toString();
234 public function __toString()
236 return parent
::__toString();
240 * Creates a new FileScanner instance.
242 * By having this as a seperate method it allows the method to be overridden
243 * if a different FileScanner is needed.
245 * @param string $filename
247 * @return FileScanner
249 protected function createFileScanner($filename)
251 return new FileScanner($filename);