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;
22 use Doctrine\Common\Annotations\Annotation\IgnoreAnnotation;
23 use Doctrine\Common\Annotations\Annotation\Target;
27 use ReflectionProperty;
30 * A reader for docblock annotations.
32 * @author Benjamin Eberlei <kontakt@beberlei.de>
33 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
34 * @author Jonathan Wage <jonwage@gmail.com>
35 * @author Roman Borschel <roman@code-factory.org>
36 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
38 class AnnotationReader implements Reader
41 * Global map for imports.
45 private static $globalImports = array(
46 'ignoreannotation' => 'Doctrine\Common\Annotations\Annotation\IgnoreAnnotation',
50 * A list with annotations that are not causing exceptions when not resolved to an annotation class.
52 * The names are case sensitive.
56 private static $globalIgnoredNames = array(
57 'access'=> true, 'author'=> true, 'copyright'=> true, 'deprecated'=> true,
58 'example'=> true, 'ignore'=> true, 'internal'=> true, 'link'=> true, 'see'=> true,
59 'since'=> true, 'tutorial'=> true, 'version'=> true, 'package'=> true,
60 'subpackage'=> true, 'name'=> true, 'global'=> true, 'param'=> true,
61 'return'=> true, 'staticvar'=> true, 'category'=> true, 'staticVar'=> true,
62 'static'=> true, 'var'=> true, 'throws'=> true, 'inheritdoc'=> true,
63 'inheritDoc'=> true, 'license'=> true, 'todo'=> true,
64 'deprec'=> true, 'property' => true, 'method' => true,
65 'abstract'=> true, 'exception'=> true, 'magic' => true, 'api' => true,
66 'final'=> true, 'filesource'=> true, 'throw' => true, 'uses' => true,
67 'usedby'=> true, 'private' => true, 'Annotation' => true, 'override' => true,
68 'codeCoverageIgnore' => true, 'codeCoverageIgnoreStart' => true, 'codeCoverageIgnoreEnd' => true,
69 'Required' => true, 'Attribute' => true, 'Attributes' => true,
70 'Target' => true, 'SuppressWarnings' => true,
71 'ingroup' => true, 'code' => true, 'endcode' => true,
72 'package_version' => true,
76 * Add a new annotation to the globally ignored annotation names with regard to exception handling.
80 static public function addGlobalIgnoredName($name)
82 self::$globalIgnoredNames[$name] = true;
88 * @var \Doctrine\Common\Annotations\DocParser
93 * Annotations Parser used to collect parsing metadata
95 * @var \Doctrine\Common\Annotations\DocParser
100 * PHP Parser used to collect imports.
102 * @var \Doctrine\Common\Annotations\PhpParser
107 * In-memory cache mechanism to store imported annotations per class.
111 private $imports = array();
114 * In-memory cache mechanism to store ignored annotations per class.
118 private $ignoredAnnotationNames = array();
123 * Initializes a new AnnotationReader.
125 public function __construct()
127 AnnotationRegistry::registerFile(__DIR__ . '/Annotation/IgnoreAnnotation.php');
129 $this->parser = new DocParser;
131 $this->preParser = new DocParser;
132 $this->preParser->setImports(self::$globalImports);
133 $this->preParser->setIgnoreNotImportedAnnotations(true);
135 $this->phpParser = new PhpParser;
139 * Gets the annotations applied to a class.
141 * @param ReflectionClass $class The ReflectionClass of the class from which
142 * the class annotations should be read.
143 * @return array An array of Annotations.
145 public function getClassAnnotations(ReflectionClass $class)
147 $this->parser->setTarget(Target::TARGET_CLASS);
148 $this->parser->setImports($this->getImports($class));
149 $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
151 return $this->parser->parse($class->getDocComment(), 'class ' . $class->getName());
155 * Gets a class annotation.
157 * @param ReflectionClass $class The ReflectionClass of the class from which
158 * the class annotations should be read.
159 * @param string $annotationName The name of the annotation.
160 * @return mixed The Annotation or NULL, if the requested annotation does not exist.
162 public function getClassAnnotation(ReflectionClass $class, $annotationName)
164 $annotations = $this->getClassAnnotations($class);
166 foreach ($annotations as $annotation) {
167 if ($annotation instanceof $annotationName) {
176 * Gets the annotations applied to a property.
178 * @param ReflectionProperty $property The ReflectionProperty of the property
179 * from which the annotations should be read.
180 * @return array An array of Annotations.
182 public function getPropertyAnnotations(ReflectionProperty $property)
184 $class = $property->getDeclaringClass();
185 $context = 'property ' . $class->getName() . "::\$" . $property->getName();
186 $this->parser->setTarget(Target::TARGET_PROPERTY);
187 $this->parser->setImports($this->getImports($class));
188 $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
190 return $this->parser->parse($property->getDocComment(), $context);
194 * Gets a property annotation.
196 * @param ReflectionProperty $property
197 * @param string $annotationName The name of the annotation.
198 * @return mixed The Annotation or NULL, if the requested annotation does not exist.
200 public function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
202 $annotations = $this->getPropertyAnnotations($property);
204 foreach ($annotations as $annotation) {
205 if ($annotation instanceof $annotationName) {
214 * Gets the annotations applied to a method.
216 * @param \ReflectionMethod $method The ReflectionMethod of the method from which
217 * the annotations should be read.
219 * @return array An array of Annotations.
221 public function getMethodAnnotations(ReflectionMethod $method)
223 $class = $method->getDeclaringClass();
224 $context = 'method ' . $class->getName() . '::' . $method->getName() . '()';
225 $this->parser->setTarget(Target::TARGET_METHOD);
226 $this->parser->setImports($this->getImports($class));
227 $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
229 return $this->parser->parse($method->getDocComment(), $context);
233 * Gets a method annotation.
235 * @param ReflectionMethod $method
236 * @param string $annotationName The name of the annotation.
237 * @return mixed The Annotation or NULL, if the requested annotation does not exist.
239 public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
241 $annotations = $this->getMethodAnnotations($method);
243 foreach ($annotations as $annotation) {
244 if ($annotation instanceof $annotationName) {
253 * Returns the ignored annotations for the given class.
255 * @param ReflectionClass $class
258 private function getIgnoredAnnotationNames(ReflectionClass $class)
260 if (isset($this->ignoredAnnotationNames[$name = $class->getName()])) {
261 return $this->ignoredAnnotationNames[$name];
263 $this->collectParsingMetadata($class);
265 return $this->ignoredAnnotationNames[$name];
271 * @param \ReflectionClass $class
274 private function getImports(ReflectionClass $class)
276 if (isset($this->imports[$name = $class->getName()])) {
277 return $this->imports[$name];
279 $this->collectParsingMetadata($class);
281 return $this->imports[$name];
285 * Collects parsing metadata for a given class
287 * @param ReflectionClass $class
289 private function collectParsingMetadata(ReflectionClass $class)
291 $ignoredAnnotationNames = self::$globalIgnoredNames;
293 $annotations = $this->preParser->parse($class->getDocComment(), 'class '.$class->name);
294 foreach ($annotations as $annotation) {
295 if ($annotation instanceof IgnoreAnnotation) {
296 foreach ($annotation->names AS $annot) {
297 $ignoredAnnotationNames[$annot] = true;
302 $name = $class->getName();
303 $this->imports[$name] = array_merge(
304 self::$globalImports,
305 $this->phpParser->parseClass($class),
306 array('__NAMESPACE__' => $class->getNamespaceName())
308 $this->ignoredAnnotationNames[$name] = $ignoredAnnotationNames;