|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Date: 04.12.15 |
|
4
|
|
|
* |
|
5
|
|
|
* @author Portey Vasil <[email protected]> |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Youshido\GraphQL\Introspection\Traits; |
|
9
|
|
|
|
|
10
|
|
|
use Youshido\GraphQL\Type\AbstractType; |
|
11
|
|
|
use Youshido\GraphQL\Type\InterfaceType\AbstractInterfaceType; |
|
12
|
|
|
use Youshido\GraphQL\Type\Object\AbstractObjectType; |
|
13
|
|
|
use Youshido\GraphQL\Type\TypeMap; |
|
14
|
|
|
use Youshido\GraphQL\Type\Union\AbstractUnionType; |
|
15
|
|
|
|
|
16
|
|
|
trait TypeCollectorTrait |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
protected $types = []; |
|
20
|
|
|
|
|
21
|
9 |
|
protected function collectTypes(AbstractType $type) |
|
22
|
|
|
{ |
|
23
|
9 |
|
if (is_object($type) && array_key_exists($type->getName(), $this->types)) return; |
|
24
|
|
|
|
|
25
|
9 |
|
switch ($type->getKind()) { |
|
26
|
9 |
|
case TypeMap::KIND_INTERFACE: |
|
27
|
9 |
|
case TypeMap::KIND_UNION: |
|
28
|
9 |
|
case TypeMap::KIND_ENUM: |
|
29
|
9 |
|
case TypeMap::KIND_SCALAR: |
|
30
|
9 |
|
$this->insertType($type->getName(), $type); |
|
31
|
|
|
|
|
32
|
9 |
|
if ($type->getKind() == TypeMap::KIND_UNION) { |
|
33
|
|
|
/** @var AbstractUnionType $type */ |
|
34
|
1 |
|
foreach ($type->getTypes() as $subType) { |
|
35
|
1 |
|
$this->collectTypes($subType); |
|
36
|
1 |
|
} |
|
37
|
1 |
|
} |
|
38
|
|
|
|
|
39
|
9 |
|
break; |
|
40
|
|
|
|
|
41
|
9 |
|
case TypeMap::KIND_INPUT_OBJECT: |
|
42
|
9 |
|
case TypeMap::KIND_OBJECT: |
|
43
|
|
|
/** @var AbstractObjectType $namedType */ |
|
44
|
9 |
|
$namedType = $type->getNamedType(); |
|
45
|
9 |
|
$this->checkAndInsertInterfaces($namedType); |
|
46
|
|
|
|
|
47
|
9 |
|
if ($this->insertType($namedType->getName(), $namedType)) { |
|
48
|
9 |
|
$this->collectFieldsArgsTypes($namedType); |
|
49
|
9 |
|
} |
|
50
|
|
|
|
|
51
|
9 |
|
break; |
|
52
|
|
|
|
|
53
|
9 |
|
case TypeMap::KIND_LIST: |
|
54
|
9 |
|
$this->collectTypes($type->getNamedType()); |
|
55
|
9 |
|
break; |
|
56
|
|
|
|
|
57
|
9 |
|
case TypeMap::KIND_NON_NULL: |
|
58
|
9 |
|
$this->collectTypes($type->getNamedType()); |
|
59
|
|
|
|
|
60
|
9 |
|
break; |
|
61
|
9 |
|
} |
|
62
|
9 |
|
} |
|
63
|
|
|
|
|
64
|
9 |
|
private function checkAndInsertInterfaces($type) |
|
65
|
|
|
{ |
|
66
|
9 |
|
foreach ((array)$type->getConfig()->getInterfaces() as $interface) { |
|
67
|
|
|
/** @var AbstractInterfaceType $interface */ |
|
68
|
4 |
|
$this->insertType($interface->getName(), $interface); |
|
69
|
9 |
|
} |
|
70
|
9 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param $type AbstractObjectType |
|
74
|
|
|
*/ |
|
75
|
9 |
|
private function collectFieldsArgsTypes($type) |
|
76
|
|
|
{ |
|
77
|
9 |
|
foreach ($type->getConfig()->getFields() as $field) { |
|
78
|
9 |
|
$arguments = $field->getConfig()->getArguments(); |
|
79
|
|
|
|
|
80
|
9 |
|
if (is_array($arguments)) { |
|
81
|
9 |
|
foreach ($arguments as $argument) { |
|
82
|
9 |
|
$this->collectTypes($argument->getType()); |
|
83
|
9 |
|
} |
|
84
|
9 |
|
} |
|
85
|
|
|
|
|
86
|
9 |
|
$this->collectTypes($field->getType()); |
|
87
|
9 |
|
} |
|
88
|
9 |
|
} |
|
89
|
|
|
|
|
90
|
9 |
|
private function insertType($name, $type) |
|
91
|
|
|
{ |
|
92
|
9 |
|
if (!array_key_exists($name, $this->types)) { |
|
93
|
9 |
|
$this->types[$name] = $type; |
|
94
|
|
|
|
|
95
|
9 |
|
return true; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
2 |
|
return false; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|