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()
104 $statements = array();
105 $explicitAlias = false;
106 while (($token = $this->next())) {
107 $isNameToken = $token[0] === T_STRING || $token[0] === T_NS_SEPARATOR;
108 if (!$explicitAlias && $isNameToken) {
111 } else if ($explicitAlias && $isNameToken) {
113 } else if ($token[0] === T_AS) {
114 $explicitAlias = true;
116 } else if ($token === ',') {
117 $statements[strtolower($alias)] = $class;
120 $explicitAlias = false;
121 } else if ($token === ';') {
122 $statements[strtolower($alias)] = $class;
133 * Gets all use statements.
135 * @param string $namespaceName The namespace name of the reflected class.
137 * @return array A list with all found use statements.
139 public function parseUseStatements($namespaceName)
141 $statements = array();
142 while (($token = $this->next())) {
143 if ($token[0] === T_USE) {
144 $statements = array_merge($statements, $this->parseUseStatement());
147 if ($token[0] !== T_NAMESPACE || $this->parseNamespace() != $namespaceName) {
151 // Get fresh array for new namespace. This is to prevent the parser to collect the use statements
152 // for a previous namespace with the same name. This is the case if a namespace is defined twice
153 // or if a namespace with the same name is commented out.
154 $statements = array();
161 * Gets the namespace.
163 * @return string The found namespace.
165 public function parseNamespace()
168 while (($token = $this->next()) && ($token[0] === T_STRING || $token[0] === T_NS_SEPARATOR)) {
176 * Gets the class name.
178 * @return string The found class name.
180 public function parseClass()
182 // Namespaces and class names are tokenized the same: T_STRINGs
183 // separated by T_NS_SEPARATOR so we can use one function to provide
185 return $this->parseNamespace();