|
0
|
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 |
|
|
|
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 |
*/ |
|
|
27 |
final class PhpParser |
|
|
28 |
{ |
|
|
29 |
private $tokens; |
|
|
30 |
|
|
|
31 |
/** |
|
|
32 |
* Parses a class. |
|
|
33 |
* |
|
|
34 |
* @param \ReflectionClass $class |
|
|
35 |
*/ |
|
|
36 |
public function parseClass(\ReflectionClass $class) |
|
|
37 |
{ |
|
|
38 |
if (false === $filename = $class->getFilename()) { |
|
|
39 |
return array(); |
|
|
40 |
} |
|
|
41 |
$src = file_get_contents($filename); |
|
|
42 |
$name = $class->getName(); |
|
|
43 |
|
|
|
44 |
// This is a short-cut for code that follows some conventions: |
|
|
45 |
// - namespaced |
|
|
46 |
// - one class per file |
|
|
47 |
if (preg_match_all('#\bnamespace\s+'.str_replace('\\', '\\\\', $class->getNamespaceName()).'\s*;.*?\b(?:class|interface)\s+'.$class->getShortName().'\b#s', $src, $matches)) { |
|
|
48 |
foreach ($matches[0] as $match) { |
|
|
49 |
$classes = $this->parse('<?php '.$match, $name); |
|
|
50 |
|
|
|
51 |
if (isset($classes[$name])) { |
|
|
52 |
return $classes[$name]; |
|
|
53 |
} |
|
|
54 |
} |
|
|
55 |
} |
|
|
56 |
|
|
|
57 |
$classes = $this->parse($src, $name); |
|
|
58 |
|
|
|
59 |
return $classes[$name]; |
|
|
60 |
} |
|
|
61 |
|
|
|
62 |
private function parse($src, $interestedClass = null) |
|
|
63 |
{ |
|
|
64 |
$this->tokens = token_get_all($src); |
|
|
65 |
$classes = $uses = array(); |
|
|
66 |
$namespace = ''; |
|
|
67 |
while ($token = $this->next()) { |
|
|
68 |
if (T_NAMESPACE === $token[0]) { |
|
|
69 |
$namespace = $this->parseNamespace(); |
|
|
70 |
$uses = array(); |
|
|
71 |
} elseif (T_CLASS === $token[0] || T_INTERFACE === $token[0]) { |
|
|
72 |
if ('' !== $namespace) { |
|
|
73 |
$class = $namespace.'\\'.$this->nextValue(); |
|
|
74 |
} else { |
|
|
75 |
$class = $this->nextValue(); |
|
|
76 |
} |
|
|
77 |
$classes[$class] = $uses; |
|
|
78 |
|
|
|
79 |
if (null !== $interestedClass && $interestedClass === $class) { |
|
|
80 |
return $classes; |
|
|
81 |
} |
|
|
82 |
} elseif (T_USE === $token[0]) { |
|
|
83 |
foreach ($this->parseUseStatement() as $useStatement) { |
|
|
84 |
list($alias, $class) = $useStatement; |
|
|
85 |
$uses[strtolower($alias)] = $class; |
|
|
86 |
} |
|
|
87 |
} |
|
|
88 |
} |
|
|
89 |
|
|
|
90 |
return $classes; |
|
|
91 |
} |
|
|
92 |
|
|
|
93 |
private function parseNamespace() |
|
|
94 |
{ |
|
|
95 |
$namespace = ''; |
|
|
96 |
while ($token = $this->next()) { |
|
|
97 |
if (T_NS_SEPARATOR === $token[0] || T_STRING === $token[0]) { |
|
|
98 |
$namespace .= $token[1]; |
|
|
99 |
} elseif (is_string($token) && in_array($token, array(';', '{'))) { |
|
|
100 |
return $namespace; |
|
|
101 |
} |
|
|
102 |
} |
|
|
103 |
} |
|
|
104 |
|
|
|
105 |
private function parseUseStatement() |
|
|
106 |
{ |
|
|
107 |
$statements = $class = array(); |
|
|
108 |
$alias = ''; |
|
|
109 |
while ($token = $this->next()) { |
|
|
110 |
if (T_NS_SEPARATOR === $token[0] || T_STRING === $token[0]) { |
|
|
111 |
$class[] = $token[1]; |
|
|
112 |
} else if (T_AS === $token[0]) { |
|
|
113 |
$alias = $this->nextValue(); |
|
|
114 |
} else if (is_string($token)) { |
|
|
115 |
if (',' === $token || ';' === $token) { |
|
|
116 |
$statements[] = array( |
|
|
117 |
$alias ? $alias : $class[count($class) - 1], |
|
|
118 |
implode('', $class) |
|
|
119 |
); |
|
|
120 |
} |
|
|
121 |
|
|
|
122 |
if (';' === $token) { |
|
|
123 |
return $statements; |
|
|
124 |
} |
|
|
125 |
if (',' === $token) { |
|
|
126 |
$class = array(); |
|
|
127 |
$alias = ''; |
|
|
128 |
|
|
|
129 |
continue; |
|
|
130 |
} |
|
|
131 |
} |
|
|
132 |
} |
|
|
133 |
} |
|
|
134 |
|
|
|
135 |
private function next() |
|
|
136 |
{ |
|
|
137 |
while ($token = array_shift($this->tokens)) { |
|
|
138 |
if (in_array($token[0], array(T_WHITESPACE, T_COMMENT, T_DOC_COMMENT))) { |
|
|
139 |
continue; |
|
|
140 |
} |
|
|
141 |
|
|
|
142 |
return $token; |
|
|
143 |
} |
|
|
144 |
} |
|
|
145 |
|
|
|
146 |
private function nextValue() |
|
|
147 |
{ |
|
|
148 |
$token = $this->next(); |
|
|
149 |
|
|
|
150 |
return is_array($token) ? $token[1] : $token; |
|
|
151 |
} |
|
|
152 |
} |