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