1
|
|
|
<?php |
2
|
|
|
namespace Arthem\GraphQLMapper\Mapping\Driver; |
3
|
|
|
|
4
|
|
|
use Arthem\GraphQLMapper\Mapping\AbstractType; |
5
|
|
|
use Arthem\GraphQLMapper\Mapping\Field; |
6
|
|
|
use Arthem\GraphQLMapper\Mapping\FieldContainer; |
7
|
|
|
use Arthem\GraphQLMapper\Mapping\InterfaceType; |
8
|
|
|
use Arthem\GraphQLMapper\Mapping\Query; |
9
|
|
|
use Arthem\GraphQLMapper\Mapping\SchemaContainer; |
10
|
|
|
use Arthem\GraphQLMapper\Mapping\Type; |
11
|
|
|
use Symfony\Component\Yaml\Yaml; |
12
|
|
|
|
13
|
|
|
class YamlDriver extends FileDriver |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* {@inheritdoc} |
17
|
|
|
*/ |
18
|
|
|
public function load(SchemaContainer $schema) |
19
|
|
|
{ |
20
|
|
|
$paths = $this->getPaths(); |
21
|
|
|
|
22
|
|
|
foreach ($paths as $path) { |
23
|
|
|
$this->loadFile($path, $schema); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $path |
29
|
|
|
* @param SchemaContainer $schemaContainer |
30
|
|
|
*/ |
31
|
|
|
private function loadFile($path, SchemaContainer $schemaContainer) |
32
|
|
|
{ |
33
|
|
|
$config = Yaml::parse($this->getFileContent($path)); |
34
|
|
|
|
35
|
|
|
if (!is_array($config)) { |
36
|
|
|
return; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** @var array $config */ |
40
|
|
|
foreach ($config as $type => $mapping) { |
41
|
|
|
switch ($type) { |
42
|
|
|
case 'query': |
43
|
|
|
$this->mapQuery($schemaContainer, $mapping); |
44
|
|
|
break; |
45
|
|
|
case 'mutation': |
46
|
|
|
$this->mapMutation($schemaContainer, $mapping); |
47
|
|
|
break; |
48
|
|
|
case 'types': |
49
|
|
|
$this->mapTypes($schemaContainer, $mapping); |
50
|
|
|
break; |
51
|
|
|
case 'interfaces': |
52
|
|
|
$this->mapInterfaces($schemaContainer, $mapping); |
53
|
|
|
break; |
54
|
|
|
default: |
55
|
|
|
throw new \UnexpectedValueException(sprintf('Unsupported key "%s"', $type)); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param SchemaContainer $schemaContainer |
62
|
|
|
* @param array $mapping |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
private function mapQuery(SchemaContainer $schemaContainer, array $mapping) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$querySchema = $schemaContainer->getQuerySchema(); |
67
|
|
|
if (null === $querySchema) { |
68
|
|
|
$querySchema = new Query(); |
69
|
|
|
$schemaContainer->setQuerySchema($querySchema); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->populateFieldContainer($querySchema, $mapping); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param SchemaContainer $schemaContainer |
77
|
|
|
* @param array $mapping |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
private function mapMutation(SchemaContainer $schemaContainer, array $mapping) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$mutationSchema = $schemaContainer->getMutationSchema(); |
82
|
|
|
if (null === $mutationSchema) { |
83
|
|
|
$mutationSchema = new Query(); |
84
|
|
|
$schemaContainer->setMutationSchema($mutationSchema); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->populateFieldContainer($mutationSchema, $mapping); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param SchemaContainer $schemaContainer |
92
|
|
|
* @param array $mapping |
93
|
|
|
*/ |
94
|
|
|
private function mapTypes(SchemaContainer $schemaContainer, array $mapping) |
95
|
|
|
{ |
96
|
|
|
foreach ($mapping as $name => $typeMapping) { |
97
|
|
|
$type = $this->createType($name, $typeMapping); |
98
|
|
|
$schemaContainer->addType($type); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param SchemaContainer $schemaContainer |
104
|
|
|
* @param array $mapping |
105
|
|
|
*/ |
106
|
|
|
private function mapInterfaces(SchemaContainer $schemaContainer, array $mapping) |
107
|
|
|
{ |
108
|
|
|
foreach ($mapping as $name => $interfaceMapping) { |
109
|
|
|
$interface = $this->createInterface($name, $interfaceMapping); |
110
|
|
|
$schemaContainer->addInterface($interface); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $name |
116
|
|
|
* @param array $mapping |
117
|
|
|
* @return Type |
118
|
|
|
*/ |
119
|
|
|
private function createType($name, array $mapping) |
120
|
|
|
{ |
121
|
|
|
$type = new Type(); |
122
|
|
|
$type |
123
|
|
|
->setName($name) |
124
|
|
|
->setExtends(isset($mapping['extends']) ? $mapping['extends'] : null) |
125
|
|
|
->setResolveConfig(isset($mapping['resolve']) ? $mapping['resolve'] : []); |
126
|
|
|
|
127
|
|
|
if (isset($mapping['values'])) { |
128
|
|
|
$type->setValues($mapping['values']); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if (isset($mapping['model'])) { |
132
|
|
|
$type->setModel($mapping['model']); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$this->populateFieldContainer($type, $mapping); |
136
|
|
|
|
137
|
|
|
return $type; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param AbstractType $type |
142
|
|
|
* @param array $mapping |
143
|
|
|
*/ |
144
|
|
|
private function populateType(AbstractType $type, array $mapping) |
145
|
|
|
{ |
146
|
|
|
if (isset($mapping['description'])) { |
147
|
|
|
$type->setDescription($mapping['description']); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param FieldContainer $type |
153
|
|
|
* @param array $mapping |
154
|
|
|
*/ |
155
|
|
|
private function populateFieldContainer(FieldContainer $type, array $mapping) |
156
|
|
|
{ |
157
|
|
|
$this->populateType($type, $mapping); |
158
|
|
|
|
159
|
|
|
if (!isset($mapping['fields'])) { |
160
|
|
|
return; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$fields = []; |
164
|
|
|
foreach ($mapping['fields'] as $name => $fieldMapping) { |
165
|
|
|
$fields[] = $this->createField($name, $fieldMapping); |
166
|
|
|
} |
167
|
|
|
$type->setFields($fields); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string $name |
172
|
|
|
* @param array $mapping |
173
|
|
|
* @return InterfaceType |
174
|
|
|
*/ |
175
|
|
|
private function createInterface($name, array $mapping) |
176
|
|
|
{ |
177
|
|
|
$interface = new InterfaceType(); |
178
|
|
|
$interface->setName($name); |
179
|
|
|
$this->populateFieldContainer($interface, $mapping); |
180
|
|
|
|
181
|
|
|
return $interface; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param string $name |
186
|
|
|
* @param array $mapping |
187
|
|
|
* @return Field |
188
|
|
|
*/ |
189
|
|
|
private function createField($name, array $mapping) |
190
|
|
|
{ |
191
|
|
|
$field = new Field(); |
192
|
|
|
$field |
193
|
|
|
->setName($name) |
194
|
|
|
->setType(isset($mapping['type']) ? $mapping['type'] : null) |
195
|
|
|
->setProperty(isset($mapping['property']) ? $mapping['property'] : null) |
196
|
|
|
->setResolveConfig(isset($mapping['resolve']) ? $mapping['resolve'] : []); |
197
|
|
|
|
198
|
|
|
$this->populateType($field, $mapping); |
199
|
|
|
|
200
|
|
|
if (isset($mapping['args'])) { |
201
|
|
|
$args = []; |
202
|
|
|
foreach ($mapping['args'] as $argName => $argMapping) { |
203
|
|
|
$args[] = $this->createField($argName, $argMapping); |
204
|
|
|
} |
205
|
|
|
$field->setArguments($args); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $field; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.