/libraries/Zend/Code/Reflection/FileReflection.php

https://github.com/kiranatama/sagalaya · PHP · 314 lines · 141 code · 36 blank · 137 comment · 13 complexity · e2bd0a8c22945d8a748dea42e2f950ec MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Code
  9. */
  10. namespace Zend\Code\Reflection;
  11. use Zend\Code\NameInformation;
  12. use Zend\Code\Scanner\CachingFileScanner;
  13. /**
  14. * @category Zend
  15. * @package Zend_Reflection
  16. */
  17. class FileReflection implements ReflectionInterface
  18. {
  19. /**
  20. * @var string
  21. */
  22. protected $filePath = null;
  23. /**
  24. * @var string
  25. */
  26. protected $docComment = null;
  27. /**
  28. * @var int
  29. */
  30. protected $startLine = 1;
  31. /**
  32. * @var int
  33. */
  34. protected $endLine = null;
  35. /**
  36. * @var string
  37. */
  38. protected $namespaces = array();
  39. /**
  40. * @var string[]
  41. */
  42. protected $uses = array();
  43. /**
  44. * @var string[]
  45. */
  46. protected $requiredFiles = array();
  47. /**
  48. * @var ReflectionClass[]
  49. */
  50. protected $classes = array();
  51. /**
  52. * @var FunctionReflection[]
  53. */
  54. protected $functions = array();
  55. /**
  56. * @var string
  57. */
  58. protected $contents = null;
  59. /**
  60. * Constructor
  61. *
  62. * @param string $filename
  63. * @throws Exception\RuntimeException
  64. * @return FileReflection
  65. */
  66. public function __construct($filename)
  67. {
  68. if (($fileRealPath = realpath($filename)) === false) {
  69. $fileRealPath = stream_resolve_include_path($filename);
  70. }
  71. if (!$fileRealPath || !in_array($fileRealPath, get_included_files())) {
  72. throw new Exception\RuntimeException('File ' . $filename . ' must be required before it can be reflected');
  73. }
  74. $this->filePath = $fileRealPath;
  75. $this->reflect();
  76. }
  77. /**
  78. * Export
  79. *
  80. * Required by the Reflector interface.
  81. *
  82. * @todo What should this do?
  83. * @return null
  84. */
  85. public static function export()
  86. {
  87. return null;
  88. }
  89. /**
  90. * Return the file name of the reflected file
  91. *
  92. * @return string
  93. */
  94. public function getFileName()
  95. {
  96. // @todo get file name from path
  97. return $this->filePath;
  98. }
  99. /**
  100. * Get the start line - Always 1, staying consistent with the Reflection API
  101. *
  102. * @return int
  103. */
  104. public function getStartLine()
  105. {
  106. return $this->startLine;
  107. }
  108. /**
  109. * Get the end line / number of lines
  110. *
  111. * @return int
  112. */
  113. public function getEndLine()
  114. {
  115. return $this->endLine;
  116. }
  117. /**
  118. * Return the doc comment
  119. *
  120. * @return string
  121. */
  122. public function getDocComment()
  123. {
  124. return $this->docComment;
  125. }
  126. /**
  127. * Return the DocBlock
  128. *
  129. * @return DocBlockReflection
  130. */
  131. public function getDocBlock()
  132. {
  133. if (!($docComment = $this->getDocComment())) {
  134. return false;
  135. }
  136. $instance = new DocBlockReflection($docComment);
  137. return $instance;
  138. }
  139. public function getNamespaces()
  140. {
  141. return $this->namespaces;
  142. }
  143. /**
  144. * getNamespace()
  145. *
  146. * @return string
  147. */
  148. public function getNamespace()
  149. {
  150. if (count($this->namespaces) > 0) {
  151. return $this->namespaces[0];
  152. }
  153. return null;
  154. }
  155. /**
  156. * getUses()
  157. *
  158. * @return string[]
  159. */
  160. public function getUses()
  161. {
  162. return $this->uses;
  163. }
  164. /**
  165. * Return the reflection classes of the classes found inside this file
  166. *
  167. * @return array Array of \Zend\Code\Reflection\ReflectionClass instances
  168. */
  169. public function getClasses()
  170. {
  171. $classes = array();
  172. foreach ($this->classes as $class) {
  173. $instance = new ClassReflection($class);
  174. $classes[] = $instance;
  175. }
  176. return $classes;
  177. }
  178. /**
  179. * Return the reflection functions of the functions found inside this file
  180. *
  181. * @return array Array of Zend_Reflection_Functions
  182. */
  183. public function getFunctions()
  184. {
  185. $functions = array();
  186. foreach ($this->functions as $function) {
  187. $instance = new FunctionReflection($function);
  188. $functions[] = $instance;
  189. }
  190. return $functions;
  191. }
  192. /**
  193. * Retrieve the reflection class of a given class found in this file
  194. *
  195. * @param null|string $name
  196. * @return ClassReflection
  197. * @throws Exception\InvalidArgumentException for invalid class name or invalid reflection class
  198. */
  199. public function getClass($name = null)
  200. {
  201. if ($name === null) {
  202. reset($this->classes);
  203. $selected = current($this->classes);
  204. $instance = new ClassReflection($selected);
  205. return $instance;
  206. }
  207. if (in_array($name, $this->classes)) {
  208. $instance = new ClassReflection($name);
  209. return $instance;
  210. }
  211. throw new Exception\InvalidArgumentException('Class by name ' . $name . ' not found.');
  212. }
  213. /**
  214. * Return the full contents of file
  215. *
  216. * @return string
  217. */
  218. public function getContents()
  219. {
  220. return file_get_contents($this->filePath);
  221. }
  222. public function toString()
  223. {
  224. return ''; // @todo
  225. }
  226. /**
  227. * Serialize to string
  228. *
  229. * Required by the Reflector interface
  230. *
  231. * @todo What should this serialization look like?
  232. * @return string
  233. */
  234. public function __toString()
  235. {
  236. return '';
  237. }
  238. /**
  239. * This method does the work of "reflecting" the file
  240. *
  241. * Uses Zend\Code\Scanner\FileScanner to gather file information
  242. *
  243. * @return void
  244. */
  245. protected function reflect()
  246. {
  247. $scanner = new CachingFileScanner($this->filePath);
  248. $this->docComment = $scanner->getDocComment();
  249. $this->requiredFiles = $scanner->getIncludes();
  250. $this->classes = $scanner->getClassNames();
  251. $this->namespaces = $scanner->getNamespaces();
  252. $this->uses = $scanner->getUses();
  253. }
  254. /**
  255. * Validate / check a file level DocBlock
  256. *
  257. * @param array $tokens Array of tokenizer tokens
  258. * @return void
  259. */
  260. protected function checkFileDocBlock($tokens)
  261. {
  262. foreach ($tokens as $token) {
  263. $type = $token[0];
  264. $value = $token[1];
  265. $lineNum = $token[2];
  266. if (($type == T_OPEN_TAG) || ($type == T_WHITESPACE)) {
  267. continue;
  268. } elseif ($type == T_DOC_COMMENT) {
  269. $this->docComment = $value;
  270. $this->startLine = $lineNum + substr_count($value, "\n") + 1;
  271. return;
  272. } else {
  273. // Only whitespace is allowed before file DocBlocks
  274. return;
  275. }
  276. }
  277. }
  278. }