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.
45 * The current array pointer.
52 * @param string $contents
54 public function __construct($contents)
56 $this->tokens = token_get_all($contents);
58 // The PHP parser sets internal compiler globals for certain things. Annoyingly, the last docblock comment it
59 // saw gets stored in doc_comment. When it comes to compile the next thing to be include()d this stored
60 // doc_comment becomes owned by the first thing the compiler sees in the file that it considers might have a
61 // docblock. If the first thing in the file is a class without a doc block this would cause calls to
62 // getDocBlock() on said class to return our long lost doc_comment. Argh.
63 // To workaround, cause the parser to parse an empty docblock. Sure getDocBlock() will return this, but at least
64 // it's harmless to us.
65 token_get_all("<?php\n/**\n *\n */");
67 $this->numTokens = count($this->tokens);
71 * Gets the next non whitespace and non comment token.
73 * @param boolean $docCommentIsComment If TRUE then a doc comment is considered a comment and skipped.
74 * If FALSE then only whitespace and normal comments are skipped.
76 * @return array|null The token if exists, null otherwise.
78 public function next($docCommentIsComment = TRUE)
80 for ($i = $this->pointer; $i < $this->numTokens; $i++) {
82 if ($this->tokens[$i][0] === T_WHITESPACE ||
83 $this->tokens[$i][0] === T_COMMENT ||
84 ($docCommentIsComment && $this->tokens[$i][0] === T_DOC_COMMENT)) {
89 return $this->tokens[$i];
96 * Parses a single use statement.
98 * @return array A list with all found class names for a use statement.
100 public function parseUseStatement()
106 $statements = array();
107 $explicitAlias = false;
108 while (($token = $this->next())) {
109 $isNameToken = $token[0] === T_STRING || $token[0] === T_NS_SEPARATOR;
110 if (!$explicitAlias && $isNameToken) {
113 } else if ($explicitAlias && $isNameToken) {
115 } else if ($token[0] === T_AS) {
116 $explicitAlias = true;
118 } else if ($token === ',') {
119 $statements[strtolower($alias)] = $groupRoot . $class;
122 $explicitAlias = false;
123 } else if ($token === ';') {
124 $statements[strtolower($alias)] = $groupRoot . $class;
126 } else if ($token === '{' ) {
129 } else if ($token === '}' ) {
140 * Gets all use statements.
142 * @param string $namespaceName The namespace name of the reflected class.
144 * @return array A list with all found use statements.
146 public function parseUseStatements($namespaceName)
148 $statements = array();
149 while (($token = $this->next())) {
150 if ($token[0] === T_USE) {
151 $statements = array_merge($statements, $this->parseUseStatement());
154 if ($token[0] !== T_NAMESPACE || $this->parseNamespace() != $namespaceName) {
158 // Get fresh array for new namespace. This is to prevent the parser to collect the use statements
159 // for a previous namespace with the same name. This is the case if a namespace is defined twice
160 // or if a namespace with the same name is commented out.
161 $statements = array();
168 * Gets the namespace.
170 * @return string The found namespace.
172 public function parseNamespace()
175 while (($token = $this->next()) && ($token[0] === T_STRING || $token[0] === T_NS_SEPARATOR)) {
183 * Gets the class name.
185 * @return string The found class name.
187 public function parseClass()
189 // Namespaces and class names are tokenized the same: T_STRINGs
190 // separated by T_NS_SEPARATOR so we can use one function to provide
192 return $this->parseNamespace();