[345] | 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 LGPL. 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 | final 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, 'deprecated'=> true, |
---|
| 64 | 'deprec'=> true, 'author'=> 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 | ); |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | * Add a new annotation to the globally ignored annotation names with regard to exception handling. |
---|
| 75 | * |
---|
| 76 | * @param string $name |
---|
| 77 | */ |
---|
| 78 | static public function addGlobalIgnoredName($name) |
---|
| 79 | { |
---|
| 80 | self::$globalIgnoredNames[$name] = true; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | /** |
---|
| 84 | * Annotations Parser |
---|
| 85 | * |
---|
| 86 | * @var Doctrine\Common\Annotations\DocParser |
---|
| 87 | */ |
---|
| 88 | private $parser; |
---|
| 89 | |
---|
| 90 | /** |
---|
| 91 | * Annotations Parser used to collect parsing metadata |
---|
| 92 | * |
---|
| 93 | * @var Doctrine\Common\Annotations\DocParser |
---|
| 94 | */ |
---|
| 95 | private $preParser; |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | * PHP Parser used to collect imports. |
---|
| 99 | * |
---|
| 100 | * @var Doctrine\Common\Annotations\PhpParser |
---|
| 101 | */ |
---|
| 102 | private $phpParser; |
---|
| 103 | |
---|
| 104 | /** |
---|
| 105 | * In-memory cache mechanism to store imported annotations per class. |
---|
| 106 | * |
---|
| 107 | * @var array |
---|
| 108 | */ |
---|
| 109 | private $imports = array(); |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | * In-memory cache mechanism to store ignored annotations per class. |
---|
| 113 | * |
---|
| 114 | * @var array |
---|
| 115 | */ |
---|
| 116 | private $ignoredAnnotationNames = array(); |
---|
| 117 | |
---|
| 118 | /** |
---|
| 119 | * Constructor. |
---|
| 120 | * |
---|
| 121 | * Initializes a new AnnotationReader. |
---|
| 122 | */ |
---|
| 123 | public function __construct() |
---|
| 124 | { |
---|
| 125 | AnnotationRegistry::registerFile(__DIR__ . '/Annotation/IgnoreAnnotation.php'); |
---|
| 126 | |
---|
| 127 | $this->parser = new DocParser; |
---|
| 128 | |
---|
| 129 | $this->preParser = new DocParser; |
---|
| 130 | $this->preParser->setImports(self::$globalImports); |
---|
| 131 | $this->preParser->setIgnoreNotImportedAnnotations(true); |
---|
| 132 | |
---|
| 133 | $this->phpParser = new PhpParser; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | /** |
---|
| 137 | * Gets the annotations applied to a class. |
---|
| 138 | * |
---|
| 139 | * @param ReflectionClass $class The ReflectionClass of the class from which |
---|
| 140 | * the class annotations should be read. |
---|
| 141 | * @return array An array of Annotations. |
---|
| 142 | */ |
---|
| 143 | public function getClassAnnotations(ReflectionClass $class) |
---|
| 144 | { |
---|
| 145 | $this->parser->setTarget(Target::TARGET_CLASS); |
---|
| 146 | $this->parser->setImports($this->getImports($class)); |
---|
| 147 | $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class)); |
---|
| 148 | |
---|
| 149 | return $this->parser->parse($class->getDocComment(), 'class ' . $class->getName()); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | /** |
---|
| 153 | * Gets a class annotation. |
---|
| 154 | * |
---|
| 155 | * @param ReflectionClass $class The ReflectionClass of the class from which |
---|
| 156 | * the class annotations should be read. |
---|
| 157 | * @param string $annotationName The name of the annotation. |
---|
| 158 | * @return The Annotation or NULL, if the requested annotation does not exist. |
---|
| 159 | */ |
---|
| 160 | public function getClassAnnotation(ReflectionClass $class, $annotationName) |
---|
| 161 | { |
---|
| 162 | $annotations = $this->getClassAnnotations($class); |
---|
| 163 | |
---|
| 164 | foreach ($annotations as $annotation) { |
---|
| 165 | if ($annotation instanceof $annotationName) { |
---|
| 166 | return $annotation; |
---|
| 167 | } |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | return null; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | /** |
---|
| 174 | * Gets the annotations applied to a property. |
---|
| 175 | * |
---|
| 176 | * @param ReflectionProperty $property The ReflectionProperty of the property |
---|
| 177 | * from which the annotations should be read. |
---|
| 178 | * @return array An array of Annotations. |
---|
| 179 | */ |
---|
| 180 | public function getPropertyAnnotations(ReflectionProperty $property) |
---|
| 181 | { |
---|
| 182 | $class = $property->getDeclaringClass(); |
---|
| 183 | $context = 'property ' . $class->getName() . "::\$" . $property->getName(); |
---|
| 184 | $this->parser->setTarget(Target::TARGET_PROPERTY); |
---|
| 185 | $this->parser->setImports($this->getImports($class)); |
---|
| 186 | $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class)); |
---|
| 187 | |
---|
| 188 | return $this->parser->parse($property->getDocComment(), $context); |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | /** |
---|
| 192 | * Gets a property annotation. |
---|
| 193 | * |
---|
| 194 | * @param ReflectionProperty $property |
---|
| 195 | * @param string $annotationName The name of the annotation. |
---|
| 196 | * @return The Annotation or NULL, if the requested annotation does not exist. |
---|
| 197 | */ |
---|
| 198 | public function getPropertyAnnotation(ReflectionProperty $property, $annotationName) |
---|
| 199 | { |
---|
| 200 | $annotations = $this->getPropertyAnnotations($property); |
---|
| 201 | |
---|
| 202 | foreach ($annotations as $annotation) { |
---|
| 203 | if ($annotation instanceof $annotationName) { |
---|
| 204 | return $annotation; |
---|
| 205 | } |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | return null; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | /** |
---|
| 212 | * Gets the annotations applied to a method. |
---|
| 213 | * |
---|
| 214 | * @param ReflectionMethod $property The ReflectionMethod of the method from which |
---|
| 215 | * the annotations should be read. |
---|
| 216 | * @return array An array of Annotations. |
---|
| 217 | */ |
---|
| 218 | public function getMethodAnnotations(ReflectionMethod $method) |
---|
| 219 | { |
---|
| 220 | $class = $method->getDeclaringClass(); |
---|
| 221 | $context = 'method ' . $class->getName() . '::' . $method->getName() . '()'; |
---|
| 222 | $this->parser->setTarget(Target::TARGET_METHOD); |
---|
| 223 | $this->parser->setImports($this->getImports($class)); |
---|
| 224 | $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class)); |
---|
| 225 | |
---|
| 226 | return $this->parser->parse($method->getDocComment(), $context); |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | /** |
---|
| 230 | * Gets a method annotation. |
---|
| 231 | * |
---|
| 232 | * @param ReflectionMethod $method |
---|
| 233 | * @param string $annotationName The name of the annotation. |
---|
| 234 | * @return The Annotation or NULL, if the requested annotation does not exist. |
---|
| 235 | */ |
---|
| 236 | public function getMethodAnnotation(ReflectionMethod $method, $annotationName) |
---|
| 237 | { |
---|
| 238 | $annotations = $this->getMethodAnnotations($method); |
---|
| 239 | |
---|
| 240 | foreach ($annotations as $annotation) { |
---|
| 241 | if ($annotation instanceof $annotationName) { |
---|
| 242 | return $annotation; |
---|
| 243 | } |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | return null; |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | /** |
---|
| 250 | * Returns the ignored annotations for the given class. |
---|
| 251 | * |
---|
| 252 | * @param ReflectionClass $class |
---|
| 253 | * @return array |
---|
| 254 | */ |
---|
| 255 | private function getIgnoredAnnotationNames(ReflectionClass $class) |
---|
| 256 | { |
---|
| 257 | if (isset($this->ignoredAnnotationNames[$name = $class->getName()])) { |
---|
| 258 | return $this->ignoredAnnotationNames[$name]; |
---|
| 259 | } |
---|
| 260 | $this->collectParsingMetadata($class); |
---|
| 261 | |
---|
| 262 | return $this->ignoredAnnotationNames[$name]; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | private function getImports(ReflectionClass $class) |
---|
| 266 | { |
---|
| 267 | if (isset($this->imports[$name = $class->getName()])) { |
---|
| 268 | return $this->imports[$name]; |
---|
| 269 | } |
---|
| 270 | $this->collectParsingMetadata($class); |
---|
| 271 | |
---|
| 272 | return $this->imports[$name]; |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | /** |
---|
| 276 | * Collects parsing metadata for a given class |
---|
| 277 | * |
---|
| 278 | * @param ReflectionClass $class |
---|
| 279 | */ |
---|
| 280 | private function collectParsingMetadata(ReflectionClass $class) |
---|
| 281 | { |
---|
| 282 | $ignoredAnnotationNames = self::$globalIgnoredNames; |
---|
| 283 | |
---|
| 284 | $annotations = $this->preParser->parse($class->getDocComment(), 'class '.$class->name); |
---|
| 285 | foreach ($annotations as $annotation) { |
---|
| 286 | if ($annotation instanceof IgnoreAnnotation) { |
---|
| 287 | foreach ($annotation->names AS $annot) { |
---|
| 288 | $ignoredAnnotationNames[$annot] = true; |
---|
| 289 | } |
---|
| 290 | } |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | $name = $class->getName(); |
---|
| 294 | $this->imports[$name] = array_merge( |
---|
| 295 | self::$globalImports, |
---|
| 296 | $this->phpParser->parseClass($class), |
---|
| 297 | array('__NAMESPACE__' => $class->getNamespaceName()) |
---|
| 298 | ); |
---|
| 299 | $this->ignoredAnnotationNames[$name] = $ignoredAnnotationNames; |
---|
| 300 | } |
---|
| 301 | } |
---|