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.
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>.
20 namespace Doctrine\Common\Annotations;
23 * Parses a file for namespaces/use/class declarations.
25 * @author Fabien Potencier <fabien@symfony.com>
26 * @author Christian Kaps <christian.kaps@mohiva.com>
38 * The number of tokens.
42 private $numTokens = 0;
45 * The current array pointer.
51 public function __construct($contents)
53 $this->tokens = token_get_all($contents);
54 $this->numTokens = count($this->tokens);
59 * Gets the next non whitespace and non comment token.
61 * @param $docCommentIsComment
62 * If TRUE then a doc comment is considered a comment and skipped.
63 * If FALSE then only whitespace and normal comments are skipped.
65 * @return array The token if exists, null otherwise.
67 public function next($docCommentIsComment = TRUE)
69 for ($i = $this->pointer; $i < $this->numTokens; $i++) {
71 if ($this->tokens[$i][0] === T_WHITESPACE ||
72 $this->tokens[$i][0] === T_COMMENT ||
73 ($docCommentIsComment && $this->tokens[$i][0] === T_DOC_COMMENT)) {
78 return $this->tokens[$i];
85 * Parse a single use statement.
87 * @return array A list with all found class names for a use statement.
89 public function parseUseStatement()
93 $statements = array();
94 $explicitAlias = false;
95 while (($token = $this->next())) {
96 $isNameToken = $token[0] === T_STRING || $token[0] === T_NS_SEPARATOR;
97 if (!$explicitAlias && $isNameToken) {
100 } else if ($explicitAlias && $isNameToken) {
102 } else if ($token[0] === T_AS) {
103 $explicitAlias = true;
105 } else if ($token === ',') {
106 $statements[strtolower($alias)] = $class;
109 $explicitAlias = false;
110 } else if ($token === ';') {
111 $statements[strtolower($alias)] = $class;
122 * Get all use statements.
124 * @param string $namespaceName The namespace name of the reflected class.
125 * @return array A list with all found use statements.
127 public function parseUseStatements($namespaceName)
129 $statements = array();
130 while (($token = $this->next())) {
131 if ($token[0] === T_USE) {
132 $statements = array_merge($statements, $this->parseUseStatement());
135 if ($token[0] !== T_NAMESPACE || $this->parseNamespace() != $namespaceName) {
139 // Get fresh array for new namespace. This is to prevent the parser to collect the use statements
140 // for a previous namespace with the same name. This is the case if a namespace is defined twice
141 // or if a namespace with the same name is commented out.
142 $statements = array();
151 * @return string The found namespace.
153 public function parseNamespace()
156 while (($token = $this->next()) && ($token[0] === T_STRING || $token[0] === T_NS_SEPARATOR)) {
164 * Get the class name.
166 * @return string The foundclass name.
168 public function parseClass()
170 // Namespaces and class names are tokenized the same: T_STRINGs
171 // separated by T_NS_SEPARATOR so we can use one function to provide
173 return $this->parseNamespace();