1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use GraphQL\Error\UserError; |
6
|
|
|
use GraphQL\Type\Schema; |
7
|
|
|
use Overblog\GraphQLBundle\CacheWarmer\CompileCacheWarmer; |
8
|
|
|
use Overblog\GraphQLBundle\Config\Processor\BuilderProcessor; |
9
|
|
|
use Overblog\GraphQLBundle\Error\ErrorHandler; |
10
|
|
|
use Overblog\GraphQLBundle\Error\UserWarning; |
11
|
|
|
use Overblog\GraphQLBundle\Event\Events; |
12
|
|
|
use Overblog\GraphQLBundle\EventListener\ClassLoaderListener; |
13
|
|
|
use Overblog\GraphQLBundle\EventListener\DebugListener; |
14
|
|
|
use Overblog\GraphQLBundle\EventListener\ErrorHandlerListener; |
15
|
|
|
use Overblog\GraphQLBundle\EventListener\ErrorLoggerListener; |
16
|
|
|
use Symfony\Component\Config\FileLocator; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
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\HttpKernel\DependencyInjection\Extension; |
24
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
25
|
|
|
|
26
|
|
|
class OverblogGraphQLExtension extends Extension implements PrependExtensionInterface |
27
|
|
|
{ |
28
|
30 |
|
public function load(array $configs, ContainerBuilder $container) |
29
|
|
|
{ |
30
|
30 |
|
$this->loadConfigFiles($container); |
31
|
30 |
|
$config = $this->treatConfigs($configs, $container); |
32
|
|
|
|
33
|
30 |
|
$this->setBatchingMethod($config, $container); |
34
|
30 |
|
$this->setServicesAliases($config, $container); |
35
|
30 |
|
$this->setSchemaBuilderArguments($config, $container); |
36
|
30 |
|
$this->setSchemaArguments($config, $container); |
37
|
30 |
|
$this->setErrorHandler($config, $container); |
38
|
30 |
|
$this->setSecurity($config, $container); |
39
|
30 |
|
$this->setConfigBuilders($config, $container); |
40
|
30 |
|
$this->setDebugListener($config, $container); |
41
|
30 |
|
$this->setDefinitionParameters($config, $container); |
42
|
30 |
|
$this->setClassLoaderListener($config, $container); |
43
|
30 |
|
$this->setCompilerCacheWarmer($config, $container); |
44
|
|
|
|
45
|
30 |
|
$container->setParameter($this->getAlias().'.resources_dir', realpath(__DIR__.'/../Resources')); |
46
|
30 |
|
} |
47
|
|
|
|
48
|
28 |
|
public function prepend(ContainerBuilder $container) |
49
|
|
|
{ |
50
|
28 |
|
$configs = $container->getExtensionConfig($this->getAlias()); |
51
|
28 |
|
$configs = $container->getParameterBag()->resolveValue($configs); |
52
|
28 |
|
$config = $this->treatConfigs($configs, $container, true); |
53
|
|
|
|
54
|
|
|
/** @var OverblogGraphQLTypesExtension $typesExtension */ |
55
|
28 |
|
$typesExtension = $container->getExtension($this->getAlias().'_types'); |
56
|
28 |
|
$typesExtension->containerPrependExtensionConfig($config, $container); |
57
|
28 |
|
} |
58
|
|
|
|
59
|
30 |
|
public function getAlias() |
60
|
|
|
{ |
61
|
30 |
|
return Configuration::NAME; |
62
|
|
|
} |
63
|
|
|
|
64
|
30 |
|
public function getConfiguration(array $config, ContainerBuilder $container) |
65
|
|
|
{ |
66
|
30 |
|
return new Configuration( |
67
|
30 |
|
$container->getParameter('kernel.debug'), |
68
|
30 |
|
$container->hasParameter('kernel.cache_dir') ? $container->getParameter('kernel.cache_dir') : null |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
30 |
|
private function loadConfigFiles(ContainerBuilder $container) |
73
|
|
|
{ |
74
|
30 |
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
75
|
30 |
|
$loader->load('services.yml'); |
76
|
30 |
|
$loader->load('graphql_types.yml'); |
77
|
30 |
|
$loader->load('expression_language_functions.yml'); |
78
|
30 |
|
$loader->load('definition_config_processors.yml'); |
79
|
30 |
|
} |
80
|
|
|
|
81
|
30 |
|
private function setCompilerCacheWarmer(array $config, ContainerBuilder $container) |
82
|
|
|
{ |
83
|
30 |
|
if ($config['definitions']['auto_compile']) { |
84
|
29 |
|
$definition = $container->setDefinition( |
85
|
29 |
|
CompileCacheWarmer::class, |
86
|
29 |
|
new Definition(CompileCacheWarmer::class) |
87
|
|
|
); |
88
|
29 |
|
$definition->setArguments([new Reference($this->getAlias().'.cache_compiler')]); |
89
|
29 |
|
$definition->addTag('kernel.cache_warmer', ['priority' => 50]); |
90
|
|
|
} |
91
|
30 |
|
} |
92
|
|
|
|
93
|
30 |
|
private function setClassLoaderListener(array $config, ContainerBuilder $container) |
94
|
|
|
{ |
95
|
30 |
|
$container->setParameter($this->getAlias().'.use_classloader_listener', $config['definitions']['use_classloader_listener']); |
96
|
30 |
|
if ($config['definitions']['use_classloader_listener']) { |
97
|
29 |
|
$definition = $container->setDefinition( |
98
|
29 |
|
$this->getAlias().'.event_listener.classloader_listener', |
99
|
29 |
|
new Definition(ClassLoaderListener::class) |
100
|
|
|
); |
101
|
29 |
|
$definition->setPublic(true); |
102
|
29 |
|
$definition->setArguments([new Reference($this->getAlias().'.cache_compiler')]); |
103
|
29 |
|
$definition->addTag('kernel.event_listener', ['event' => 'kernel.request', 'method' => 'load', 'priority' => 255]); |
104
|
29 |
|
$definition->addTag('kernel.event_listener', ['event' => 'console.command', 'method' => 'load', 'priority' => 255]); |
105
|
|
|
} |
106
|
30 |
|
} |
107
|
|
|
|
108
|
30 |
|
private function setDefinitionParameters(array $config, ContainerBuilder $container) |
109
|
|
|
{ |
110
|
|
|
// auto mapping |
111
|
30 |
|
$container->setParameter($this->getAlias().'.auto_mapping.enabled', $config['definitions']['auto_mapping']['enabled']); |
112
|
30 |
|
$container->setParameter($this->getAlias().'.auto_mapping.directories', $config['definitions']['auto_mapping']['directories']); |
113
|
|
|
// generator and config |
114
|
30 |
|
$container->setParameter($this->getAlias().'.default_resolver', $config['definitions']['default_resolver']); |
115
|
30 |
|
$container->setParameter($this->getAlias().'.class_namespace', $config['definitions']['class_namespace']); |
116
|
30 |
|
$container->setParameter($this->getAlias().'.cache_dir', $config['definitions']['cache_dir']); |
117
|
30 |
|
} |
118
|
|
|
|
119
|
30 |
|
private function setBatchingMethod(array $config, ContainerBuilder $container) |
120
|
|
|
{ |
121
|
30 |
|
$container->setParameter($this->getAlias().'.batching_method', $config['batching_method']); |
122
|
30 |
|
} |
123
|
|
|
|
124
|
30 |
|
private function setDebugListener(array $config, ContainerBuilder $container) |
125
|
|
|
{ |
126
|
30 |
|
if ($config['definitions']['show_debug_info']) { |
127
|
1 |
|
$definition = $container->setDefinition( |
128
|
1 |
|
DebugListener::class, |
129
|
1 |
|
new Definition(DebugListener::class) |
130
|
|
|
); |
131
|
1 |
|
$definition->addTag('kernel.event_listener', ['event' => Events::PRE_EXECUTOR, 'method' => 'onPreExecutor']); |
132
|
1 |
|
$definition->addTag('kernel.event_listener', ['event' => Events::POST_EXECUTOR, 'method' => 'onPostExecutor']); |
133
|
|
|
} |
134
|
30 |
|
} |
135
|
|
|
|
136
|
30 |
|
private function setConfigBuilders(array $config, ContainerBuilder $container) |
137
|
|
|
{ |
138
|
30 |
|
$useObjectToAddResource = method_exists($container, 'addObjectResource'); |
139
|
30 |
|
$objectToAddResourceMethod = $useObjectToAddResource ? 'addObjectResource' : 'addClassResource'; |
140
|
|
|
|
141
|
30 |
|
foreach (BuilderProcessor::BUILDER_TYPES as $type) { |
142
|
30 |
|
if (!empty($config['definitions']['builders'][$type])) { |
143
|
1 |
|
foreach ($config['definitions']['builders'][$type] as $params) { |
144
|
1 |
|
$object = $useObjectToAddResource ? $params['class'] : new \ReflectionClass($params['class']); |
145
|
1 |
|
$container->$objectToAddResourceMethod($object); |
146
|
30 |
|
BuilderProcessor::addBuilderClass($params['alias'], $type, $params['class']); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
30 |
|
} |
151
|
|
|
|
152
|
30 |
|
private function treatConfigs(array $configs, ContainerBuilder $container, $forceReload = false) |
153
|
|
|
{ |
154
|
30 |
|
static $config = null; |
155
|
|
|
|
156
|
30 |
|
if ($forceReload || null === $config) { |
157
|
30 |
|
$configuration = $this->getConfiguration($configs, $container); |
158
|
30 |
|
$config = $this->processConfiguration($configuration, $configs); |
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
30 |
|
return $config; |
162
|
|
|
} |
163
|
|
|
|
164
|
30 |
|
private function setSecurity(array $config, ContainerBuilder $container) |
165
|
|
|
{ |
166
|
30 |
|
foreach ($config['security'] as $key => $value) { |
167
|
30 |
|
$container->setParameter(sprintf('%s.%s', $this->getAlias(), $key), $value); |
168
|
|
|
} |
169
|
30 |
|
} |
170
|
|
|
|
171
|
30 |
|
private function setErrorHandler(array $config, ContainerBuilder $container) |
172
|
|
|
{ |
173
|
30 |
|
if ($config['errors_handler']['enabled']) { |
174
|
30 |
|
$id = $this->getAlias().'.error_handler'; |
175
|
30 |
|
$errorHandlerDefinition = $container->setDefinition($id, new Definition(ErrorHandler::class)); |
176
|
30 |
|
$errorHandlerDefinition->setPublic(false) |
177
|
30 |
|
->setArguments( |
178
|
|
|
[ |
179
|
30 |
|
new Reference('event_dispatcher'), |
180
|
30 |
|
$config['errors_handler']['internal_error_message'], |
181
|
30 |
|
$this->buildExceptionMap($config['errors_handler']['exceptions']), |
182
|
30 |
|
$config['errors_handler']['map_exceptions_to_parent'], |
183
|
|
|
] |
184
|
|
|
) |
185
|
|
|
; |
186
|
|
|
|
187
|
30 |
|
$errorHandlerListenerDefinition = $container->setDefinition(ErrorHandlerListener::class, new Definition(ErrorHandlerListener::class)); |
188
|
30 |
|
$errorHandlerListenerDefinition->setPublic(true) |
189
|
30 |
|
->setArguments([new Reference($id), $config['errors_handler']['rethrow_internal_exceptions'], $config['errors_handler']['debug']]) |
190
|
30 |
|
->addTag('kernel.event_listener', ['event' => Events::POST_EXECUTOR, 'method' => 'onPostExecutor']) |
191
|
|
|
; |
192
|
|
|
|
193
|
30 |
|
if ($config['errors_handler']['log']) { |
194
|
30 |
|
$loggerServiceId = $config['errors_handler']['logger_service']; |
195
|
30 |
|
$invalidBehavior = ErrorLoggerListener::DEFAULT_LOGGER_SERVICE === $loggerServiceId ? ContainerInterface::NULL_ON_INVALID_REFERENCE : ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE; |
196
|
30 |
|
$errorHandlerListenerDefinition = $container->setDefinition(ErrorLoggerListener::class, new Definition(ErrorLoggerListener::class)); |
197
|
30 |
|
$errorHandlerListenerDefinition->setPublic(true) |
198
|
30 |
|
->setArguments([new Reference($loggerServiceId, $invalidBehavior)]) |
199
|
30 |
|
->addTag('kernel.event_listener', ['event' => Events::ERROR_FORMATTING, 'method' => 'onErrorFormatting']) |
200
|
|
|
; |
201
|
|
|
} |
202
|
|
|
} |
203
|
30 |
|
} |
204
|
|
|
|
205
|
30 |
|
private function setSchemaBuilderArguments(array $config, ContainerBuilder $container) |
206
|
|
|
{ |
207
|
30 |
|
$container->getDefinition($this->getAlias().'.schema_builder') |
208
|
30 |
|
->replaceArgument(1, $config['definitions']['config_validation']); |
209
|
30 |
|
} |
210
|
|
|
|
211
|
30 |
|
private function setSchemaArguments(array $config, ContainerBuilder $container) |
212
|
|
|
{ |
213
|
30 |
|
if (isset($config['definitions']['schema'])) { |
214
|
30 |
|
$executorDefinition = $container->getDefinition($this->getAlias().'.request_executor'); |
215
|
|
|
|
216
|
30 |
|
foreach ($config['definitions']['schema'] as $schemaName => $schemaConfig) { |
217
|
26 |
|
$schemaID = sprintf('%s.schema_%s', $this->getAlias(), $schemaName); |
218
|
26 |
|
$definition = new Definition(Schema::class); |
219
|
26 |
|
$definition->setFactory([new Reference('overblog_graphql.schema_builder'), 'create']); |
220
|
26 |
|
$definition->setArguments([ |
221
|
26 |
|
$schemaConfig['query'], |
222
|
26 |
|
$schemaConfig['mutation'], |
223
|
26 |
|
$schemaConfig['subscription'], |
224
|
26 |
|
array_map(function ($id) { |
225
|
1 |
|
return new Reference($id); |
226
|
26 |
|
}, $schemaConfig['resolver_maps']), |
227
|
26 |
|
$schemaConfig['types'], |
228
|
|
|
]); |
229
|
26 |
|
$definition->setPublic(false); |
230
|
26 |
|
$container->setDefinition($schemaID, $definition); |
231
|
|
|
|
232
|
26 |
|
$executorDefinition->addMethodCall('addSchema', [$schemaName, new Reference($schemaID)]); |
233
|
|
|
} |
234
|
|
|
} |
235
|
30 |
|
} |
236
|
|
|
|
237
|
30 |
|
private function setServicesAliases(array $config, ContainerBuilder $container) |
238
|
|
|
{ |
239
|
30 |
|
if (isset($config['services'])) { |
240
|
30 |
|
foreach ($config['services'] as $name => $id) { |
241
|
30 |
|
$alias = sprintf('%s.%s', $this->getAlias(), $name); |
242
|
30 |
|
$container->setAlias($alias, $id); |
243
|
|
|
} |
244
|
|
|
} |
245
|
30 |
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Returns a list of custom exceptions mapped to error/warning classes. |
249
|
|
|
* |
250
|
|
|
* @param array $exceptionConfig |
251
|
|
|
* |
252
|
|
|
* @return array Custom exception map, [exception => UserError/UserWarning] |
253
|
|
|
*/ |
254
|
30 |
|
private function buildExceptionMap(array $exceptionConfig) |
255
|
|
|
{ |
256
|
30 |
|
$exceptionMap = []; |
257
|
|
|
$errorsMapping = [ |
258
|
30 |
|
'errors' => UserError::class, |
259
|
|
|
'warnings' => UserWarning::class, |
260
|
|
|
]; |
261
|
|
|
|
262
|
30 |
|
foreach ($exceptionConfig as $type => $exceptionList) { |
263
|
30 |
|
foreach ($exceptionList as $exception) { |
264
|
30 |
|
$exceptionMap[$exception] = $errorsMapping[$type]; |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
268
|
30 |
|
return $exceptionMap; |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: