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