|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace FatCode\Annotation; |
| 4 | + |
| 5 | +use FatCode\Annotation\Exception\AnnotationReaderException; |
| 6 | +use ReflectionClass; |
| 7 | +use ReflectionFunction; |
| 8 | + |
| 9 | +/** |
| 10 | + * Reads annotation from given context. |
| 11 | + */ |
| 12 | +class AnnotationReader |
| 13 | +{ |
| 14 | + private $parser; |
| 15 | + |
| 16 | + public function __construct(Parser $parser = null) |
| 17 | + { |
| 18 | + $this->parser = $parser ?? new Parser(); |
| 19 | + } |
| 20 | + |
| 21 | + public function readClassAnnotations(string $className) : array |
| 22 | + { |
| 23 | + if (!class_exists($className)) { |
| 24 | + throw AnnotationReaderException::forUndefinedClass($className); |
| 25 | + } |
| 26 | + $reflectionClass = new ReflectionClass($className); |
| 27 | + |
| 28 | + return $this->parser->parse($reflectionClass->getDocComment(), Context::fromReflectionClass($reflectionClass)); |
| 29 | + } |
| 30 | + |
| 31 | + public function readMethodAnnotations(string $className, string $methodName) : array |
| 32 | + { |
| 33 | + if (!class_exists($className)) { |
| 34 | + throw AnnotationReaderException::forUndefinedClass($className); |
| 35 | + } |
| 36 | + $reflectionClass = new ReflectionClass($className); |
| 37 | + if (!$reflectionClass->hasMethod($methodName)) { |
| 38 | + throw AnnotationReaderException::forUndefinedMethod($reflectionClass->name, $methodName); |
| 39 | + } |
| 40 | + $reflectionMethod = $reflectionClass->getMethod($methodName); |
| 41 | + |
| 42 | + return $this->parser->parse( |
| 43 | + $reflectionMethod->getDocComment(), |
| 44 | + Context::fromReflectionMethod($reflectionMethod) |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + public function readPropertyAnnotations(string $className, string $propertyName) : array |
| 49 | + { |
| 50 | + if (!class_exists($className)) { |
| 51 | + throw AnnotationReaderException::forUndefinedClass($className); |
| 52 | + } |
| 53 | + $reflectionClass = new ReflectionClass($className); |
| 54 | + if (!$reflectionClass->hasProperty($propertyName)) { |
| 55 | + throw AnnotationReaderException::forUndefinedProperty($reflectionClass->name, $propertyName); |
| 56 | + } |
| 57 | + $reflectionProperty = $reflectionClass->getProperty($propertyName); |
| 58 | + |
| 59 | + return $this->parser->parse( |
| 60 | + $reflectionProperty->getDocComment(), |
| 61 | + Context::fromReflectionProperty($reflectionProperty) |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + public function readFunctionAnnotations(string $functionName) : array |
| 66 | + { |
| 67 | + if (!function_exists($functionName)) { |
| 68 | + throw AnnotationReaderException::forUndefinedFunction($functionName); |
| 69 | + } |
| 70 | + $reflectionFunction = new ReflectionFunction($functionName); |
| 71 | + |
| 72 | + return $this->parser->parse( |
| 73 | + $reflectionFunction->getDocComment(), |
| 74 | + Context::fromReflectionFunction($reflectionFunction) |
| 75 | + ); |
| 76 | + } |
| 77 | +} |
0 commit comments