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