|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace TheCodingMachine\GraphQL\Controllers; |
|
5
|
|
|
|
|
6
|
|
|
use function get_parent_class; |
|
7
|
|
|
use GraphQL\Type\Definition\InputObjectType; |
|
8
|
|
|
use GraphQL\Type\Definition\ObjectType; |
|
9
|
|
|
use phpDocumentor\Reflection\Fqsen; |
|
10
|
|
|
use phpDocumentor\Reflection\Types\Object_; |
|
11
|
|
|
use ReflectionClass; |
|
12
|
|
|
use ReflectionMethod; |
|
13
|
|
|
use ReflectionType; |
|
14
|
|
|
use TheCodingMachine\GraphQL\Controllers\Annotations\Type; |
|
15
|
|
|
use TheCodingMachine\GraphQL\Controllers\Hydrators\HydratorInterface; |
|
16
|
|
|
use TheCodingMachine\GraphQL\Controllers\Mappers\RecursiveTypeMapperInterface; |
|
17
|
|
|
use TheCodingMachine\GraphQL\Controllers\Types\ResolvableInputObjectType; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* This class is in charge of creating Webonix InputTypes from Factory annotations. |
|
21
|
|
|
*/ |
|
22
|
|
|
class InputTypeGenerator |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var FieldsBuilderFactory |
|
26
|
|
|
*/ |
|
27
|
|
|
private $fieldsBuilderFactory; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var array<string, InputObjectType> |
|
30
|
|
|
*/ |
|
31
|
|
|
private $cache = []; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var HydratorInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
private $hydrator; |
|
36
|
|
|
/** |
|
37
|
|
|
* @var InputTypeUtils |
|
38
|
|
|
*/ |
|
39
|
|
|
private $inputTypeUtils; |
|
40
|
|
|
|
|
41
|
|
|
public function __construct(InputTypeUtils $inputTypeUtils, |
|
42
|
|
|
FieldsBuilderFactory $fieldsBuilderFactory, |
|
43
|
|
|
HydratorInterface $hydrator) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->inputTypeUtils = $inputTypeUtils; |
|
46
|
|
|
$this->fieldsBuilderFactory = $fieldsBuilderFactory; |
|
47
|
|
|
$this->hydrator = $hydrator; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param object $factory |
|
52
|
|
|
* @param string $methodName |
|
53
|
|
|
* @param RecursiveTypeMapperInterface $recursiveTypeMapper |
|
54
|
|
|
* @return InputObjectType |
|
55
|
|
|
*/ |
|
56
|
|
|
public function mapFactoryMethod($factory, string $methodName, RecursiveTypeMapperInterface $recursiveTypeMapper): InputObjectType |
|
57
|
|
|
{ |
|
58
|
|
|
$method = new ReflectionMethod($factory, $methodName); |
|
59
|
|
|
|
|
60
|
|
|
[$inputName, $className] = $this->inputTypeUtils->getInputTypeNameAndClassName($method); |
|
61
|
|
|
|
|
62
|
|
|
if (!isset($this->cache[$inputName])) { |
|
63
|
|
|
// TODO: add comment argument. |
|
64
|
|
|
$this->cache[$inputName] = new ResolvableInputObjectType($inputName, $this->fieldsBuilderFactory, $recursiveTypeMapper, $factory, $methodName, $this->hydrator, null); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $this->cache[$inputName]; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|