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\Reflection;
22
use ReflectionException;
23
use Doctrine\Common\Annotations\TokenParser;
26
* Parses a file for namespaces/use/class declarations.
28
* @author Karoly Negyesi <karoly@negyesi.net>
30
class StaticReflectionParser implements ReflectionProviderInterface
33
* The fully qualified class name.
40
* The short class name.
44
protected $shortClassName;
47
* Whether the caller only wants class annotations.
51
protected $classAnnotationOptimize;
54
* Whether the parser has run.
58
protected $parsed = false;
61
* The namespace of the class.
65
protected $namespace = '';
68
* The use statements of the class.
72
protected $useStatements = array();
75
* The docComment of the class.
79
protected $docComment = array(
81
'property' => array(),
86
* The name of the class this class extends, if any.
90
protected $parentClassName = '';
93
* The parent PSR-0 Parser.
95
* @var \Doctrine\Common\Reflection\StaticReflectionParser
97
protected $parentStaticReflectionParser;
100
* Parses a class residing in a PSR-0 hierarchy.
102
* @param string $className The full, namespaced class name.
103
* @param ClassFinderInterface $finder A ClassFinder object which finds the class.
104
* @param boolean $classAnnotationOptimize Only retrieve the class docComment.
105
* Presumes there is only one statement per line.
107
public function __construct($className, $finder, $classAnnotationOptimize = false)
109
$this->className = ltrim($className, '\\');
110
$lastNsPos = strrpos($this->className, '\\');
112
if ($lastNsPos !== false) {
113
$this->namespace = substr($this->className, 0, $lastNsPos);
114
$this->shortClassName = substr($this->className, $lastNsPos + 1);
116
$this->shortClassName = $this->className;
119
$this->finder = $finder;
120
$this->classAnnotationOptimize = $classAnnotationOptimize;
126
protected function parse()
128
if ($this->parsed || !$fileName = $this->finder->findFile($this->className)) {
131
$this->parsed = true;
132
$contents = file_get_contents($fileName);
133
if ($this->classAnnotationOptimize) {
134
if (preg_match("/\A.*^\s*((abstract|final)\s+)?class\s+{$this->shortClassName}\s+/sm", $contents, $matches)) {
135
$contents = $matches[0];
138
$tokenParser = new TokenParser($contents);
140
while ($token = $tokenParser->next(false)) {
141
if (is_array($token)) {
144
$this->useStatements = array_merge($this->useStatements, $tokenParser->parseUseStatement());
147
$docComment = $token[1];
150
$this->docComment['class'] = $docComment;
157
$token = $tokenParser->next();
158
if ($token[0] === T_VARIABLE) {
159
$propertyName = substr($token[1], 1);
160
$this->docComment['property'][$propertyName] = $docComment;
163
if ($token[0] !== T_FUNCTION) {
164
// For example, it can be T_FINAL.
169
// The next string after function is the name, but
170
// there can be & before the function name so find the
172
while (($token = $tokenParser->next()) && $token[0] !== T_STRING);
173
$methodName = $token[1];
174
$this->docComment['method'][$methodName] = $docComment;
178
$this->parentClassName = $tokenParser->parseClass();
179
$nsPos = strpos($this->parentClassName, '\\');
180
$fullySpecified = false;
182
$fullySpecified = true;
185
$prefix = strtolower(substr($this->parentClassName, 0, $nsPos));
186
$postfix = substr($this->parentClassName, $nsPos);
188
$prefix = strtolower($this->parentClassName);
191
foreach ($this->useStatements as $alias => $use) {
192
if ($alias == $prefix) {
193
$this->parentClassName = '\\' . $use . $postfix;
194
$fullySpecified = true;
198
if (!$fullySpecified) {
199
$this->parentClassName = '\\' . $this->namespace . '\\' . $this->parentClassName;
208
* @return StaticReflectionParser
210
protected function getParentStaticReflectionParser()
212
if (empty($this->parentStaticReflectionParser)) {
213
$this->parentStaticReflectionParser = new static($this->parentClassName, $this->finder);
216
return $this->parentStaticReflectionParser;
222
public function getClassName()
224
return $this->className;
230
public function getNamespaceName()
232
return $this->namespace;
238
public function getReflectionClass()
240
return new StaticReflectionClass($this);
246
public function getReflectionMethod($methodName)
248
return new StaticReflectionMethod($this, $methodName);
254
public function getReflectionProperty($propertyName)
256
return new StaticReflectionProperty($this, $propertyName);
260
* Gets the use statements from this file.
264
public function getUseStatements()
268
return $this->useStatements;
272
* Gets the doc comment.
274
* @param string $type The type: 'class', 'property' or 'method'.
275
* @param string $name The name of the property or method, not needed for 'class'.
277
* @return string The doc comment, empty string if none.
279
public function getDocComment($type = 'class', $name = '')
283
return $name ? $this->docComment[$type][$name] : $this->docComment[$type];
287
* Gets the PSR-0 parser for the declaring class.
289
* @param string $type The type: 'property' or 'method'.
290
* @param string $name The name of the property or method.
292
* @return StaticReflectionParser A static reflection parser for the declaring class.
294
* @throws ReflectionException
296
public function getStaticReflectionParserForDeclaringClass($type, $name)
299
if (isset($this->docComment[$type][$name])) {
302
if (!empty($this->parentClassName)) {
303
return $this->getParentStaticReflectionParser()->getStaticReflectionParserForDeclaringClass($type, $name);
305
throw new ReflectionException('Invalid ' . $type . ' "' . $name . '"');