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\DependencyInjection; |
13
|
|
|
|
14
|
|
|
use Overblog\GraphQLBundle\Config\TypeWithOutputFieldsDefinition; |
15
|
|
|
use Symfony\Component\Config\FileLocator; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
17
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
18
|
|
|
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; |
19
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
20
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
21
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
22
|
|
|
|
23
|
|
|
class OverblogGraphQLExtension extends Extension implements PrependExtensionInterface |
24
|
|
|
{ |
25
|
13 |
|
public function load(array $configs, ContainerBuilder $container) |
26
|
|
|
{ |
27
|
13 |
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
28
|
13 |
|
$loader->load('services.yml'); |
29
|
13 |
|
$loader->load('graphql_types.yml'); |
30
|
13 |
|
$loader->load('graphql_resolvers.yml'); |
31
|
|
|
|
32
|
13 |
|
$config = $this->treatConfigs($configs, $container); |
33
|
|
|
|
34
|
13 |
|
$this->setServicesAliases($config, $container); |
|
|
|
|
35
|
13 |
|
$this->setSchemaBuilderArguments($config, $container); |
|
|
|
|
36
|
13 |
|
$this->setSchemaArguments($config, $container); |
|
|
|
|
37
|
13 |
|
$this->setErrorHandlerArguments($config, $container); |
|
|
|
|
38
|
13 |
|
$this->setGraphiQLTemplate($config, $container); |
|
|
|
|
39
|
13 |
|
$this->setSecurity($config, $container); |
|
|
|
|
40
|
13 |
|
$this->setConfigBuilders($config); |
|
|
|
|
41
|
13 |
|
$this->setVersions($config, $container); |
|
|
|
|
42
|
13 |
|
$this->setShowDebug($config, $container); |
|
|
|
|
43
|
|
|
|
44
|
13 |
|
$container->setParameter($this->getAlias().'.resources_dir', realpath(__DIR__.'/../Resources')); |
45
|
13 |
|
} |
46
|
|
|
|
47
|
11 |
|
public function prepend(ContainerBuilder $container) |
48
|
|
|
{ |
49
|
11 |
|
$configs = $container->getExtensionConfig($this->getAlias()); |
50
|
11 |
|
$configs = $container->getParameterBag()->resolveValue($configs); |
51
|
11 |
|
$config = $this->treatConfigs($configs, $container, true); |
52
|
|
|
|
53
|
|
|
/** @var OverblogGraphQLTypesExtension $typesExtension */ |
54
|
11 |
|
$typesExtension = $container->getExtension($this->getAlias().'_types'); |
55
|
11 |
|
$typesExtension->containerPrependExtensionConfig($config, $container); |
|
|
|
|
56
|
11 |
|
} |
57
|
|
|
|
58
|
13 |
|
private function setShowDebug(array $config, ContainerBuilder $container) |
59
|
|
|
{ |
60
|
13 |
|
$container->getDefinition($this->getAlias().'.request_executor')->replaceArgument(4, $config['definitions']['show_debug_info']); |
61
|
13 |
|
} |
62
|
|
|
|
63
|
13 |
|
private function setVersions(array $config, ContainerBuilder $container) |
64
|
|
|
{ |
65
|
13 |
|
$container->setParameter($this->getAlias().'.versions.graphiql', $config['versions']['graphiql']); |
66
|
13 |
|
$container->setParameter($this->getAlias().'.versions.react', $config['versions']['react']); |
67
|
13 |
|
$container->setParameter($this->getAlias().'.versions.fetch', $config['versions']['fetch']); |
68
|
13 |
|
} |
69
|
|
|
|
70
|
13 |
|
private function setConfigBuilders(array $config) |
71
|
|
|
{ |
72
|
13 |
|
foreach (['args', 'field'] as $category) { |
73
|
13 |
|
if (!empty($config['definitions']['builders'][$category])) { |
74
|
1 |
|
$method = 'add'.ucfirst($category).'BuilderClass'; |
75
|
|
|
|
76
|
1 |
|
foreach ($config['definitions']['builders'][$category] as $params) { |
77
|
1 |
|
TypeWithOutputFieldsDefinition::$method($params['alias'], $params['class']); |
78
|
1 |
|
} |
79
|
1 |
|
} |
80
|
13 |
|
} |
81
|
13 |
|
} |
82
|
|
|
|
83
|
13 |
|
private function treatConfigs(array $configs, ContainerBuilder $container, $forceReload = false) |
84
|
|
|
{ |
85
|
13 |
|
static $config = null; |
86
|
|
|
|
87
|
13 |
|
if ($forceReload || null === $config) { |
88
|
13 |
|
$configuration = $this->getConfiguration($configs, $container); |
89
|
13 |
|
$config = $this->processConfiguration($configuration, $configs); |
|
|
|
|
90
|
13 |
|
} |
91
|
|
|
|
92
|
13 |
|
return $config; |
93
|
|
|
} |
94
|
|
|
|
95
|
13 |
|
private function setSecurity(array $config, ContainerBuilder $container) |
96
|
|
|
{ |
97
|
13 |
|
$container->setParameter($this->getAlias().'.query_max_depth', $config['security']['query_max_depth']); |
98
|
13 |
|
$container->setParameter($this->getAlias().'.query_max_complexity', $config['security']['query_max_complexity']); |
99
|
13 |
|
} |
100
|
|
|
|
101
|
13 |
|
private function setGraphiQLTemplate(array $config, ContainerBuilder $container) |
102
|
|
|
{ |
103
|
13 |
|
$container->setParameter($this->getAlias().'.graphiql_template', $config['templates']['graphiql']); |
104
|
13 |
|
} |
105
|
|
|
|
106
|
13 |
|
private function setErrorHandlerArguments(array $config, ContainerBuilder $container) |
107
|
|
|
{ |
108
|
13 |
|
$errorHandlerDefinition = $container->getDefinition($this->getAlias().'.error_handler'); |
109
|
|
|
|
110
|
13 |
|
if (isset($config['definitions']['internal_error_message'])) { |
111
|
1 |
|
$errorHandlerDefinition->replaceArgument(0, $config['definitions']['internal_error_message']); |
112
|
1 |
|
} |
113
|
|
|
|
114
|
13 |
|
if (isset($config['definitions']['exceptions'])) { |
115
|
|
|
$errorHandlerDefinition |
116
|
13 |
|
->replaceArgument(2, $this->buildExceptionMap($config['definitions']['exceptions'])) |
117
|
13 |
|
->addMethodCall('setUserWarningClass', [$config['definitions']['exceptions']['types']['warnings']]) |
118
|
13 |
|
->addMethodCall('setUserErrorClass', [$config['definitions']['exceptions']['types']['errors']]) |
119
|
|
|
; |
120
|
13 |
|
} |
121
|
13 |
|
} |
122
|
|
|
|
123
|
13 |
|
private function setSchemaBuilderArguments(array $config, ContainerBuilder $container) |
124
|
|
|
{ |
125
|
13 |
|
$container->getDefinition($this->getAlias().'.schema_builder') |
126
|
13 |
|
->replaceArgument(1, $config['definitions']['config_validation']); |
127
|
13 |
|
} |
128
|
|
|
|
129
|
13 |
|
private function setSchemaArguments(array $config, ContainerBuilder $container) |
130
|
|
|
{ |
131
|
13 |
|
if (isset($config['definitions']['schema'])) { |
132
|
13 |
|
$executorDefinition = $container->getDefinition($this->getAlias().'.request_executor'); |
133
|
|
|
|
134
|
13 |
|
foreach ($config['definitions']['schema'] as $schemaName => $schemaConfig) { |
135
|
10 |
|
$schemaID = sprintf('%s.schema_%s', $this->getAlias(), $schemaName); |
136
|
10 |
|
$definition = new Definition('GraphQL\Schema'); |
137
|
10 |
|
$definition->setFactory([new Reference('overblog_graphql.schema_builder'), 'create']); |
138
|
10 |
|
$definition->setArguments([$schemaConfig['query'], $schemaConfig['mutation'], $schemaConfig['subscription']]); |
139
|
10 |
|
$definition->setPublic(false); |
140
|
10 |
|
$container->setDefinition($schemaID, $definition); |
141
|
|
|
|
142
|
10 |
|
$executorDefinition->addMethodCall('addSchema', [$schemaName, new Reference($schemaID)]); |
143
|
13 |
|
} |
144
|
13 |
|
} |
145
|
13 |
|
} |
146
|
|
|
|
147
|
13 |
|
private function setServicesAliases(array $config, ContainerBuilder $container) |
148
|
|
|
{ |
149
|
13 |
|
if (isset($config['services'])) { |
150
|
13 |
|
foreach ($config['services'] as $name => $id) { |
151
|
13 |
|
$alias = sprintf('%s.%s', $this->getAlias(), $name); |
152
|
13 |
|
$container->setAlias($alias, $id); |
153
|
13 |
|
} |
154
|
13 |
|
} |
155
|
13 |
|
} |
156
|
|
|
|
157
|
13 |
|
public function getAlias() |
158
|
|
|
{ |
159
|
13 |
|
return 'overblog_graphql'; |
160
|
|
|
} |
161
|
|
|
|
162
|
13 |
|
public function getConfiguration(array $config, ContainerBuilder $container) |
163
|
|
|
{ |
164
|
13 |
|
return new Configuration($container->getParameter('kernel.debug')); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Returns a list of custom exceptions mapped to error/warning classes. |
169
|
|
|
* |
170
|
|
|
* @param array $exceptionConfig |
171
|
|
|
* |
172
|
|
|
* @return array Custom exception map, [exception => UserError/UserWarning] |
173
|
|
|
*/ |
174
|
13 |
|
private function buildExceptionMap(array $exceptionConfig) |
175
|
|
|
{ |
176
|
13 |
|
$exceptionMap = []; |
177
|
13 |
|
$typeMap = $exceptionConfig['types']; |
178
|
|
|
|
179
|
13 |
|
foreach ($exceptionConfig as $type => $exceptionList) { |
180
|
13 |
|
if ('types' === $type) { |
181
|
13 |
|
continue; |
182
|
|
|
} |
183
|
|
|
|
184
|
13 |
|
foreach ($exceptionList as $exception) { |
185
|
2 |
|
$exceptionMap[$exception] = $typeMap[$type]; |
186
|
13 |
|
} |
187
|
13 |
|
} |
188
|
|
|
|
189
|
13 |
|
return $exceptionMap; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.