1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\GraphQL\Controllers; |
5
|
|
|
|
6
|
|
|
use function get_parent_class; |
7
|
|
|
use GraphQL\Type\Definition\ObjectType; |
8
|
|
|
use ReflectionClass; |
9
|
|
|
use TheCodingMachine\GraphQL\Controllers\Annotations\Type; |
10
|
|
|
use TheCodingMachine\GraphQL\Controllers\Mappers\RecursiveTypeMapperInterface; |
11
|
|
|
use TheCodingMachine\GraphQL\Controllers\Types\MutableObjectType; |
12
|
|
|
use TheCodingMachine\GraphQL\Controllers\Types\TypeAnnotatedObjectType; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* This class is in charge of creating Webonix GraphQL types from annotated objects that do not extend the |
16
|
|
|
* Webonix ObjectType class. |
17
|
|
|
*/ |
18
|
|
|
class TypeGenerator |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var AnnotationReader |
22
|
|
|
*/ |
23
|
|
|
private $annotationReader; |
24
|
|
|
/** |
25
|
|
|
* @var FieldsBuilderFactory |
26
|
|
|
*/ |
27
|
|
|
private $fieldsBuilderFactory; |
28
|
|
|
/** |
29
|
|
|
* @var NamingStrategyInterface |
30
|
|
|
*/ |
31
|
|
|
private $namingStrategy; |
32
|
|
|
/** |
33
|
|
|
* @var TypeRegistry |
34
|
|
|
*/ |
35
|
|
|
private $typeRegistry; |
36
|
|
|
|
37
|
|
|
public function __construct(AnnotationReader $annotationReader, |
38
|
|
|
FieldsBuilderFactory $fieldsBuilderFactory, |
39
|
|
|
NamingStrategyInterface $namingStrategy, |
40
|
|
|
TypeRegistry $typeRegistry) |
41
|
|
|
{ |
42
|
|
|
$this->annotationReader = $annotationReader; |
43
|
|
|
$this->fieldsBuilderFactory = $fieldsBuilderFactory; |
44
|
|
|
$this->namingStrategy = $namingStrategy; |
45
|
|
|
$this->typeRegistry = $typeRegistry; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param object $annotatedObject An object with a Type annotation. |
50
|
|
|
* @param RecursiveTypeMapperInterface $recursiveTypeMapper |
51
|
|
|
* @return MutableObjectType |
52
|
|
|
* @throws \ReflectionException |
53
|
|
|
*/ |
54
|
|
|
public function mapAnnotatedObject($annotatedObject, RecursiveTypeMapperInterface $recursiveTypeMapper): MutableObjectType |
55
|
|
|
{ |
56
|
|
|
$refTypeClass = new \ReflectionClass($annotatedObject); |
57
|
|
|
|
58
|
|
|
$typeField = $this->annotationReader->getTypeAnnotation($refTypeClass); |
59
|
|
|
|
60
|
|
|
if ($typeField === null) { |
61
|
|
|
throw MissingAnnotationException::missingTypeException(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$typeName = $this->namingStrategy->getOutputTypeName($refTypeClass->getName(), $typeField); |
65
|
|
|
|
66
|
|
|
if ($this->typeRegistry->hasType($typeName)) { |
67
|
|
|
return $this->typeRegistry->getMutableObjectType($typeName); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return TypeAnnotatedObjectType::createFromAnnotatedClass($typeName, $typeField->getClass(), $annotatedObject, $this->fieldsBuilderFactory, $recursiveTypeMapper); |
71
|
|
|
|
72
|
|
|
/*return new ObjectType([ |
73
|
|
|
'name' => $typeName, |
74
|
|
|
'fields' => function() use ($annotatedObject, $recursiveTypeMapper, $typeField) { |
75
|
|
|
$parentClass = get_parent_class($typeField->getClass()); |
76
|
|
|
$parentType = null; |
77
|
|
|
if ($parentClass !== false) { |
78
|
|
|
if ($recursiveTypeMapper->canMapClassToType($parentClass)) { |
79
|
|
|
$parentType = $recursiveTypeMapper->mapClassToType($parentClass, null); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$fieldProvider = $this->controllerQueryProviderFactory->buildFieldsBuilder($recursiveTypeMapper); |
84
|
|
|
$fields = $fieldProvider->getFields($annotatedObject); |
85
|
|
|
if ($parentType !== null) { |
86
|
|
|
$fields = $parentType->getFields() + $fields; |
87
|
|
|
} |
88
|
|
|
return $fields; |
89
|
|
|
}, |
90
|
|
|
'interfaces' => function() use ($typeField, $recursiveTypeMapper) { |
91
|
|
|
return $recursiveTypeMapper->findInterfaces($typeField->getClass()); |
92
|
|
|
} |
93
|
|
|
]);*/ |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param object $annotatedObject An object with a ExtendType annotation. |
98
|
|
|
* @param MutableObjectType $type |
99
|
|
|
* @param RecursiveTypeMapperInterface $recursiveTypeMapper |
100
|
|
|
*/ |
101
|
|
|
public function extendAnnotatedObject($annotatedObject, MutableObjectType $type, RecursiveTypeMapperInterface $recursiveTypeMapper) |
102
|
|
|
{ |
103
|
|
|
$refTypeClass = new \ReflectionClass($annotatedObject); |
104
|
|
|
|
105
|
|
|
$extendTypeAnnotation = $this->annotationReader->getExtendTypeAnnotation($refTypeClass); |
106
|
|
|
|
107
|
|
|
if ($extendTypeAnnotation === null) { |
108
|
|
|
throw MissingAnnotationException::missingExtendTypeException(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
//$typeName = $this->namingStrategy->getOutputTypeName($refTypeClass->getName(), $extendTypeAnnotation); |
112
|
|
|
$typeName = $type->name; |
|
|
|
|
113
|
|
|
|
114
|
|
|
/*if ($this->typeRegistry->hasType($typeName)) { |
115
|
|
|
throw new GraphQLException(sprintf('Tried to extend GraphQL type "%s" that is already stored in the type registry.', $typeName)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if (!$type instanceof MutableObjectType) { |
119
|
|
|
throw new \RuntimeException('TEMP EXCEPTION'); |
120
|
|
|
}*/ |
121
|
|
|
|
122
|
|
|
$type->addFields(function() use ($annotatedObject, $recursiveTypeMapper) { |
123
|
|
|
/*$parentClass = get_parent_class($extendTypeAnnotation->getClass()); |
124
|
|
|
$parentType = null; |
125
|
|
|
if ($parentClass !== false) { |
126
|
|
|
if ($recursiveTypeMapper->canMapClassToType($parentClass)) { |
127
|
|
|
$parentType = $recursiveTypeMapper->mapClassToType($parentClass, null); |
128
|
|
|
} |
129
|
|
|
}*/ |
130
|
|
|
|
131
|
|
|
$fieldProvider = $this->fieldsBuilderFactory->buildFieldsBuilder($recursiveTypeMapper); |
132
|
|
|
return $fieldProvider->getFields($annotatedObject); |
133
|
|
|
/*if ($parentType !== null) { |
134
|
|
|
$fields = $parentType->getFields() + $fields; |
135
|
|
|
}*/ |
136
|
|
|
}); |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
// return new ObjectType([ |
140
|
|
|
// 'name' => $typeName, |
141
|
|
|
// 'fields' => function() use ($annotatedObject, $recursiveTypeMapper, $type) { |
142
|
|
|
// /*$parentClass = get_parent_class($extendTypeAnnotation->getClass()); |
143
|
|
|
// $parentType = null; |
144
|
|
|
// if ($parentClass !== false) { |
145
|
|
|
// if ($recursiveTypeMapper->canMapClassToType($parentClass)) { |
146
|
|
|
// $parentType = $recursiveTypeMapper->mapClassToType($parentClass, null); |
147
|
|
|
// } |
148
|
|
|
// }*/ |
149
|
|
|
// |
150
|
|
|
// $fieldProvider = $this->fieldsBuilderFactory->buildFieldsBuilder($recursiveTypeMapper); |
151
|
|
|
// $fields = $fieldProvider->getFields($annotatedObject); |
152
|
|
|
// /*if ($parentType !== null) { |
153
|
|
|
// $fields = $parentType->getFields() + $fields; |
154
|
|
|
// }*/ |
155
|
|
|
// |
156
|
|
|
// $fields = $type->getFields() + $fields; |
157
|
|
|
// |
158
|
|
|
// return $fields; |
159
|
|
|
// }, |
160
|
|
|
// 'interfaces' => function() use ($type) { |
161
|
|
|
// return $type->getInterfaces(); |
162
|
|
|
// } |
163
|
|
|
// ]); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|