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