Ecodev /
graphql-doctrine
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace GraphQL\Doctrine\Attribute\Reader; |
||
| 6 | |||
| 7 | use GraphQL\Doctrine\Attribute\ApiAttribute; |
||
| 8 | use GraphQL\Doctrine\Exception; |
||
| 9 | use ReflectionClass; |
||
| 10 | use ReflectionMethod; |
||
| 11 | use ReflectionParameter; |
||
| 12 | use ReflectionProperty; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * API attribute reader. |
||
| 16 | */ |
||
| 17 | final class Reader |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Return an array of all attributes found in the class hierarchy, including its traits, indexed by the class name. |
||
| 21 | * |
||
| 22 | * @template T of ApiAttribute |
||
| 23 | * |
||
| 24 | * @param class-string<T> $attributeName |
||
| 25 | * |
||
| 26 | * @return array<class-string, T[]> attributes indexed by the class name where they were found |
||
| 27 | */ |
||
| 28 | 29 | public function getRecursiveClassAttributes(ReflectionClass $class, string $attributeName): array |
|
| 29 | { |
||
| 30 | 29 | $result = []; |
|
| 31 | |||
| 32 | 29 | $attributes = $this->getAttributeInstances($class, $attributeName); |
|
| 33 | 29 | if ($attributes) { |
|
| 34 | 29 | $result[$class->getName()] = $attributes; |
|
| 35 | } |
||
| 36 | |||
| 37 | 29 | foreach ($class->getTraits() as $trait) { |
|
| 38 | 2 | $result = array_merge($result, self::getRecursiveClassAttributes($trait, $attributeName)); |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 39 | } |
||
| 40 | |||
| 41 | 29 | $parent = $class->getParentClass(); |
|
| 42 | 29 | if ($parent) { |
|
| 43 | 27 | $result = array_merge($result, self::getRecursiveClassAttributes($parent, $attributeName)); |
|
| 44 | } |
||
| 45 | |||
| 46 | 29 | return $result; |
|
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @template T of ApiAttribute |
||
| 51 | * |
||
| 52 | * @param class-string<T> $attributeName |
||
| 53 | * |
||
| 54 | * @return null|T |
||
| 55 | */ |
||
| 56 | 47 | public function getAttribute(ReflectionClass|ReflectionProperty|ReflectionMethod|ReflectionParameter $element, string $attributeName): ?ApiAttribute |
|
| 57 | { |
||
| 58 | 47 | $attributes = $this->getAttributeInstances($element, $attributeName); |
|
| 59 | |||
| 60 | 45 | return reset($attributes) ?: null; |
|
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @template T of ApiAttribute |
||
| 65 | * |
||
| 66 | * @param class-string<T> $attributeName |
||
| 67 | * |
||
| 68 | * @return T[] |
||
| 69 | */ |
||
| 70 | 58 | private function getAttributeInstances(ReflectionClass|ReflectionMethod|ReflectionParameter|ReflectionProperty $element, string $attributeName): array |
|
| 71 | { |
||
| 72 | 58 | if (!is_subclass_of($attributeName, ApiAttribute::class)) { |
|
| 73 | 1 | throw new Exception(self::class . ' cannot be used for attribute than are not part of `ecodev/graphql-doctrine`.'); |
|
| 74 | } |
||
| 75 | |||
| 76 | 57 | $attributes = $element->getAttributes($attributeName); |
|
| 77 | 57 | $instances = []; |
|
| 78 | |||
| 79 | 57 | foreach ($attributes as $attribute) { |
|
| 80 | 52 | $instance = $attribute->newInstance(); |
|
| 81 | 51 | $instances[] = $instance; |
|
| 82 | } |
||
| 83 | |||
| 84 | 56 | return $instances; |
|
| 85 | } |
||
| 86 | } |
||
| 87 |