PhpParser.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Common\Annotations;
  20. use SplFileObject;
  21. /**
  22. * Parses a file for namespaces/use/class declarations.
  23. *
  24. * @author Fabien Potencier <fabien@symfony.com>
  25. * @author Christian Kaps <christian.kaps@mohiva.com>
  26. */
  27. final class PhpParser
  28. {
  29. /**
  30. * The token list.
  31. *
  32. * @var array
  33. */
  34. private $tokens;
  35. /**
  36. * The number of tokens.
  37. *
  38. * @var int
  39. */
  40. private $numTokens = 0;
  41. /**
  42. * The current array pointer.
  43. *
  44. * @var int
  45. */
  46. private $pointer = 0;
  47. /**
  48. * Parses a class.
  49. *
  50. * @param \ReflectionClass $class A <code>ReflectionClass</code> object.
  51. * @return array A list with use statements in the form (Alias => FQN).
  52. */
  53. public function parseClass(\ReflectionClass $class)
  54. {
  55. if (false === $filename = $class->getFilename()) {
  56. return array();
  57. }
  58. $content = $this->getFileContent($filename, $class->getStartLine());
  59. $namespace = str_replace('\\', '\\\\', $class->getNamespaceName());
  60. $content = preg_replace('/^.*?(\bnamespace\s+' . $namespace . '\s*[;{].*)$/s', '\\1', $content);
  61. $this->tokens = token_get_all('<?php ' . $content);
  62. $this->numTokens = count($this->tokens);
  63. $this->pointer = 0;
  64. $statements = $this->parseUseStatements($class->getNamespaceName());
  65. return $statements;
  66. }
  67. /**
  68. * Get the content of the file right up to the given line number.
  69. *
  70. * @param string $filename The name of the file to load.
  71. * @param int $lineNumber The number of lines to read from file.
  72. * @return string The content of the file.
  73. */
  74. private function getFileContent($filename, $lineNumber)
  75. {
  76. $content = '';
  77. $lineCnt = 0;
  78. $file = new SplFileObject($filename);
  79. while(!$file->eof()) {
  80. if ($lineCnt++ == $lineNumber) {
  81. break;
  82. }
  83. $content .= $file->fgets();
  84. }
  85. return $content;
  86. }
  87. /**
  88. * Gets the next non whitespace and non comment token.
  89. *
  90. * @return array The token if exists, null otherwise.
  91. */
  92. private function next()
  93. {
  94. for ($i = $this->pointer; $i < $this->numTokens; $i++) {
  95. $this->pointer++;
  96. if ($this->tokens[$i][0] === T_WHITESPACE ||
  97. $this->tokens[$i][0] === T_COMMENT ||
  98. $this->tokens[$i][0] === T_DOC_COMMENT) {
  99. continue;
  100. }
  101. return $this->tokens[$i];
  102. }
  103. return null;
  104. }
  105. /**
  106. * Get all use statements.
  107. *
  108. * @param string $namespaceName The namespace name of the reflected class.
  109. * @return array A list with all found use statements.
  110. */
  111. private function parseUseStatements($namespaceName)
  112. {
  113. $statements = array();
  114. while (($token = $this->next())) {
  115. if ($token[0] === T_USE) {
  116. $statements = array_merge($statements, $this->parseUseStatement());
  117. continue;
  118. } else if ($token[0] !== T_NAMESPACE || $this->parseNamespace() != $namespaceName) {
  119. continue;
  120. }
  121. // Get fresh array for new namespace. This is to prevent the parser to collect the use statements
  122. // for a previous namespace with the same name. This is the case if a namespace is defined twice
  123. // or if a namespace with the same name is commented out.
  124. $statements = array();
  125. }
  126. return $statements;
  127. }
  128. /**
  129. * Get the namespace name.
  130. *
  131. * @return string The found namespace name.
  132. */
  133. private function parseNamespace()
  134. {
  135. $namespace = '';
  136. while (($token = $this->next())){
  137. if ($token[0] === T_STRING || $token[0] === T_NS_SEPARATOR) {
  138. $namespace .= $token[1];
  139. } else {
  140. break;
  141. }
  142. }
  143. return $namespace;
  144. }
  145. /**
  146. * Parse a single use statement.
  147. *
  148. * @return array A list with all found class names for a use statement.
  149. */
  150. private function parseUseStatement()
  151. {
  152. $class = '';
  153. $alias = '';
  154. $statements = array();
  155. $explicitAlias = false;
  156. while (($token = $this->next())) {
  157. $isNameToken = $token[0] === T_STRING || $token[0] === T_NS_SEPARATOR;
  158. if (!$explicitAlias && $isNameToken) {
  159. $class .= $token[1];
  160. $alias = $token[1];
  161. } else if ($explicitAlias && $isNameToken) {
  162. $alias .= $token[1];
  163. } else if ($token[0] === T_AS) {
  164. $explicitAlias = true;
  165. $alias = '';
  166. } else if ($token === ',') {
  167. $statements[strtolower($alias)] = $class;
  168. $class = '';
  169. $alias = '';
  170. $explicitAlias = false;
  171. } else if ($token === ';') {
  172. $statements[strtolower($alias)] = $class;
  173. break;
  174. } else {
  175. break;
  176. }
  177. }
  178. return $statements;
  179. }
  180. }