7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
16 * @package Zend_Reflection
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
23 * @see Zend_Reflection_Class
25 require_once 'Zend/Reflection/Class.php';
28 * @see Zend_Reflection_Function
30 require_once 'Zend/Reflection/Function.php';
34 * @package Zend_Reflection
35 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
36 * @license http://framework.zend.com/license/new-bsd New BSD License
38 class Zend_Reflection_File
implements Reflector
43 protected $_filepath = null;
48 protected $_docComment = null;
53 protected $_startLine = 1;
58 protected $_endLine = null;
63 protected $_requiredFiles = array();
66 * @var Zend_Reflection_Class[]
68 protected $_classes = array();
71 * @var Zend_Reflection_Function[]
73 protected $_functions = array();
78 protected $_contents = null;
86 public function __construct($file)
90 if (($fileRealpath = realpath($fileName)) === false) {
91 $fileRealpath = self
::findRealpathInIncludePath($file);
94 if (!$fileRealpath ||
!in_array($fileRealpath, get_included_files())) {
95 require_once 'Zend/Reflection/Exception.php';
96 throw new Zend_Reflection_Exception('File ' . $file . ' must be required before it can be reflected');
99 $this->_fileName
= $fileRealpath;
100 $this->_contents
= file_get_contents($this->_fileName
);
105 * Find realpath of file based on include_path
107 * @param string $fileName
110 public static function findRealpathInIncludePath($fileName)
112 require_once 'Zend/Loader.php';
113 $includePaths = Zend_Loader
::explodeIncludePath();
114 while (count($includePaths) > 0) {
115 $filePath = array_shift($includePaths) . DIRECTORY_SEPARATOR
. $fileName;
117 if ( ($foundRealpath = realpath($filePath)) !== false) {
122 return $foundRealpath;
128 * Required by the Reflector interface.
130 * @todo What should this do?
133 public static function export()
139 * Return the file name of the reflected file
143 public function getFileName()
145 return $this->_fileName
;
149 * Get the start line - Always 1, staying consistent with the Reflection API
153 public function getStartLine()
155 return $this->_startLine
;
159 * Get the end line / number of lines
163 public function getEndLine()
165 return $this->_endLine
;
169 * Return the doc comment
173 public function getDocComment()
175 return $this->_docComment
;
179 * Return the docblock
181 * @param string $reflectionClass Reflection class to use
182 * @return Zend_Reflection_Docblock
184 public function getDocblock($reflectionClass = 'Zend_Reflection_Docblock')
186 $instance = new $reflectionClass($this);
187 if (!$instance instanceof Zend_Reflection_Docblock
) {
188 require_once 'Zend/Reflection/Exception.php';
189 throw new Zend_Reflection_Exception('Invalid reflection class specified; must extend Zend_Reflection_Docblock');
195 * Return the reflection classes of the classes found inside this file
197 * @param string $reflectionClass Name of reflection class to use for instances
198 * @return array Array of Zend_Reflection_Class instances
200 public function getClasses($reflectionClass = 'Zend_Reflection_Class')
203 foreach ($this->_classes
as $class) {
204 $instance = new $reflectionClass($class);
205 if (!$instance instanceof Zend_Reflection_Class
) {
206 require_once 'Zend/Reflection/Exception.php';
207 throw new Zend_Reflection_Exception('Invalid reflection class provided; must extend Zend_Reflection_Class');
209 $classes[] = $instance;
215 * Return the reflection functions of the functions found inside this file
217 * @param string $reflectionClass Name of reflection class to use for instances
218 * @return array Array of Zend_Reflection_Functions
220 public function getFunctions($reflectionClass = 'Zend_Reflection_Function')
222 $functions = array();
223 foreach ($this->_functions
as $function) {
224 $instance = new $reflectionClass($function);
225 if (!$instance instanceof Zend_Reflection_Function
) {
226 require_once 'Zend/Reflection/Exception.php';
227 throw new Zend_Reflection_Exception('Invalid reflection class provided; must extend Zend_Reflection_Function');
229 $functions[] = $instance;
235 * Retrieve the reflection class of a given class found in this file
237 * @param null|string $name
238 * @param string $reflectionClass Reflection class to use when creating reflection instance
239 * @return Zend_Reflection_Class
240 * @throws Zend_Reflection_Exception for invalid class name or invalid reflection class
242 public function getClass($name = null, $reflectionClass = 'Zend_Reflection_Class')
244 if ($name === null) {
245 reset($this->_classes
);
246 $selected = current($this->_classes
);
247 $instance = new $reflectionClass($selected);
248 if (!$instance instanceof Zend_Reflection_Class
) {
249 require_once 'Zend/Reflection/Exception.php';
250 throw new Zend_Reflection_Exception('Invalid reflection class given; must extend Zend_Reflection_Class');
255 if (in_array($name, $this->_classes
)) {
256 $instance = new $reflectionClass($name);
257 if (!$instance instanceof Zend_Reflection_Class
) {
258 require_once 'Zend/Reflection/Exception.php';
259 throw new Zend_Reflection_Exception('Invalid reflection class given; must extend Zend_Reflection_Class');
264 require_once 'Zend/Reflection/Exception.php';
265 throw new Zend_Reflection_Exception('Class by name ' . $name . ' not found.');
269 * Return the full contents of file
273 public function getContents()
275 return $this->_contents
;
279 * Serialize to string
281 * Required by the Reflector interface
283 * @todo What should this serialization look like?
286 public function __toString()
292 * This method does the work of "reflecting" the file
294 * Uses PHP's tokenizer to perform file reflection.
298 protected function _reflect()
300 $contents = $this->_contents
;
301 $tokens = token_get_all($contents);
303 $functionTrapped = false;
304 $classTrapped = false;
305 $requireTrapped = false;
308 $this->_checkFileDocBlock($tokens);
310 foreach ($tokens as $token) {
312 * Tokens are characters representing symbols or arrays
313 * representing strings. The keys/values in the arrays are
319 * Token ID's are explained here:
320 * http://www.php.net/manual/en/tokens.php.
323 if (is_array($token)) {
326 $lineNum = $token[2];
329 // Maintain the count of open braces
332 } else if ($token == '}') {
342 if ($functionTrapped) {
343 $this->_functions
[] = $value;
344 $functionTrapped = false;
345 } elseif ($classTrapped) {
346 $this->_classes
[] = $value;
347 $classTrapped = false;
351 // Required file names are T_CONSTANT_ENCAPSED_STRING
352 case T_CONSTANT_ENCAPSED_STRING
:
353 if ($requireTrapped) {
354 $this->_requiredFiles
[] = $value ."\n";
355 $requireTrapped = false;
361 if ($openBraces == 0) {
362 $functionTrapped = true;
369 $classTrapped = true;
372 // All types of requires
377 $requireTrapped = true;
380 // Default case: do nothing
386 $this->_endLine
= count(explode("\n", $this->_contents
));
390 * Validate / check a file level docblock
392 * @param array $tokens Array of tokenizer tokens
395 protected function _checkFileDocBlock($tokens) {
396 foreach ($tokens as $token) {
399 $lineNum = $token[2];
400 if(($type == T_OPEN_TAG
) ||
($type == T_WHITESPACE
)) {
402 } elseif ($type == T_DOC_COMMENT
) {
403 $this->_docComment
= $value;
404 $this->_startLine
= $lineNum +
substr_count($value, "\n") +
1;
407 // Only whitespace is allowed before file docblocks