Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / common / lib / Doctrine / Common / Annotations / TokenParser.php
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
20 namespace Doctrine\Common\Annotations;
21
22 /**
23  * Parses a file for namespaces/use/class declarations.
24  *
25  * @author Fabien Potencier <fabien@symfony.com>
26  * @author Christian Kaps <christian.kaps@mohiva.com>
27  */
28 class TokenParser
29 {
30     /**
31      * The token list.
32      *
33      * @var array
34      */
35     private $tokens;
36
37     /**
38      * The number of tokens.
39      *
40      * @var int
41      */
42     private $numTokens = 0;
43
44     /**
45      * The current array pointer.
46      *
47      * @var int
48      */
49     private $pointer = 0;
50
51     public function __construct($contents)
52     {
53         $this->tokens = token_get_all($contents);
54         $this->numTokens = count($this->tokens);
55         $this->pointer = 0;
56     }
57
58     /**
59      * Gets the next non whitespace and non comment token.
60      *
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.
64      *
65      * @return array The token if exists, null otherwise.
66      */
67     public function next($docCommentIsComment = TRUE)
68     {
69         for ($i = $this->pointer; $i < $this->numTokens; $i++) {
70             $this->pointer++;
71             if ($this->tokens[$i][0] === T_WHITESPACE ||
72                 $this->tokens[$i][0] === T_COMMENT ||
73                 ($docCommentIsComment && $this->tokens[$i][0] === T_DOC_COMMENT)) {
74
75                 continue;
76             }
77
78             return $this->tokens[$i];
79         }
80
81         return null;
82     }
83
84     /**
85      * Parse a single use statement.
86      *
87      * @return array A list with all found class names for a use statement.
88      */
89     public function parseUseStatement()
90     {
91         $class = '';
92         $alias = '';
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) {
98                 $class .= $token[1];
99                 $alias = $token[1];
100             } else if ($explicitAlias && $isNameToken) {
101                 $alias .= $token[1];
102             } else if ($token[0] === T_AS) {
103                 $explicitAlias = true;
104                 $alias = '';
105             } else if ($token === ',') {
106                 $statements[strtolower($alias)] = $class;
107                 $class = '';
108                 $alias = '';
109                 $explicitAlias = false;
110             } else if ($token === ';') {
111                 $statements[strtolower($alias)] = $class;
112                 break;
113             } else {
114                 break;
115             }
116         }
117
118         return $statements;
119     }
120
121     /**
122      * Get all use statements.
123      *
124      * @param string $namespaceName The namespace name of the reflected class.
125      * @return array A list with all found use statements.
126      */
127     public function parseUseStatements($namespaceName)
128     {
129         $statements = array();
130         while (($token = $this->next())) {
131             if ($token[0] === T_USE) {
132                 $statements = array_merge($statements, $this->parseUseStatement());
133                 continue;
134             }
135             if ($token[0] !== T_NAMESPACE || $this->parseNamespace() != $namespaceName) {
136                 continue;
137             }
138
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();
143         }
144
145         return $statements;
146     }
147
148     /**
149      * Get the namespace.
150      *
151      * @return string The found namespace.
152      */
153     public function parseNamespace()
154     {
155         $name = '';
156         while (($token = $this->next()) && ($token[0] === T_STRING || $token[0] === T_NS_SEPARATOR)) {
157             $name .= $token[1];
158         }
159
160         return $name;
161     }
162
163     /**
164      * Get the class name.
165      *
166      * @return string The foundclass name.
167      */
168     public function parseClass()
169     {
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
172         // both.
173         return $this->parseNamespace();
174     }
175 }