1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overblog\GraphQLBundle\Config; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
15
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
16
|
|
|
|
17
|
|
|
class ObjectTypeDefinition extends TypeWithOutputFieldsDefinition |
18
|
|
|
{ |
19
|
20 |
|
public function getDefinition() |
20
|
|
|
{ |
21
|
20 |
|
$builder = new TreeBuilder(); |
22
|
20 |
|
$node = $builder->root('_object_config'); |
23
|
|
|
|
24
|
|
|
$node |
|
|
|
|
25
|
20 |
|
->children() |
26
|
20 |
|
->append($this->nameSection()) |
27
|
20 |
|
->append($this->outputFieldsSelection('fields')) |
28
|
20 |
|
->append($this->descriptionSection()) |
29
|
20 |
|
->arrayNode('interfaces') |
30
|
20 |
|
->prototype('scalar')->info('One of internal or custom interface types.')->end() |
31
|
20 |
|
->end() |
32
|
20 |
|
->variableNode('isTypeOf')->end() |
33
|
20 |
|
->variableNode('resolveField')->end() |
34
|
20 |
|
->variableNode('fieldsDefaultAccess') |
35
|
20 |
|
->info('Default access control to fields (expression language can be use here)') |
36
|
20 |
|
->end() |
37
|
20 |
|
->variableNode('fieldsDefaultPublic') |
38
|
20 |
|
->info('Default public control to fields (expression language can be use here)') |
39
|
20 |
|
->end() |
40
|
20 |
|
->end(); |
41
|
|
|
|
42
|
20 |
|
$this->treatFieldsDefaultAccess($node); |
43
|
20 |
|
$this->treatFieldsDefaultPublic($node); |
44
|
20 |
|
$this->treatResolveField($node); |
45
|
|
|
|
46
|
20 |
|
return $node; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* set empty fields.access with fieldsDefaultAccess values if is set? |
51
|
|
|
* |
52
|
|
|
* @param ArrayNodeDefinition $node |
53
|
|
|
*/ |
54
|
20 |
View Code Duplication |
private function treatFieldsDefaultAccess(ArrayNodeDefinition $node) |
|
|
|
|
55
|
|
|
{ |
56
|
20 |
|
$node->validate() |
57
|
|
|
->ifTrue(function ($v) { |
58
|
14 |
|
return array_key_exists('fieldsDefaultAccess', $v) && null !== $v['fieldsDefaultAccess']; |
59
|
20 |
|
}) |
60
|
|
|
->then(function ($v) { |
61
|
1 |
|
foreach ($v['fields'] as &$field) { |
62
|
1 |
|
if (array_key_exists('access', $field) && null !== $field['access']) { |
63
|
1 |
|
continue; |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
$field['access'] = $v['fieldsDefaultAccess']; |
67
|
1 |
|
} |
68
|
|
|
|
69
|
1 |
|
return $v; |
70
|
20 |
|
}) |
71
|
20 |
|
->end(); |
72
|
20 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* set empty fields.public with fieldsDefaultPublic values if is set? |
76
|
|
|
* |
77
|
|
|
* @param ArrayNodeDefinition $node |
78
|
|
|
*/ |
79
|
20 |
View Code Duplication |
private function treatFieldsDefaultPublic(ArrayNodeDefinition $node) |
|
|
|
|
80
|
|
|
{ |
81
|
20 |
|
$node->validate() |
82
|
|
|
->ifTrue(function ($v) { |
83
|
14 |
|
return array_key_exists('fieldsDefaultPublic', $v) && null !== $v['fieldsDefaultPublic']; |
84
|
20 |
|
}) |
85
|
|
|
->then(function ($v) { |
86
|
|
|
foreach ($v['fields'] as &$field) { |
87
|
|
|
if (array_key_exists('public', $field) && null !== $field['public']) { |
88
|
|
|
continue; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$field['public'] = $v['fieldsDefaultPublic']; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $v; |
95
|
20 |
|
}) |
96
|
20 |
|
->end(); |
97
|
20 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* resolveField is set as fields default resolver if not set |
101
|
|
|
* then remove resolveField to keep "access" feature |
102
|
|
|
* TODO(mcg-web) : get a cleaner way to use resolveField combine with "access" feature. |
103
|
|
|
* |
104
|
|
|
* @param ArrayNodeDefinition $node |
105
|
|
|
*/ |
106
|
20 |
|
private function treatResolveField(ArrayNodeDefinition $node) |
107
|
|
|
{ |
108
|
20 |
|
$node->validate() |
109
|
|
|
->ifTrue(function ($v) { |
110
|
14 |
|
return array_key_exists('resolveField', $v) && null !== $v['resolveField']; |
111
|
20 |
|
}) |
112
|
20 |
|
->then(function ($v) { |
113
|
1 |
|
$resolveField = $v['resolveField']; |
114
|
1 |
|
unset($v['resolveField']); |
115
|
1 |
|
foreach ($v['fields'] as &$field) { |
116
|
1 |
|
if (!empty($field['resolve'])) { |
117
|
|
|
continue; |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
$field['resolve'] = $resolveField; |
121
|
1 |
|
} |
122
|
|
|
|
123
|
1 |
|
return $v; |
124
|
20 |
|
}) |
125
|
20 |
|
->end(); |
126
|
20 |
|
} |
127
|
|
|
} |
128
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.