StaticReflectionParser.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Common\Reflection;
  20. use ReflectionException;
  21. use Doctrine\Common\Annotations\TokenParser;
  22. /**
  23. * Parses a file for namespaces/use/class declarations.
  24. *
  25. * @author Karoly Negyesi <karoly@negyesi.net>
  26. */
  27. class StaticReflectionParser implements ReflectionProviderInterface
  28. {
  29. /**
  30. * The fully qualified class name.
  31. *
  32. * @var string
  33. */
  34. protected $className;
  35. /**
  36. * The short class name.
  37. *
  38. * @var string
  39. */
  40. protected $shortClassName;
  41. /**
  42. * Whether the caller only wants class annotations.
  43. *
  44. * @var boolean.
  45. */
  46. protected $classAnnotationOptimize;
  47. /**
  48. * Whether the parser has run.
  49. *
  50. * @var boolean
  51. */
  52. protected $parsed = false;
  53. /**
  54. * The namespace of the class.
  55. *
  56. * @var string
  57. */
  58. protected $namespace = '';
  59. /**
  60. * The use statements of the class.
  61. *
  62. * @var array
  63. */
  64. protected $useStatements = array();
  65. /**
  66. * The docComment of the class.
  67. *
  68. * @var string
  69. */
  70. protected $docComment = array(
  71. 'class' => '',
  72. 'property' => array(),
  73. 'method' => array()
  74. );
  75. /**
  76. * The name of the class this class extends, if any.
  77. *
  78. * @var string
  79. */
  80. protected $parentClassName = '';
  81. /**
  82. * The parent PSR-0 Parser.
  83. *
  84. * @var \Doctrine\Common\Reflection\StaticReflectionParser
  85. */
  86. protected $parentStaticReflectionParser;
  87. /**
  88. * Parses a class residing in a PSR-0 hierarchy.
  89. *
  90. * @param string $className The full, namespaced class name.
  91. * @param ClassFinderInterface $finder A ClassFinder object which finds the class.
  92. * @param boolean $classAnnotationOptimize Only retrieve the class docComment.
  93. * Presumes there is only one statement per line.
  94. */
  95. public function __construct($className, $finder, $classAnnotationOptimize = false)
  96. {
  97. $this->className = ltrim($className, '\\');
  98. $lastNsPos = strrpos($this->className, '\\');
  99. if ($lastNsPos !== false) {
  100. $this->namespace = substr($this->className, 0, $lastNsPos);
  101. $this->shortClassName = substr($this->className, $lastNsPos + 1);
  102. } else {
  103. $this->shortClassName = $this->className;
  104. }
  105. $this->finder = $finder;
  106. $this->classAnnotationOptimize = $classAnnotationOptimize;
  107. }
  108. /**
  109. * @return void
  110. */
  111. protected function parse()
  112. {
  113. if ($this->parsed || !$fileName = $this->finder->findFile($this->className)) {
  114. return;
  115. }
  116. $this->parsed = true;
  117. $contents = file_get_contents($fileName);
  118. if ($this->classAnnotationOptimize) {
  119. if (preg_match("/(\A.*)^\s+(abstract|final)?\s+class\s+{$this->shortClassName}\s+{/sm", $contents, $matches)) {
  120. $contents = $matches[1];
  121. }
  122. }
  123. $tokenParser = new TokenParser($contents);
  124. $docComment = '';
  125. while ($token = $tokenParser->next(false)) {
  126. if (is_array($token)) {
  127. switch ($token[0]) {
  128. case T_USE:
  129. $this->useStatements = array_merge($this->useStatements, $tokenParser->parseUseStatement());
  130. break;
  131. case T_DOC_COMMENT:
  132. $docComment = $token[1];
  133. break;
  134. case T_CLASS:
  135. $this->docComment['class'] = $docComment;
  136. $docComment = '';
  137. break;
  138. case T_VAR:
  139. case T_PRIVATE:
  140. case T_PROTECTED:
  141. case T_PUBLIC:
  142. $token = $tokenParser->next();
  143. if ($token[0] === T_VARIABLE) {
  144. $propertyName = substr($token[1], 1);
  145. $this->docComment['property'][$propertyName] = $docComment;
  146. continue 2;
  147. }
  148. if ($token[0] !== T_FUNCTION) {
  149. // For example, it can be T_FINAL.
  150. continue 2;
  151. }
  152. // No break.
  153. case T_FUNCTION:
  154. // The next string after function is the name, but
  155. // there can be & before the function name so find the
  156. // string.
  157. while (($token = $tokenParser->next()) && $token[0] !== T_STRING);
  158. $methodName = $token[1];
  159. $this->docComment['method'][$methodName] = $docComment;
  160. $docComment = '';
  161. break;
  162. case T_EXTENDS:
  163. $this->parentClassName = $tokenParser->parseClass();
  164. $nsPos = strpos($this->parentClassName, '\\');
  165. $fullySpecified = false;
  166. if ($nsPos === 0) {
  167. $fullySpecified = true;
  168. } else {
  169. if ($nsPos) {
  170. $prefix = strtolower(substr($this->parentClassName, 0, $nsPos));
  171. $postfix = substr($this->parentClassName, $nsPos);
  172. } else {
  173. $prefix = strtolower($this->parentClassName);
  174. $postfix = '';
  175. }
  176. foreach ($this->useStatements as $alias => $use) {
  177. if ($alias == $prefix) {
  178. $this->parentClassName = '\\' . $use . $postfix;
  179. $fullySpecified = true;
  180. }
  181. }
  182. }
  183. if (!$fullySpecified) {
  184. $this->parentClassName = '\\' . $this->namespace . '\\' . $this->parentClassName;
  185. }
  186. break;
  187. }
  188. }
  189. }
  190. }
  191. /**
  192. * @return StaticReflectionParser
  193. */
  194. protected function getParentStaticReflectionParser()
  195. {
  196. if (empty($this->parentStaticReflectionParser)) {
  197. $this->parentStaticReflectionParser = new static($this->parentClassName, $this->finder);
  198. }
  199. return $this->parentStaticReflectionParser;
  200. }
  201. /**
  202. * @return string
  203. */
  204. public function getClassName()
  205. {
  206. return $this->className;
  207. }
  208. /**
  209. * @return string
  210. */
  211. public function getNamespaceName()
  212. {
  213. return $this->namespace;
  214. }
  215. /**
  216. * {@inheritDoc}
  217. */
  218. public function getReflectionClass()
  219. {
  220. return new StaticReflectionClass($this);
  221. }
  222. /**
  223. * {@inheritDoc}
  224. */
  225. public function getReflectionMethod($methodName)
  226. {
  227. return new StaticReflectionMethod($this, $methodName);
  228. }
  229. /**
  230. * {@inheritDoc}
  231. */
  232. public function getReflectionProperty($propertyName)
  233. {
  234. return new StaticReflectionProperty($this, $propertyName);
  235. }
  236. /**
  237. * Gets the use statements from this file.
  238. *
  239. * @return array
  240. */
  241. public function getUseStatements()
  242. {
  243. $this->parse();
  244. return $this->useStatements;
  245. }
  246. /**
  247. * Gets the doc comment.
  248. *
  249. * @param string $type The type: 'class', 'property' or 'method'.
  250. * @param string $name The name of the property or method, not needed for 'class'.
  251. *
  252. * @return string The doc comment, empty string if none.
  253. */
  254. public function getDocComment($type = 'class', $name = '')
  255. {
  256. $this->parse();
  257. return $name ? $this->docComment[$type][$name] : $this->docComment[$type];
  258. }
  259. /**
  260. * Gets the PSR-0 parser for the declaring class.
  261. *
  262. * @param string $type The type: 'property' or 'method'.
  263. * @param string $name The name of the property or method.
  264. *
  265. * @return StaticReflectionParser A static reflection parser for the declaring class.
  266. *
  267. * @throws ReflectionException
  268. */
  269. public function getStaticReflectionParserForDeclaringClass($type, $name)
  270. {
  271. $this->parse();
  272. if (isset($this->docComment[$type][$name])) {
  273. return $this;
  274. }
  275. if (!empty($this->parentClassName)) {
  276. return $this->getParentStaticReflectionParser()->getStaticReflectionParserForDeclaringClass($type, $name);
  277. }
  278. throw new ReflectionException('Invalid ' . $type . ' "' . $name . '"');
  279. }
  280. }