1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\DependencyInjection; |
6
|
|
|
|
7
|
|
|
use GraphQL\Error\UserError; |
8
|
|
|
use GraphQL\Type\Definition\Type; |
9
|
|
|
use GraphQL\Type\Schema; |
10
|
|
|
use Overblog\GraphQLBundle\CacheWarmer\CompileCacheWarmer; |
11
|
|
|
use Overblog\GraphQLBundle\Config\Processor\BuilderProcessor; |
12
|
|
|
use Overblog\GraphQLBundle\Definition\Builder\SchemaBuilder; |
13
|
|
|
use Overblog\GraphQLBundle\Definition\Resolver\MutationInterface; |
14
|
|
|
use Overblog\GraphQLBundle\Definition\Resolver\ResolverInterface; |
15
|
|
|
use Overblog\GraphQLBundle\Error\ErrorHandler; |
16
|
|
|
use Overblog\GraphQLBundle\Error\ExceptionConverter; |
17
|
|
|
use Overblog\GraphQLBundle\Error\ExceptionConverterInterface; |
18
|
|
|
use Overblog\GraphQLBundle\Error\UserWarning; |
19
|
|
|
use Overblog\GraphQLBundle\Event\Events; |
20
|
|
|
use Overblog\GraphQLBundle\EventListener\ClassLoaderListener; |
21
|
|
|
use Overblog\GraphQLBundle\EventListener\DebugListener; |
22
|
|
|
use Overblog\GraphQLBundle\EventListener\ErrorHandlerListener; |
23
|
|
|
use Overblog\GraphQLBundle\EventListener\ErrorLoggerListener; |
24
|
|
|
use Overblog\GraphQLBundle\EventListener\TypeDecoratorListener; |
25
|
|
|
use Overblog\GraphQLBundle\Request\Executor; |
26
|
|
|
use Overblog\GraphQLBundle\Validator\ValidatorFactory; |
27
|
|
|
use Symfony\Component\Config\FileLocator; |
28
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
29
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
30
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
31
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
32
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
33
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
34
|
|
|
|
35
|
|
|
class OverblogGraphQLExtension extends Extension |
36
|
|
|
{ |
37
|
42 |
|
public function load(array $configs, ContainerBuilder $container): void |
38
|
|
|
{ |
39
|
42 |
|
$this->loadConfigFiles($container); |
40
|
42 |
|
$configuration = $this->getConfiguration($configs, $container); |
41
|
42 |
|
$config = $this->processConfiguration($configuration, $configs); |
42
|
|
|
|
43
|
42 |
|
$this->setBatchingMethod($config, $container); |
44
|
42 |
|
$this->setServicesAliases($config, $container); |
45
|
42 |
|
$this->setSchemaBuilderArguments($config, $container); |
46
|
42 |
|
$this->setSchemaArguments($config, $container); |
47
|
42 |
|
$this->setErrorHandler($config, $container); |
48
|
42 |
|
$this->setSecurity($config, $container); |
49
|
42 |
|
$this->setConfigBuilders($config, $container); |
50
|
42 |
|
$this->setDebugListener($config, $container); |
51
|
42 |
|
$this->setDefinitionParameters($config, $container); |
52
|
42 |
|
$this->setClassLoaderListener($config, $container); |
53
|
42 |
|
$this->setCompilerCacheWarmer($config, $container); |
54
|
42 |
|
$this->registerForAutoconfiguration($container); |
55
|
42 |
|
$this->setDefaultFieldResolver($config, $container); |
56
|
42 |
|
$this->registerValidatorFactory($container); |
57
|
|
|
|
58
|
42 |
|
$container->setParameter($this->getAlias().'.config', $config); |
59
|
42 |
|
$container->setParameter($this->getAlias().'.resources_dir', \realpath(__DIR__.'/../Resources')); |
60
|
42 |
|
} |
61
|
|
|
|
62
|
42 |
|
public function getAlias() |
63
|
|
|
{ |
64
|
42 |
|
return Configuration::NAME; |
65
|
|
|
} |
66
|
|
|
|
67
|
42 |
|
public function getConfiguration(array $config, ContainerBuilder $container) |
68
|
|
|
{ |
69
|
42 |
|
return new Configuration( |
70
|
42 |
|
$container->getParameter('kernel.debug'), |
71
|
42 |
|
$container->hasParameter('kernel.cache_dir') ? $container->getParameter('kernel.cache_dir') : null |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
42 |
|
private function loadConfigFiles(ContainerBuilder $container): void |
76
|
|
|
{ |
77
|
42 |
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
78
|
42 |
|
$loader->load('services.yaml'); |
79
|
42 |
|
$loader->load('commands.yaml'); |
80
|
42 |
|
$loader->load('listeners.yaml'); |
81
|
42 |
|
$loader->load('graphql_types.yaml'); |
82
|
42 |
|
$loader->load('graphql_resolvers.yaml'); |
83
|
42 |
|
$loader->load('expression_language_functions.yaml'); |
84
|
42 |
|
$loader->load('definition_config_processors.yaml'); |
85
|
42 |
|
$loader->load('aliases.yaml'); |
86
|
42 |
|
} |
87
|
|
|
|
88
|
42 |
|
private function registerForAutoconfiguration(ContainerBuilder $container): void |
89
|
|
|
{ |
90
|
42 |
|
$container->registerForAutoconfiguration(MutationInterface::class) |
91
|
42 |
|
->addTag('overblog_graphql.mutation'); |
92
|
42 |
|
$container->registerForAutoconfiguration(ResolverInterface::class) |
93
|
42 |
|
->addTag('overblog_graphql.resolver'); |
94
|
42 |
|
$container->registerForAutoconfiguration(Type::class) |
95
|
42 |
|
->addTag('overblog_graphql.type'); |
96
|
42 |
|
} |
97
|
|
|
|
98
|
42 |
|
private function registerValidatorFactory(ContainerBuilder $container): void |
99
|
|
|
{ |
100
|
42 |
|
if (\class_exists('Symfony\\Component\\Validator\\Validation')) { |
101
|
42 |
|
$container->register(ValidatorFactory::class) |
102
|
42 |
|
->setArguments([ |
103
|
42 |
|
new Reference('validator.validator_factory'), |
104
|
42 |
|
new Reference('translator.default', $container::NULL_ON_INVALID_REFERENCE), |
105
|
|
|
]) |
106
|
42 |
|
->addTag( |
107
|
42 |
|
'overblog_graphql.global_variable', |
108
|
|
|
[ |
109
|
42 |
|
'alias' => 'validatorFactory', |
110
|
|
|
'public' => false, |
111
|
|
|
] |
112
|
|
|
); |
113
|
|
|
} |
114
|
42 |
|
} |
115
|
|
|
|
116
|
42 |
|
private function setDefaultFieldResolver(array $config, ContainerBuilder $container): void |
117
|
|
|
{ |
118
|
42 |
|
$container->setAlias($this->getAlias().'.default_field_resolver', $config['definitions']['default_field_resolver']); |
119
|
42 |
|
} |
120
|
|
|
|
121
|
42 |
|
private function setCompilerCacheWarmer(array $config, ContainerBuilder $container): void |
122
|
|
|
{ |
123
|
42 |
|
$container->register(CompileCacheWarmer::class) |
124
|
42 |
|
->setArguments([ |
125
|
42 |
|
new Reference($this->getAlias().'.cache_compiler'), |
126
|
42 |
|
$config['definitions']['auto_compile'], |
127
|
|
|
]) |
128
|
42 |
|
->addTag('kernel.cache_warmer', ['priority' => 50]) |
129
|
|
|
; |
130
|
42 |
|
} |
131
|
|
|
|
132
|
42 |
|
private function setClassLoaderListener(array $config, ContainerBuilder $container): void |
133
|
|
|
{ |
134
|
42 |
|
$container->setParameter($this->getAlias().'.use_classloader_listener', $config['definitions']['use_classloader_listener']); |
135
|
42 |
|
if ($config['definitions']['use_classloader_listener']) { |
136
|
41 |
|
$definition = $container->register( |
137
|
41 |
|
$this->getAlias().'.event_listener.classloader_listener', |
138
|
41 |
|
ClassLoaderListener::class |
139
|
|
|
); |
140
|
41 |
|
$definition->setPublic(true); |
141
|
41 |
|
$definition->setArguments([new Reference($this->getAlias().'.cache_compiler')]); |
142
|
41 |
|
$definition->addTag('kernel.event_listener', ['event' => 'kernel.request', 'method' => 'load', 'priority' => 255]); |
143
|
41 |
|
$definition->addTag('kernel.event_listener', ['event' => 'console.command', 'method' => 'load', 'priority' => 255]); |
144
|
|
|
} |
145
|
42 |
|
} |
146
|
|
|
|
147
|
42 |
|
private function setDefinitionParameters(array $config, ContainerBuilder $container): void |
148
|
|
|
{ |
149
|
|
|
// generator and config |
150
|
42 |
|
$container->setParameter($this->getAlias().'.class_namespace', $config['definitions']['class_namespace']); |
151
|
42 |
|
$container->setParameter($this->getAlias().'.cache_dir', $config['definitions']['cache_dir']); |
152
|
42 |
|
$container->setParameter($this->getAlias().'.cache_dir_permissions', $config['definitions']['cache_dir_permissions']); |
153
|
42 |
|
$container->setParameter($this->getAlias().'.argument_class', $config['definitions']['argument_class']); |
154
|
42 |
|
$container->setParameter($this->getAlias().'.use_experimental_executor', $config['definitions']['use_experimental_executor']); |
155
|
42 |
|
} |
156
|
|
|
|
157
|
42 |
|
private function setBatchingMethod(array $config, ContainerBuilder $container): void |
158
|
|
|
{ |
159
|
42 |
|
$container->setParameter($this->getAlias().'.batching_method', $config['batching_method']); |
160
|
42 |
|
} |
161
|
|
|
|
162
|
42 |
|
private function setDebugListener(array $config, ContainerBuilder $container): void |
163
|
|
|
{ |
164
|
42 |
|
if ($config['definitions']['show_debug_info']) { |
165
|
1 |
|
$definition = $container->register(DebugListener::class); |
166
|
1 |
|
$definition->addTag('kernel.event_listener', ['event' => Events::PRE_EXECUTOR, 'method' => 'onPreExecutor']); |
167
|
1 |
|
$definition->addTag('kernel.event_listener', ['event' => Events::POST_EXECUTOR, 'method' => 'onPostExecutor']); |
168
|
|
|
} |
169
|
42 |
|
} |
170
|
|
|
|
171
|
42 |
|
private function setConfigBuilders(array $config, ContainerBuilder $container): void |
172
|
|
|
{ |
173
|
42 |
|
foreach (BuilderProcessor::BUILDER_TYPES as $type) { |
174
|
42 |
|
if (!empty($config['definitions']['builders'][$type])) { |
175
|
5 |
|
foreach ($config['definitions']['builders'][$type] as $params) { |
176
|
5 |
|
$container->addObjectResource($params['class']); |
177
|
5 |
|
BuilderProcessor::addBuilderClass($params['alias'], $type, $params['class']); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
42 |
|
} |
182
|
|
|
|
183
|
42 |
|
private function setSecurity(array $config, ContainerBuilder $container): void |
184
|
|
|
{ |
185
|
42 |
|
$executorDefinition = $container->getDefinition(Executor::class); |
186
|
42 |
|
if ($config['security']['enable_introspection']) { |
187
|
41 |
|
$executorDefinition->addMethodCall('enableIntrospectionQuery'); |
188
|
|
|
} else { |
189
|
1 |
|
$executorDefinition->addMethodCall('disableIntrospectionQuery'); |
190
|
|
|
} |
191
|
|
|
|
192
|
42 |
|
foreach ($config['security'] as $key => $value) { |
193
|
42 |
|
$container->setParameter(\sprintf('%s.%s', $this->getAlias(), $key), $value); |
194
|
|
|
} |
195
|
42 |
|
} |
196
|
|
|
|
197
|
42 |
|
private function setErrorHandler(array $config, ContainerBuilder $container): void |
198
|
|
|
{ |
199
|
42 |
|
if (!$config['errors_handler']['enabled']) { |
200
|
|
|
return; |
201
|
|
|
} |
202
|
|
|
|
203
|
42 |
|
$container->register(ExceptionConverter::class) |
204
|
42 |
|
->setArgument(0, $this->buildExceptionMap($config['errors_handler']['exceptions'])) |
205
|
42 |
|
->setArgument(1, $config['errors_handler']['map_exceptions_to_parent']); |
206
|
|
|
|
207
|
42 |
|
$container->register(ErrorHandler::class) |
208
|
42 |
|
->setArgument(0, new Reference(EventDispatcherInterface::class)) |
209
|
42 |
|
->setArgument(1, new Reference(ExceptionConverterInterface::class)); |
210
|
|
|
|
211
|
42 |
|
$container->register(ErrorHandlerListener::class) |
212
|
42 |
|
->setArgument(0, new Reference(ErrorHandler::class)) |
213
|
42 |
|
->setArgument(1, $config['errors_handler']['rethrow_internal_exceptions']) |
214
|
42 |
|
->setArgument(2, $config['errors_handler']['debug']) |
215
|
42 |
|
->addTag('kernel.event_listener', ['event' => Events::POST_EXECUTOR, 'method' => 'onPostExecutor']); |
216
|
|
|
|
217
|
42 |
|
$container->setAlias(ExceptionConverterInterface::class, ExceptionConverter::class); |
218
|
|
|
|
219
|
42 |
|
if ($config['errors_handler']['log']) { |
220
|
42 |
|
$loggerServiceId = $config['errors_handler']['logger_service']; |
221
|
42 |
|
$invalidBehavior = ErrorLoggerListener::DEFAULT_LOGGER_SERVICE === $loggerServiceId ? ContainerInterface::NULL_ON_INVALID_REFERENCE : ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE; |
222
|
|
|
|
223
|
42 |
|
$container->register(ErrorLoggerListener::class) |
224
|
42 |
|
->setPublic(true) |
225
|
42 |
|
->addArgument(new Reference($loggerServiceId, $invalidBehavior)) |
226
|
42 |
|
->addTag('kernel.event_listener', ['event' => Events::ERROR_FORMATTING, 'method' => 'onErrorFormatting']); |
227
|
|
|
} |
228
|
42 |
|
} |
229
|
|
|
|
230
|
42 |
|
private function setSchemaBuilderArguments(array $config, ContainerBuilder $container): void |
231
|
|
|
{ |
232
|
42 |
|
$container->getDefinition(SchemaBuilder::class) |
233
|
42 |
|
->replaceArgument(1, $config['definitions']['config_validation']); |
234
|
42 |
|
} |
235
|
|
|
|
236
|
42 |
|
private function setSchemaArguments(array $config, ContainerBuilder $container): void |
237
|
|
|
{ |
238
|
42 |
|
if (isset($config['definitions']['schema'])) { |
239
|
42 |
|
$executorDefinition = $container->getDefinition(Executor::class); |
240
|
42 |
|
$typeDecoratorListenerDefinition = $container->getDefinition(TypeDecoratorListener::class); |
241
|
|
|
|
242
|
42 |
|
foreach ($config['definitions']['schema'] as $schemaName => $schemaConfig) { |
243
|
|
|
// builder |
244
|
34 |
|
$schemaBuilderID = \sprintf('%s.schema_builder_%s', $this->getAlias(), $schemaName); |
245
|
34 |
|
$definition = $container->register($schemaBuilderID, \Closure::class); |
246
|
34 |
|
$definition->setFactory([new Reference('overblog_graphql.schema_builder'), 'getBuilder']); |
247
|
34 |
|
$definition->setArguments([ |
248
|
34 |
|
$schemaName, |
249
|
34 |
|
$schemaConfig['query'], |
250
|
34 |
|
$schemaConfig['mutation'], |
251
|
34 |
|
$schemaConfig['subscription'], |
252
|
34 |
|
$schemaConfig['types'], |
253
|
|
|
]); |
254
|
|
|
// schema |
255
|
34 |
|
$schemaID = \sprintf('%s.schema_%s', $this->getAlias(), $schemaName); |
256
|
34 |
|
$definition = $container->register($schemaID, Schema::class); |
257
|
34 |
|
$definition->setFactory([new Reference($schemaBuilderID), 'call']); |
258
|
|
|
|
259
|
34 |
|
if (!empty($schemaConfig['resolver_maps'])) { |
260
|
1 |
|
$typeDecoratorListenerDefinition->addMethodCall( |
261
|
1 |
|
'addSchemaResolverMaps', |
262
|
|
|
[ |
263
|
1 |
|
$schemaName, |
264
|
|
|
\array_map(function ($id) { |
265
|
1 |
|
if (!is_string($id)) { |
266
|
|
|
throw new \InvalidArgumentException(sprintf( |
267
|
|
|
'Schema resolver maps must be string class references, got %s instead. Check your resolver config.', |
268
|
|
|
gettype($id) |
269
|
|
|
)); |
270
|
|
|
} |
271
|
|
|
|
272
|
1 |
|
return new Reference($id); |
273
|
1 |
|
}, $schemaConfig['resolver_maps']), |
274
|
|
|
] |
275
|
|
|
); |
276
|
|
|
} |
277
|
34 |
|
$executorDefinition->addMethodCall('addSchemaBuilder', [$schemaName, new Reference($schemaBuilderID)]); |
278
|
|
|
} |
279
|
|
|
} |
280
|
42 |
|
} |
281
|
|
|
|
282
|
42 |
|
private function setServicesAliases(array $config, ContainerBuilder $container): void |
283
|
|
|
{ |
284
|
42 |
|
if (isset($config['services'])) { |
285
|
42 |
|
foreach ($config['services'] as $name => $id) { |
286
|
42 |
|
$alias = \sprintf('%s.%s', $this->getAlias(), $name); |
287
|
42 |
|
$container->setAlias($alias, $id); |
288
|
|
|
} |
289
|
|
|
} |
290
|
42 |
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Returns a list of custom exceptions mapped to error/warning classes. |
294
|
|
|
* |
295
|
|
|
* @param array<string, string[]> $exceptionConfig |
296
|
|
|
* |
297
|
|
|
* @return array<string, string> Custom exception map, [exception => UserError/UserWarning] |
298
|
|
|
*/ |
299
|
42 |
|
private function buildExceptionMap(array $exceptionConfig): array |
300
|
|
|
{ |
301
|
42 |
|
$exceptionMap = []; |
302
|
|
|
$errorsMapping = [ |
303
|
42 |
|
'errors' => UserError::class, |
304
|
|
|
'warnings' => UserWarning::class, |
305
|
|
|
]; |
306
|
|
|
|
307
|
42 |
|
foreach ($exceptionConfig as $type => $exceptionList) { |
308
|
42 |
|
foreach ($exceptionList as $exception) { |
309
|
2 |
|
$exceptionMap[$exception] = $errorsMapping[$type]; |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
313
|
42 |
|
return $exceptionMap; |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|