-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathMyCLabsEnumTypeMapper.php
More file actions
174 lines (154 loc) · 5.65 KB
/
MyCLabsEnumTypeMapper.php
File metadata and controls
174 lines (154 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
declare(strict_types=1);
namespace TheCodingMachine\GraphQLite\Mappers\Root;
use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\InputType;
use GraphQL\Type\Definition\NamedType;
use GraphQL\Type\Definition\OutputType;
use MyCLabs\Enum\Enum;
use phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\Type;
use phpDocumentor\Reflection\Types\Object_;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
use TheCodingMachine\GraphQLite\AnnotationReader;
use TheCodingMachine\GraphQLite\Discovery\Cache\ClassFinderComputedCache;
use TheCodingMachine\GraphQLite\Discovery\ClassFinder;
use TheCodingMachine\GraphQLite\Types\MyCLabsEnumType;
use function array_filter;
use function array_merge;
use function array_values;
use function assert;
use function is_a;
use function ltrim;
/**
* Maps an class extending MyCLabs enums to a GraphQL type
*/
class MyCLabsEnumTypeMapper implements RootTypeMapperInterface
{
/** @var array<class-string<object>, EnumType> */
private array $cacheByClass = [];
/** @var array<string, EnumType> */
private array $cacheByName = [];
/** @var array<string, class-string<Enum>> */
private array $nameToClassMapping;
public function __construct(
private readonly RootTypeMapperInterface $next,
private readonly AnnotationReader $annotationReader,
private readonly ClassFinder $classFinder,
private readonly ClassFinderComputedCache $classFinderComputedCache,
) {
}
public function toGraphQLOutputType(
Type $type,
OutputType|null $subType,
ReflectionMethod|ReflectionProperty $reflector,
DocBlock $docBlockObj,
): OutputType&\GraphQL\Type\Definition\Type
{
$result = $this->map($type);
return $result ?? $this->next->toGraphQLOutputType($type, $subType, $reflector, $docBlockObj);
}
public function toGraphQLInputType(
Type $type,
InputType|null $subType,
string $argumentName,
ReflectionMethod|ReflectionProperty $reflector,
DocBlock $docBlockObj,
): InputType&\GraphQL\Type\Definition\Type
{
$result = $this->map($type);
return $result ?? $this->next->toGraphQLInputType(
$type,
$subType,
$argumentName,
$reflector,
$docBlockObj,
);
}
private function map(Type $type): EnumType|null
{
if (! $type instanceof Object_) {
return null;
}
$fqsen = $type->getFqsen();
if ($fqsen === null) {
return null;
}
/** @var class-string<object> $enumClass */
$enumClass = (string) $fqsen;
return $this->mapByClassName($enumClass);
}
/** @param class-string<object> $enumClass */
private function mapByClassName(string $enumClass): EnumType|null
{
if (! is_a($enumClass, Enum::class, true)) {
return null;
}
/** @var class-string<Enum> $enumClass */
$enumClass = ltrim($enumClass, '\\');
if (isset($this->cacheByClass[$enumClass])) {
return $this->cacheByClass[$enumClass];
}
$refClass = new ReflectionClass($enumClass);
$type = new MyCLabsEnumType($enumClass, $this->getTypeName($refClass));
return $this->cacheByName[$type->name] = $this->cacheByClass[$enumClass] = $type;
}
private function getTypeName(ReflectionClass $refClass): string
{
$enumType = $this->annotationReader->getEnumTypeAnnotation($refClass);
if ($enumType !== null) {
$name = $enumType->getName();
if ($name !== null) {
return $name;
}
}
return $refClass->getShortName();
}
/**
* Returns a GraphQL type by name.
* If this root type mapper can return this type in "toGraphQLOutputType" or "toGraphQLInputType",
* it should also map these types by name in the "mapNameToType" method.
*
* @param string $typeName The name of the GraphQL type
*/
public function mapNameToType(string $typeName): NamedType&\GraphQL\Type\Definition\Type
{
// This is a hack to make sure "$schema->assertValid()" returns true.
// The mapNameToType will fail if the mapByClassName method was not called before.
// This is actually not an issue in real life scenarios where enum types are never queried
// by type name.
if (isset($this->cacheByName[$typeName])) {
return $this->cacheByName[$typeName];
}
$nameToClassMapping = $this->getNameToClassMapping();
if (isset($this->nameToClassMapping[$typeName])) {
$className = $nameToClassMapping[$typeName];
$type = $this->mapByClassName($className);
assert($type !== null);
return $type;
}
return $this->next->mapNameToType($typeName);
}
/**
* Go through all classes in the defined namespaces and loads the cache.
*
* @return array<string, class-string<Enum>>
*/
private function getNameToClassMapping(): array
{
$this->nameToClassMapping ??= $this->classFinderComputedCache->compute(
$this->classFinder,
'myclabsenum_name_to_class',
function (ReflectionClass $classReflection): array|null {
if (! $classReflection->isSubclassOf(Enum::class)) {
return null;
}
return [$this->getTypeName($classReflection) => $classReflection->getName()];
},
static fn (array $entries) => array_merge(...array_values(array_filter($entries))),
);
return $this->nameToClassMapping;
}
}