Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / common / lib / Doctrine / Common / Annotations / SimpleAnnotationReader.php
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
20 namespace Doctrine\Common\Annotations;
21
22 use Doctrine\Common\Annotations\Annotation\Target;
23
24 /**
25  * Simple Annotation Reader.
26  *
27  * This annotation reader is intended to be used in projects where you have
28  * full-control over all annotations that are available.
29  *
30  * @since  2.2
31  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
32  * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
33  */
34 class SimpleAnnotationReader implements Reader
35 {
36     /**
37      * @var DocParser
38      */
39     private $parser;
40
41     /**
42      * Constructor.
43      *
44      * Initializes a new SimpleAnnotationReader.
45      */
46     public function __construct()
47     {
48         $this->parser = new DocParser();
49         $this->parser->setIgnoreNotImportedAnnotations(true);
50     }
51
52     /**
53      * Adds a namespace in which we will look for annotations.
54      *
55      * @param string $namespace
56      */
57     public function addNamespace($namespace)
58     {
59         $this->parser->addNamespace($namespace);
60     }
61
62     /**
63      * Gets the annotations applied to a class.
64      *
65      * @param \ReflectionClass $class The ReflectionClass of the class from which
66      *                               the class annotations should be read.
67      *
68      * @return array An array of Annotations.
69      */
70     public function getClassAnnotations(\ReflectionClass $class)
71     {
72         return $this->parser->parse($class->getDocComment(), 'class '.$class->getName());
73     }
74
75     /**
76      * Gets the annotations applied to a method.
77      *
78      * @param \ReflectionMethod $method The ReflectionMethod of the method from which
79      *                                   the annotations should be read.
80      *
81      * @return array An array of Annotations.
82      */
83     public function getMethodAnnotations(\ReflectionMethod $method)
84     {
85         return $this->parser->parse($method->getDocComment(), 'method '.$method->getDeclaringClass()->name.'::'.$method->getName().'()');
86     }
87
88     /**
89      * Gets the annotations applied to a property.
90      *
91      * @param \ReflectionProperty $property The ReflectionProperty of the property
92      *                                     from which the annotations should be read.
93      *
94      * @return array An array of Annotations.
95      */
96     public function getPropertyAnnotations(\ReflectionProperty $property)
97     {
98         return $this->parser->parse($property->getDocComment(), 'property '.$property->getDeclaringClass()->name.'::$'.$property->getName());
99     }
100
101     /**
102      * Gets a class annotation.
103      *
104      * @param \ReflectionClass $class The ReflectionClass of the class from which
105      *                               the class annotations should be read.
106      * @param string $annotationName The name of the annotation.
107      *
108      * @return mixed The Annotation or NULL, if the requested annotation does not exist.
109      */
110     public function getClassAnnotation(\ReflectionClass $class, $annotationName)
111     {
112         foreach ($this->getClassAnnotations($class) as $annot) {
113             if ($annot instanceof $annotationName) {
114                 return $annot;
115             }
116         }
117
118         return null;
119     }
120
121     /**
122      * Gets a method annotation.
123      *
124      * @param \ReflectionMethod $method
125      * @param string $annotationName The name of the annotation.
126      *
127      * @return mixed The Annotation or NULL, if the requested annotation does not exist.
128      */
129     public function getMethodAnnotation(\ReflectionMethod $method, $annotationName)
130     {
131         foreach ($this->getMethodAnnotations($method) as $annot) {
132             if ($annot instanceof $annotationName) {
133                 return $annot;
134             }
135         }
136
137         return null;
138     }
139
140     /**
141      * Gets a property annotation.
142      *
143      * @param \ReflectionProperty $property
144      * @param string $annotationName The name of the annotation.
145      * @return mixed The Annotation or NULL, if the requested annotation does not exist.
146      */
147     public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName)
148     {
149         foreach ($this->getPropertyAnnotations($property) as $annot) {
150             if ($annot instanceof $annotationName) {
151                 return $annot;
152             }
153         }
154
155         return null;
156     }
157 }