StaticReflectionParser.php 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 name of the class.
  31. *
  32. * @var string
  33. */
  34. protected $className;
  35. /**
  36. * TRUE if the caller only wants class annotations.
  37. *
  38. * @var boolean.
  39. */
  40. protected $classAnnotationOptimize;
  41. /**
  42. * TRUE when the parser has ran.
  43. *
  44. * @var boolean
  45. */
  46. protected $parsed = false;
  47. /**
  48. * The namespace of the class
  49. *
  50. * @var string
  51. */
  52. protected $namespace = '';
  53. /**
  54. * The use statements of this class.
  55. *
  56. * @var array
  57. */
  58. protected $useStatements = array();
  59. /**
  60. * The docComment of the class.
  61. *
  62. * @var string
  63. */
  64. protected $docComment = array(
  65. 'class' => '',
  66. 'property' => array(),
  67. 'method' => array(),
  68. );
  69. /**
  70. * The name of the class this class extends, if any.
  71. *
  72. * @var string
  73. */
  74. protected $parentClassName = '';
  75. /**
  76. * The parent PSR-0 Parser.
  77. *
  78. * @var \Doctrine\Common\Annotations\StaticReflectionParser
  79. */
  80. protected $parentStaticReflectionParser;
  81. /**
  82. * Parses a class residing in a PSR-0 hierarchy.
  83. *
  84. * @param string $class
  85. * The full, namespaced class name.
  86. * @param ClassFinder $finder
  87. * A ClassFinder object which finds the class.
  88. * @param boolean $classAnnotationOptimize
  89. * Only retrieve the class docComment. Presumes there is only one
  90. * statement per line.
  91. */
  92. public function __construct($className, $finder, $classAnnotationOptimize = false)
  93. {
  94. $this->className = ltrim($className, '\\');
  95. if ($lastNsPos = strrpos($this->className, '\\')) {
  96. $this->namespace = substr($this->className, 0, $lastNsPos);
  97. }
  98. $this->finder = $finder;
  99. $this->classAnnotationOptimize = $classAnnotationOptimize;
  100. }
  101. protected function parse()
  102. {
  103. if ($this->parsed || !$fileName = $this->finder->findFile($this->className)) {
  104. return;
  105. }
  106. $this->parsed = true;
  107. $contents = file_get_contents($fileName);
  108. if ($this->classAnnotationOptimize) {
  109. if (preg_match("/(\A.*)^\s+(abstract|final)?\s+class\s+$className\s+{/sm", $contents, $matches)) {
  110. $contents = $matches[1];
  111. }
  112. }
  113. $tokenParser = new TokenParser($contents);
  114. $docComment = '';
  115. while ($token = $tokenParser->next(false)) {
  116. if (is_array($token)) {
  117. switch ($token[0]) {
  118. case T_USE:
  119. $this->useStatements = array_merge($this->useStatements, $tokenParser->parseUseStatement());
  120. break;
  121. case T_DOC_COMMENT:
  122. $docComment = $token[1];
  123. break;
  124. case T_CLASS:
  125. $this->docComment['class'] = $docComment;
  126. $docComment = '';
  127. break;
  128. case T_VAR:
  129. case T_PRIVATE:
  130. case T_PROTECTED:
  131. case T_PUBLIC:
  132. $token = $tokenParser->next();
  133. if ($token[0] === T_VARIABLE) {
  134. $propertyName = substr($token[1], 1);
  135. $this->docComment['property'][$propertyName] = $docComment;
  136. continue 2;
  137. }
  138. if ($token[0] !== T_FUNCTION) {
  139. // For example, it can be T_FINAL.
  140. continue 2;
  141. }
  142. // No break.
  143. case T_FUNCTION:
  144. // The next string after function is the name, but
  145. // there can be & before the function name so find the
  146. // string.
  147. while (($token = $tokenParser->next()) && $token[0] !== T_STRING);
  148. $methodName = $token[1];
  149. $this->docComment['method'][$methodName] = $docComment;
  150. $docComment = '';
  151. break;
  152. case T_EXTENDS:
  153. $this->parentClassName = $tokenParser->parseClass();
  154. $nsPos = strpos($this->parentClassName, '\\');
  155. $fullySpecified = false;
  156. if ($nsPos === 0) {
  157. $fullySpecified = true;
  158. } else {
  159. if ($nsPos) {
  160. $prefix = strtolower(substr($this->parentClassName, 0, $nsPos));
  161. $postfix = substr($this->parentClassName, $nsPos);
  162. } else {
  163. $prefix = strtolower($this->parentClassName);
  164. $postfix = '';
  165. }
  166. foreach ($this->useStatements as $alias => $use) {
  167. if ($alias == $prefix) {
  168. $this->parentClassName = '\\' . $use . $postfix;
  169. $fullySpecified = true;
  170. }
  171. }
  172. }
  173. if (!$fullySpecified) {
  174. $this->parentClassName = '\\' . $this->namespace . '\\' . $this->parentClassName;
  175. }
  176. break;
  177. }
  178. }
  179. }
  180. }
  181. protected function getParentStaticReflectionParser()
  182. {
  183. if (empty($this->parentStaticReflectionParser)) {
  184. $this->parentStaticReflectionParser = new static($this->parentClassName, $this->finder);
  185. }
  186. return $this->parentStaticReflectionParser;
  187. }
  188. public function getClassName()
  189. {
  190. return $this->className;
  191. }
  192. public function getNamespaceName()
  193. {
  194. return $this->namespace;
  195. }
  196. /**
  197. * Get the ReflectionClass equivalent for this file / class.
  198. */
  199. public function getReflectionClass()
  200. {
  201. return new StaticReflectionClass($this);
  202. }
  203. /**
  204. * Get the ReflectionMethod equivalent for the method of this file / class.
  205. */
  206. public function getReflectionMethod($methodName)
  207. {
  208. return new StaticReflectionMethod($this, $methodName);
  209. }
  210. /**
  211. * Get the ReflectionProperty equivalent for the method of this file / class.
  212. */
  213. public function getReflectionProperty($propertyName)
  214. {
  215. return new StaticReflectionProperty($this, $propertyName);
  216. }
  217. /**
  218. * Get the use statements from this file.
  219. */
  220. public function getUseStatements()
  221. {
  222. $this->parse();
  223. return $this->useStatements;
  224. }
  225. /**
  226. * Get docComment.
  227. *
  228. * @param string $type class, property or method.
  229. * @param string $name Name of the property or method, not needed for class.
  230. *
  231. * @return string the doc comment or empty string if none.
  232. */
  233. public function getDocComment($type = 'class', $name = '')
  234. {
  235. $this->parse();
  236. return $name ? $this->docComment[$type][$name] : $this->docComment[$type];
  237. }
  238. /**
  239. * Get the PSR-0 parser for the declaring class.
  240. *
  241. * @param string $type property or method.
  242. * @param string $name Name of the property or method.
  243. *
  244. * @return StaticReflectionParser A static reflection parser for the declaring class.
  245. */
  246. public function getStaticReflectionParserForDeclaringClass($type, $name)
  247. {
  248. $this->parse();
  249. if (isset($this->docComment[$type][$name])) {
  250. return $this;
  251. }
  252. if (!empty($this->parentClassName)) {
  253. return $this->getParentStaticReflectionParser()->getStaticReflectionParserForDeclaringClass($type, $name);
  254. }
  255. throw new ReflectionException('Invalid ' . $type . ' "' . $name . '"');
  256. }
  257. }