Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / common / lib / Doctrine / Common / Annotations / AnnotationReader.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\IgnoreAnnotation;
23 use Doctrine\Common\Annotations\Annotation\Target;
24 use Closure;
25 use ReflectionClass;
26 use ReflectionMethod;
27 use ReflectionProperty;
28
29 /**
30  * A reader for docblock annotations.
31  *
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>
37  */
38 class AnnotationReader implements Reader
39 {
40     /**
41      * Global map for imports.
42      *
43      * @var array
44      */
45     private static $globalImports = array(
46         'ignoreannotation' => 'Doctrine\Common\Annotations\Annotation\IgnoreAnnotation',
47     );
48
49     /**
50      * A list with annotations that are not causing exceptions when not resolved to an annotation class.
51      *
52      * The names are case sensitive.
53      *
54      * @var array
55      */
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,
73     );
74
75     /**
76      * Add a new annotation to the globally ignored annotation names with regard to exception handling.
77      *
78      * @param string $name
79      */
80     static public function addGlobalIgnoredName($name)
81     {
82         self::$globalIgnoredNames[$name] = true;
83     }
84
85     /**
86      * Annotations Parser
87      *
88      * @var \Doctrine\Common\Annotations\DocParser
89      */
90     private $parser;
91
92     /**
93      * Annotations Parser used to collect parsing metadata
94      *
95      * @var \Doctrine\Common\Annotations\DocParser
96      */
97     private $preParser;
98
99     /**
100      * PHP Parser used to collect imports.
101      *
102      * @var \Doctrine\Common\Annotations\PhpParser
103      */
104     private $phpParser;
105
106     /**
107      * In-memory cache mechanism to store imported annotations per class.
108      *
109      * @var array
110      */
111     private $imports = array();
112
113     /**
114      * In-memory cache mechanism to store ignored annotations per class.
115      *
116      * @var array
117      */
118     private $ignoredAnnotationNames = array();
119
120     /**
121      * Constructor.
122      *
123      * Initializes a new AnnotationReader.
124      */
125     public function __construct()
126     {
127         AnnotationRegistry::registerFile(__DIR__ . '/Annotation/IgnoreAnnotation.php');
128
129         $this->parser = new DocParser;
130
131         $this->preParser = new DocParser;
132         $this->preParser->setImports(self::$globalImports);
133         $this->preParser->setIgnoreNotImportedAnnotations(true);
134
135         $this->phpParser = new PhpParser;
136     }
137
138     /**
139      * Gets the annotations applied to a class.
140      *
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.
144      */
145     public function getClassAnnotations(ReflectionClass $class)
146     {
147         $this->parser->setTarget(Target::TARGET_CLASS);
148         $this->parser->setImports($this->getImports($class));
149         $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
150
151         return $this->parser->parse($class->getDocComment(), 'class ' . $class->getName());
152     }
153
154     /**
155      * Gets a class annotation.
156      *
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.
161      */
162     public function getClassAnnotation(ReflectionClass $class, $annotationName)
163     {
164         $annotations = $this->getClassAnnotations($class);
165
166         foreach ($annotations as $annotation) {
167             if ($annotation instanceof $annotationName) {
168                 return $annotation;
169             }
170         }
171
172         return null;
173     }
174
175     /**
176      * Gets the annotations applied to a property.
177      *
178      * @param ReflectionProperty $property The ReflectionProperty of the property
179      *                                     from which the annotations should be read.
180      * @return array An array of Annotations.
181      */
182     public function getPropertyAnnotations(ReflectionProperty $property)
183     {
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));
189
190         return $this->parser->parse($property->getDocComment(), $context);
191     }
192
193     /**
194      * Gets a property annotation.
195      *
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.
199      */
200     public function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
201     {
202         $annotations = $this->getPropertyAnnotations($property);
203
204         foreach ($annotations as $annotation) {
205             if ($annotation instanceof $annotationName) {
206                 return $annotation;
207             }
208         }
209
210         return null;
211     }
212
213     /**
214      * Gets the annotations applied to a method.
215      *
216      * @param \ReflectionMethod $method The ReflectionMethod of the method from which
217      *                                   the annotations should be read.
218      *
219      * @return array An array of Annotations.
220      */
221     public function getMethodAnnotations(ReflectionMethod $method)
222     {
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));
228
229         return $this->parser->parse($method->getDocComment(), $context);
230     }
231
232     /**
233      * Gets a method annotation.
234      *
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.
238      */
239     public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
240     {
241         $annotations = $this->getMethodAnnotations($method);
242
243         foreach ($annotations as $annotation) {
244             if ($annotation instanceof $annotationName) {
245                 return $annotation;
246             }
247         }
248
249         return null;
250     }
251
252     /**
253      * Returns the ignored annotations for the given class.
254      *
255      * @param ReflectionClass $class
256      * @return array
257      */
258     private function getIgnoredAnnotationNames(ReflectionClass $class)
259     {
260         if (isset($this->ignoredAnnotationNames[$name = $class->getName()])) {
261             return $this->ignoredAnnotationNames[$name];
262         }
263         $this->collectParsingMetadata($class);
264
265         return $this->ignoredAnnotationNames[$name];
266     }
267
268     /**
269      * Retrieve imports
270      *
271      * @param \ReflectionClass $class
272      * @return array
273      */
274     private function getImports(ReflectionClass $class)
275     {
276         if (isset($this->imports[$name = $class->getName()])) {
277             return $this->imports[$name];
278         }
279         $this->collectParsingMetadata($class);
280
281         return $this->imports[$name];
282     }
283
284     /**
285      * Collects parsing metadata for a given class
286      *
287      * @param ReflectionClass $class
288      */
289     private function collectParsingMetadata(ReflectionClass $class)
290     {
291         $ignoredAnnotationNames = self::$globalIgnoredNames;
292
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;
298                 }
299             }
300         }
301
302         $name = $class->getName();
303         $this->imports[$name] = array_merge(
304             self::$globalImports,
305             $this->phpParser->parseClass($class),
306             array('__NAMESPACE__' => $class->getNamespaceName())
307         );
308         $this->ignoredAnnotationNames[$name] = $ignoredAnnotationNames;
309     }
310 }