|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace TheCodingMachine\GraphQLite\Mappers\Root; |
| 6 | + |
| 7 | +use GraphQL\Type\Definition\InputType; |
| 8 | +use GraphQL\Type\Definition\NamedType; |
| 9 | +use GraphQL\Type\Definition\OutputType; |
| 10 | +use GraphQL\Type\Definition\Type as GraphQLType; |
| 11 | +use phpDocumentor\Reflection\DocBlock; |
| 12 | +use phpDocumentor\Reflection\Type; |
| 13 | +use phpDocumentor\Reflection\Types\Object_; |
| 14 | +use ReflectionClass; |
| 15 | +use ReflectionEnum; |
| 16 | +use ReflectionMethod; |
| 17 | +use ReflectionProperty; |
| 18 | +use Symfony\Contracts\Cache\CacheInterface; |
| 19 | +use TheCodingMachine\GraphQLite\AnnotationReader; |
| 20 | +use TheCodingMachine\GraphQLite\Types\EnumType; |
| 21 | +use TheCodingMachine\GraphQLite\Utils\Namespaces\NS; |
| 22 | +use UnitEnum; |
| 23 | + |
| 24 | +use function assert; |
| 25 | +use function enum_exists; |
| 26 | + |
| 27 | +/** |
| 28 | + * Maps an enum class to a GraphQL type (only available in PHP>=8.1) |
| 29 | + */ |
| 30 | +class EnumTypeMapper implements RootTypeMapperInterface |
| 31 | +{ |
| 32 | + /** @var array<class-string<UnitEnum>, EnumType> */ |
| 33 | + private $cache = []; |
| 34 | + /** @var array<string, EnumType> */ |
| 35 | + private $cacheByName = []; |
| 36 | + /** @var array<string, class-string<UnitEnum>> */ |
| 37 | + private $nameToClassMapping; |
| 38 | + /** @var RootTypeMapperInterface */ |
| 39 | + private $next; |
| 40 | + /** @var AnnotationReader */ |
| 41 | + private $annotationReader; |
| 42 | + /** @var array|NS[] */ |
| 43 | + private $namespaces; |
| 44 | + /** @var CacheInterface */ |
| 45 | + private $cacheService; |
| 46 | + |
| 47 | + /** |
| 48 | + * @param NS[] $namespaces List of namespaces containing enums. Used when searching an enum by name. |
| 49 | + */ |
| 50 | + public function __construct( |
| 51 | + RootTypeMapperInterface $next, |
| 52 | + AnnotationReader $annotationReader, |
| 53 | + CacheInterface $cacheService, |
| 54 | + array $namespaces |
| 55 | + ) { |
| 56 | + $this->next = $next; |
| 57 | + $this->annotationReader = $annotationReader; |
| 58 | + $this->cacheService = $cacheService; |
| 59 | + $this->namespaces = $namespaces; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @param (OutputType&GraphQLType)|null $subType |
| 64 | + * @param ReflectionMethod|ReflectionProperty $reflector |
| 65 | + * |
| 66 | + * @return OutputType&GraphQLType |
| 67 | + */ |
| 68 | + public function toGraphQLOutputType( |
| 69 | + Type $type, |
| 70 | + ?OutputType $subType, |
| 71 | + $reflector, |
| 72 | + DocBlock $docBlockObj |
| 73 | + ): OutputType { |
| 74 | + $result = $this->map($type); |
| 75 | + if ($result === null) { |
| 76 | + return $this->next->toGraphQLOutputType($type, $subType, $reflector, $docBlockObj); |
| 77 | + } |
| 78 | + |
| 79 | + return $result; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Maps into the appropriate InputType |
| 84 | + * |
| 85 | + * @param InputType|GraphQLType|null $subType |
| 86 | + * @param ReflectionMethod|ReflectionProperty $reflector |
| 87 | + * |
| 88 | + * @return InputType|GraphQLType |
| 89 | + */ |
| 90 | + public function toGraphQLInputType( |
| 91 | + Type $type, |
| 92 | + ?InputType $subType, |
| 93 | + string $argumentName, |
| 94 | + $reflector, |
| 95 | + DocBlock $docBlockObj |
| 96 | + ): InputType |
| 97 | + { |
| 98 | + $result = $this->map($type); |
| 99 | + if ($result === null) { |
| 100 | + return $this->next->toGraphQLInputType($type, $subType, $argumentName, $reflector, $docBlockObj); |
| 101 | + } |
| 102 | + |
| 103 | + return $result; |
| 104 | + } |
| 105 | + |
| 106 | + private function map(Type $type): ?EnumType |
| 107 | + { |
| 108 | + if (! $type instanceof Object_) { |
| 109 | + return null; |
| 110 | + } |
| 111 | + $fqsen = $type->getFqsen(); |
| 112 | + if ($fqsen === null) { |
| 113 | + return null; |
| 114 | + } |
| 115 | + |
| 116 | + /** @var class-string<object> $enumClass */ |
| 117 | + $enumClass = (string) $fqsen; |
| 118 | + |
| 119 | + return $this->mapByClassName($enumClass); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @param class-string $enumClass |
| 124 | + */ |
| 125 | + private function mapByClassName(string $enumClass): ?EnumType |
| 126 | + { |
| 127 | + if (isset($this->cache[$enumClass])) { |
| 128 | + return $this->cache[$enumClass]; |
| 129 | + } |
| 130 | + |
| 131 | + if (! enum_exists($enumClass)) { |
| 132 | + return null; |
| 133 | + } |
| 134 | + |
| 135 | + // phpcs:disable SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration.MissingVariable |
| 136 | + /** @var class-string<UnitEnum> $enumClass */ |
| 137 | + // phpcs:enable SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration.MissingVariable |
| 138 | + |
| 139 | + $reflectionEnum = new ReflectionEnum($enumClass); |
| 140 | + |
| 141 | + $typeAnnotation = $this->annotationReader->getTypeAnnotation($reflectionEnum); |
| 142 | + $typeName = ($typeAnnotation !== null ? $typeAnnotation->getName() : null) ?? $reflectionEnum->getShortName(); |
| 143 | + |
| 144 | + // Expose values instead of names if specifically configured to and if enum is string-backed |
| 145 | + $useValues = $typeAnnotation !== null && |
| 146 | + $typeAnnotation->useEnumValues() && |
| 147 | + $reflectionEnum->isBacked() && |
| 148 | + (string) $reflectionEnum->getBackingType() === 'string'; |
| 149 | + |
| 150 | + $type = new EnumType($enumClass, $typeName, $useValues); |
| 151 | + |
| 152 | + return $this->cacheByName[$typeName] = $this->cache[$enumClass] = $type; |
| 153 | + } |
| 154 | + |
| 155 | + private function getTypeName(ReflectionClass $reflectionClass): string |
| 156 | + { |
| 157 | + $typeAnnotation = $this->annotationReader->getTypeAnnotation($reflectionClass); |
| 158 | + |
| 159 | + return ($typeAnnotation !== null ? $typeAnnotation->getName() : null) ?? $reflectionClass->getShortName(); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Returns a GraphQL type by name. |
| 164 | + * If this root type mapper can return this type in "toGraphQLOutputType" or "toGraphQLInputType", it should |
| 165 | + * also map these types by name in the "mapNameToType" method. |
| 166 | + * |
| 167 | + * @param string $typeName The name of the GraphQL type |
| 168 | + */ |
| 169 | + public function mapNameToType(string $typeName): NamedType |
| 170 | + { |
| 171 | + // This is a hack to make sure "$schema->assertValid()" returns true. |
| 172 | + // The mapNameToType will fail if the mapByClassName method was not called before. |
| 173 | + // This is actually not an issue in real life scenarios where enum types are never queried by type name. |
| 174 | + if (isset($this->cacheByName[$typeName])) { |
| 175 | + return $this->cacheByName[$typeName]; |
| 176 | + } |
| 177 | + |
| 178 | + $nameToClassMapping = $this->getNameToClassMapping(); |
| 179 | + if (isset($this->nameToClassMapping[$typeName])) { |
| 180 | + $className = $nameToClassMapping[$typeName]; |
| 181 | + $type = $this->mapByClassName($className); |
| 182 | + assert($type !== null); |
| 183 | + return $type; |
| 184 | + } |
| 185 | + |
| 186 | + return $this->next->mapNameToType($typeName); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Go through all classes in the defined namespaces and loads the cache. |
| 191 | + * |
| 192 | + * @return array<string, class-string<UnitEnum>> |
| 193 | + */ |
| 194 | + private function getNameToClassMapping(): array |
| 195 | + { |
| 196 | + if ($this->nameToClassMapping === null) { |
| 197 | + $this->nameToClassMapping = $this->cacheService->get('enum_name_to_class', function () { |
| 198 | + $nameToClassMapping = []; |
| 199 | + foreach ($this->namespaces as $ns) { |
| 200 | + foreach ($ns->getClassList() as $className => $classRef) { |
| 201 | + if (! enum_exists($className)) { |
| 202 | + continue; |
| 203 | + } |
| 204 | + |
| 205 | + $nameToClassMapping[$this->getTypeName($classRef)] = $className; |
| 206 | + } |
| 207 | + } |
| 208 | + return $nameToClassMapping; |
| 209 | + }); |
| 210 | + } |
| 211 | + |
| 212 | + return $this->nameToClassMapping; |
| 213 | + } |
| 214 | +} |
0 commit comments