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