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
34 * The name of the class.
41 * TRUE if the caller only wants class annotations.
45 protected $classAnnotationOptimize;
48 * TRUE when the parser has ran.
52 protected $parsed = false;
55 * The namespace of the class
59 protected $namespace = '';
62 * The use statements of this class.
66 protected $useStatements = array();
69 * The docComment of the class.
73 protected $docComment = array(
75 'property' => array(),
80 * The name of the class this class extends, if any.
84 protected $parentClassName = '';
87 * The parent PSR-0 Parser.
89 * @var \Doctrine\Common\Annotations\StaticReflectionParser
91 protected $parentStaticReflectionParser;
94 * Parses a class residing in a PSR-0 hierarchy.
96 * @param string $class
97 * The full, namespaced class name.
98 * @param ClassFinder $finder
99 * A ClassFinder object which finds the class.
100 * @param boolean $classAnnotationOptimize
101 * Only retrieve the class docComment. Presumes there is only one
102 * statement per line.
104 public function __construct($className, $finder, $classAnnotationOptimize = false)
106 $this->className = ltrim($className, '\\');
107 if ($lastNsPos = strrpos($this->className, '\\')) {
108 $this->namespace = substr($this->className, 0, $lastNsPos);
110 $this->finder = $finder;
111 $this->classAnnotationOptimize = $classAnnotationOptimize;
114 protected function parse()
116 if ($this->parsed || !$fileName = $this->finder->findFile($this->className)) {
119 $this->parsed = true;
120 $contents = file_get_contents($fileName);
121 if ($this->classAnnotationOptimize) {
122 if (preg_match("/(\A.*)^\s+(abstract|final)?\s+class\s+$className\s+{/sm", $contents, $matches)) {
123 $contents = $matches[1];
126 $tokenParser = new TokenParser($contents);
128 while ($token = $tokenParser->next(false)) {
129 if (is_array($token)) {
132 $this->useStatements = array_merge($this->useStatements, $tokenParser->parseUseStatement());
135 $docComment = $token[1];
138 $this->docComment['class'] = $docComment;
145 $token = $tokenParser->next();
146 if ($token[0] === T_VARIABLE) {
147 $propertyName = substr($token[1], 1);
148 $this->docComment['property'][$propertyName] = $docComment;
151 if ($token[0] !== T_FUNCTION) {
152 // For example, it can be T_FINAL.
157 // The next string after function is the name, but
158 // there can be & before the function name so find the
160 while (($token = $tokenParser->next()) && $token[0] !== T_STRING);
161 $methodName = $token[1];
162 $this->docComment['method'][$methodName] = $docComment;
166 $this->parentClassName = $tokenParser->parseClass();
167 $nsPos = strpos($this->parentClassName, '\\');
168 $fullySpecified = false;
170 $fullySpecified = true;
173 $prefix = strtolower(substr($this->parentClassName, 0, $nsPos));
174 $postfix = substr($this->parentClassName, $nsPos);
176 $prefix = strtolower($this->parentClassName);
179 foreach ($this->useStatements as $alias => $use) {
180 if ($alias == $prefix) {
181 $this->parentClassName = '\\' . $use . $postfix;
182 $fullySpecified = true;
186 if (!$fullySpecified) {
187 $this->parentClassName = '\\' . $this->namespace . '\\' . $this->parentClassName;
195 protected function getParentStaticReflectionParser()
197 if (empty($this->parentStaticReflectionParser)) {
198 $this->parentStaticReflectionParser = new static($this->parentClassName, $this->finder);
201 return $this->parentStaticReflectionParser;
204 public function getClassName()
206 return $this->className;
209 public function getNamespaceName()
211 return $this->namespace;
215 * Get the ReflectionClass equivalent for this file / class.
217 public function getReflectionClass()
219 return new StaticReflectionClass($this);
223 * Get the ReflectionMethod equivalent for the method of this file / class.
225 public function getReflectionMethod($methodName)
227 return new StaticReflectionMethod($this, $methodName);
231 * Get the ReflectionProperty equivalent for the method of this file / class.
233 public function getReflectionProperty($propertyName)
235 return new StaticReflectionProperty($this, $propertyName);
239 * Get the use statements from this file.
241 public function getUseStatements()
245 return $this->useStatements;
251 * @param string $type class, property or method.
252 * @param string $name Name of the property or method, not needed for class.
254 * @return string the doc comment or empty string if none.
256 public function getDocComment($type = 'class', $name = '')
260 return $name ? $this->docComment[$type][$name] : $this->docComment[$type];
264 * Get the PSR-0 parser for the declaring class.
266 * @param string $type property or method.
267 * @param string $name Name of the property or method.
269 * @return StaticReflectionParser A static reflection parser for the declaring class.
271 public function getStaticReflectionParserForDeclaringClass($type, $name)
274 if (isset($this->docComment[$type][$name])) {
277 if (!empty($this->parentClassName)) {
278 return $this->getParentStaticReflectionParser()->getStaticReflectionParserForDeclaringClass($type, $name);
280 throw new ReflectionException('Invalid ' . $type . ' "' . $name . '"');