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