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
20namespace Doctrine\Common\Annotations;
21
22/**
23 * Simple Annotation Reader.
24 *
25 * This annotation reader is intended to be used in projects where you have
26 * full-control over all annotations that are available.
27 *
28 * @since  2.2
29 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
30 * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
31 */
32class SimpleAnnotationReader implements Reader
33{
34    /**
35     * @var DocParser
36     */
37    private $parser;
38
39    /**
40     * Constructor.
41     *
42     * Initializes a new SimpleAnnotationReader.
43     */
44    public function __construct()
45    {
46        $this->parser = new DocParser();
47        $this->parser->setIgnoreNotImportedAnnotations(true);
48    }
49
50    /**
51     * Adds a namespace in which we will look for annotations.
52     *
53     * @param string $namespace
54     *
55     * @return void
56     */
57    public function addNamespace($namespace)
58    {
59        $this->parser->addNamespace($namespace);
60    }
61
62    /**
63     * {@inheritDoc}
64     */
65    public function getClassAnnotations(\ReflectionClass $class)
66    {
67        return $this->parser->parse($class->getDocComment(), 'class '.$class->getName());
68    }
69
70    /**
71     * {@inheritDoc}
72     */
73    public function getMethodAnnotations(\ReflectionMethod $method)
74    {
75        return $this->parser->parse($method->getDocComment(), 'method '.$method->getDeclaringClass()->name.'::'.$method->getName().'()');
76    }
77
78    /**
79     * {@inheritDoc}
80     */
81    public function getPropertyAnnotations(\ReflectionProperty $property)
82    {
83        return $this->parser->parse($property->getDocComment(), 'property '.$property->getDeclaringClass()->name.'::$'.$property->getName());
84    }
85
86    /**
87     * {@inheritDoc}
88     */
89    public function getClassAnnotation(\ReflectionClass $class, $annotationName)
90    {
91        foreach ($this->getClassAnnotations($class) as $annot) {
92            if ($annot instanceof $annotationName) {
93                return $annot;
94            }
95        }
96
97        return null;
98    }
99
100    /**
101     * {@inheritDoc}
102     */
103    public function getMethodAnnotation(\ReflectionMethod $method, $annotationName)
104    {
105        foreach ($this->getMethodAnnotations($method) as $annot) {
106            if ($annot instanceof $annotationName) {
107                return $annot;
108            }
109        }
110
111        return null;
112    }
113
114    /**
115     * {@inheritDoc}
116     */
117    public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName)
118    {
119        foreach ($this->getPropertyAnnotations($property) as $annot) {
120            if ($annot instanceof $annotationName) {
121                return $annot;
122            }
123        }
124
125        return null;
126    }
127}
128