|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Overblog\GraphQLBundle\Config\Parser\Annotation; |
| 6 | + |
| 7 | +use Doctrine\Common\Annotations\AnnotationException; |
| 8 | +use Doctrine\Common\Annotations\AnnotationReader; |
| 9 | +use Doctrine\Common\Annotations\AnnotationRegistry; |
| 10 | +use ReflectionClass; |
| 11 | +use ReflectionMethod; |
| 12 | +use ReflectionProperty; |
| 13 | +use RuntimeException; |
| 14 | +use function class_exists; |
| 15 | + |
| 16 | +class GraphClass extends ReflectionClass |
| 17 | +{ |
| 18 | + private static ?AnnotationReader $annotationReader = null; |
| 19 | + |
| 20 | + protected array $annotations = []; |
| 21 | + |
| 22 | + protected array $propertiesExtended = []; |
| 23 | + |
| 24 | + /** |
| 25 | + * @param mixed $className |
| 26 | + */ |
| 27 | + public function __construct($className) |
| 28 | + { |
| 29 | + parent::__construct($className); |
| 30 | + |
| 31 | + $annotationReader = self::getAnnotationReader(); |
| 32 | + $this->annotations = $annotationReader->getClassAnnotations($this); |
| 33 | + |
| 34 | + $reflection = $this; |
| 35 | + do { |
| 36 | + foreach ($reflection->getProperties() as $property) { |
| 37 | + if (isset($this->propertiesExtended[$property->getName()])) { |
| 38 | + continue; |
| 39 | + } |
| 40 | + $this->propertiesExtended[$property->getName()] = $property; |
| 41 | + } |
| 42 | + } while ($reflection = $reflection->getParentClass()); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @return ReflectionProperty[] |
| 47 | + */ |
| 48 | + public function getPropertiesExtended() |
| 49 | + { |
| 50 | + return $this->propertiesExtended; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @param ReflectionMethod|ReflectionProperty|null $from |
| 55 | + * |
| 56 | + * @return array |
| 57 | + */ |
| 58 | + public function getAnnotations(object $from = null) |
| 59 | + { |
| 60 | + if (!$from) { |
| 61 | + return $this->annotations; |
| 62 | + } |
| 63 | + |
| 64 | + if ($from instanceof ReflectionMethod) { |
| 65 | + return self::getAnnotationReader()->getMethodAnnotations($from); |
| 66 | + } |
| 67 | + |
| 68 | + if ($from instanceof ReflectionProperty) { |
| 69 | + return self::getAnnotationReader()->getPropertyAnnotations($from); |
| 70 | + } |
| 71 | + |
| 72 | + /** @phpstan-ignore-next-line */ |
| 73 | + throw new AnnotationException(sprintf('Unable to retrieve annotations from object of class "%s".', get_class($from))); |
| 74 | + } |
| 75 | + |
| 76 | + private static function getAnnotationReader(): AnnotationReader |
| 77 | + { |
| 78 | + if (null === self::$annotationReader) { |
| 79 | + if (!class_exists(AnnotationReader::class) || |
| 80 | + !class_exists(AnnotationRegistry::class)) { |
| 81 | + throw new RuntimeException('In order to use graphql annotation, you need to require doctrine annotations'); |
| 82 | + } |
| 83 | + |
| 84 | + AnnotationRegistry::registerLoader('class_exists'); |
| 85 | + self::$annotationReader = new AnnotationReader(); |
| 86 | + } |
| 87 | + |
| 88 | + return self::$annotationReader; |
| 89 | + } |
| 90 | +} |
0 commit comments