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