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