[ZF-10089] Zend_Log
[zend/radio.git] / library / Zend / Reflection / File.php
blob26ae5b5af3595822b2e6afa8951d4175909ad652
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
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
19 * @version $Id$
22 /**
23 * @see Zend_Reflection_Class
25 require_once 'Zend/Reflection/Class.php';
27 /**
28 * @see Zend_Reflection_Function
30 require_once 'Zend/Reflection/Function.php';
32 /**
33 * @category Zend
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
40 /**
41 * @var string
43 protected $_filepath = null;
45 /**
46 * @var string
48 protected $_docComment = null;
50 /**
51 * @var int
53 protected $_startLine = 1;
55 /**
56 * @var int
58 protected $_endLine = null;
60 /**
61 * @var string[]
63 protected $_requiredFiles = array();
65 /**
66 * @var Zend_Reflection_Class[]
68 protected $_classes = array();
70 /**
71 * @var Zend_Reflection_Function[]
73 protected $_functions = array();
75 /**
76 * @var string
78 protected $_contents = null;
80 /**
81 * Constructor
83 * @param string $file
84 * @return void
86 public function __construct($file)
88 $fileName = $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);
101 $this->_reflect();
105 * Find realpath of file based on include_path
107 * @param string $fileName
108 * @return string
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) {
118 break;
122 return $foundRealpath;
126 * Export
128 * Required by the Reflector interface.
130 * @todo What should this do?
131 * @return null
133 public static function export()
135 return null;
139 * Return the file name of the reflected file
141 * @return string
143 public function getFileName()
145 return $this->_fileName;
149 * Get the start line - Always 1, staying consistent with the Reflection API
151 * @return int
153 public function getStartLine()
155 return $this->_startLine;
159 * Get the end line / number of lines
161 * @return int
163 public function getEndLine()
165 return $this->_endLine;
169 * Return the doc comment
171 * @return string
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');
191 return $instance;
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')
202 $classes = array();
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;
211 return $classes;
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;
231 return $functions;
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');
252 return $instance;
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');
261 return $instance;
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
271 * @return string
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?
284 * @return string
286 public function __toString()
288 return '';
292 * This method does the work of "reflecting" the file
294 * Uses PHP's tokenizer to perform file reflection.
296 * @return void
298 protected function _reflect()
300 $contents = $this->_contents;
301 $tokens = token_get_all($contents);
303 $functionTrapped = false;
304 $classTrapped = false;
305 $requireTrapped = false;
306 $openBraces = 0;
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
315 * - 0 => token id,
316 * - 1 => string,
317 * - 2 => line number
319 * Token ID's are explained here:
320 * http://www.php.net/manual/en/tokens.php.
323 if (is_array($token)) {
324 $type = $token[0];
325 $value = $token[1];
326 $lineNum = $token[2];
327 } else {
328 // It's a symbol
329 // Maintain the count of open braces
330 if ($token == '{') {
331 $openBraces++;
332 } else if ($token == '}') {
333 $openBraces--;
336 continue;
339 switch ($type) {
340 // Name of something
341 case T_STRING:
342 if ($functionTrapped) {
343 $this->_functions[] = $value;
344 $functionTrapped = false;
345 } elseif ($classTrapped) {
346 $this->_classes[] = $value;
347 $classTrapped = false;
349 continue;
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;
357 continue;
359 // Functions
360 case T_FUNCTION:
361 if ($openBraces == 0) {
362 $functionTrapped = true;
364 break;
366 // Classes
367 case T_CLASS:
368 case T_INTERFACE:
369 $classTrapped = true;
370 break;
372 // All types of requires
373 case T_REQUIRE:
374 case T_REQUIRE_ONCE:
375 case T_INCLUDE:
376 case T_INCLUDE_ONCE:
377 $requireTrapped = true;
378 break;
380 // Default case: do nothing
381 default:
382 break;
386 $this->_endLine = count(explode("\n", $this->_contents));
390 * Validate / check a file level docblock
392 * @param array $tokens Array of tokenizer tokens
393 * @return void
395 protected function _checkFileDocBlock($tokens) {
396 foreach ($tokens as $token) {
397 $type = $token[0];
398 $value = $token[1];
399 $lineNum = $token[2];
400 if(($type == T_OPEN_TAG) || ($type == T_WHITESPACE)) {
401 continue;
402 } elseif ($type == T_DOC_COMMENT) {
403 $this->_docComment = $value;
404 $this->_startLine = $lineNum + substr_count($value, "\n") + 1;
405 return;
406 } else {
407 // Only whitespace is allowed before file docblocks
408 return;