|
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 Overblog\GraphQLBundle\Config\TypeWithOutputFieldsDefinition; |
|
15
|
|
|
use Symfony\Component\Config\FileLocator; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; |
|
19
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
|
20
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
21
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
22
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
|
23
|
|
|
|
|
24
|
|
|
class OverblogGraphQLExtension extends Extension implements PrependExtensionInterface |
|
25
|
|
|
{ |
|
26
|
|
|
public function load(array $configs, ContainerBuilder $container) |
|
27
|
|
|
{ |
|
28
|
|
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
29
|
16 |
|
$loader->load('services.yml'); |
|
30
|
|
|
$loader->load('graphql_types.yml'); |
|
31
|
16 |
|
$loader->load('graphql_resolvers.yml'); |
|
32
|
16 |
|
|
|
33
|
16 |
|
$config = $this->treatConfigs($configs, $container); |
|
34
|
|
|
|
|
35
|
16 |
|
$this->setExpressionLanguageDefaultParser($container); |
|
36
|
|
|
$this->setServicesAliases($config, $container); |
|
|
|
|
|
|
37
|
16 |
|
$this->setSchemaBuilderArguments($config, $container); |
|
|
|
|
|
|
38
|
16 |
|
$this->setSchemaArguments($config, $container); |
|
|
|
|
|
|
39
|
16 |
|
$this->setErrorHandlerArguments($config, $container); |
|
|
|
|
|
|
40
|
16 |
|
$this->setGraphiQLTemplate($config, $container); |
|
|
|
|
|
|
41
|
16 |
|
$this->setSecurity($config, $container); |
|
|
|
|
|
|
42
|
16 |
|
$this->setConfigBuilders($config, $container); |
|
|
|
|
|
|
43
|
16 |
|
$this->setVersions($config, $container); |
|
|
|
|
|
|
44
|
16 |
|
$this->setShowDebug($config, $container); |
|
|
|
|
|
|
45
|
16 |
|
|
|
46
|
16 |
|
$container->setParameter($this->getAlias().'.resources_dir', realpath(__DIR__.'/../Resources')); |
|
47
|
16 |
|
} |
|
48
|
16 |
|
|
|
49
|
|
|
public function prepend(ContainerBuilder $container) |
|
50
|
16 |
|
{ |
|
51
|
16 |
|
$configs = $container->getExtensionConfig($this->getAlias()); |
|
52
|
|
|
$configs = $container->getParameterBag()->resolveValue($configs); |
|
53
|
14 |
|
$config = $this->treatConfigs($configs, $container, true); |
|
54
|
|
|
|
|
55
|
14 |
|
/** @var OverblogGraphQLTypesExtension $typesExtension */ |
|
56
|
14 |
|
$typesExtension = $container->getExtension($this->getAlias().'_types'); |
|
57
|
14 |
|
$typesExtension->containerPrependExtensionConfig($config, $container); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
14 |
|
private function setExpressionLanguageDefaultParser(ContainerBuilder $container) |
|
61
|
14 |
|
{ |
|
62
|
14 |
|
$class = version_compare(Kernel::VERSION, '3.2.0', '>=') ? |
|
63
|
|
|
'Symfony\\Component\\Cache\Adapter\\ArrayAdapter' |
|
64
|
16 |
|
: 'Symfony\\Component\\ExpressionLanguage\\ParserCache\\ArrayParserCache' |
|
65
|
|
|
; |
|
66
|
16 |
|
$definition = new Definition($class); |
|
67
|
|
|
$definition->setPublic(false); |
|
68
|
|
|
$container->setDefinition($this->getAlias().'.cache_expression_language_parser.default', $definition); |
|
69
|
16 |
|
} |
|
70
|
|
|
|
|
71
|
16 |
|
private function setShowDebug(array $config, ContainerBuilder $container) |
|
72
|
|
|
{ |
|
73
|
|
|
$container->getDefinition($this->getAlias().'.request_executor')->replaceArgument(4, $config['definitions']['show_debug_info']); |
|
74
|
16 |
|
} |
|
75
|
|
|
|
|
76
|
16 |
|
private function setVersions(array $config, ContainerBuilder $container) |
|
77
|
16 |
|
{ |
|
78
|
16 |
|
$container->setParameter($this->getAlias().'.versions.graphiql', $config['versions']['graphiql']); |
|
79
|
|
|
$container->setParameter($this->getAlias().'.versions.react', $config['versions']['react']); |
|
80
|
16 |
|
$container->setParameter($this->getAlias().'.versions.fetch', $config['versions']['fetch']); |
|
81
|
|
|
} |
|
82
|
16 |
|
|
|
83
|
16 |
|
private function setConfigBuilders(array $config, ContainerBuilder $container) |
|
84
|
|
|
{ |
|
85
|
16 |
|
foreach (['args', 'field'] as $category) { |
|
86
|
|
|
if (!empty($config['definitions']['builders'][$category])) { |
|
87
|
16 |
|
$method = 'add'.ucfirst($category).'BuilderClass'; |
|
88
|
16 |
|
|
|
89
|
16 |
|
foreach ($config['definitions']['builders'][$category] as $params) { |
|
90
|
16 |
|
$container->addClassResource(new \ReflectionClass($params['class'])); |
|
|
|
|
|
|
91
|
16 |
|
TypeWithOutputFieldsDefinition::$method($params['alias'], $params['class']); |
|
92
|
|
|
} |
|
93
|
16 |
|
} |
|
94
|
|
|
} |
|
95
|
16 |
|
} |
|
96
|
16 |
|
|
|
97
|
|
|
private function treatConfigs(array $configs, ContainerBuilder $container, $forceReload = false) |
|
98
|
16 |
|
{ |
|
99
|
|
|
static $config = null; |
|
100
|
16 |
|
|
|
101
|
16 |
|
if ($forceReload || null === $config) { |
|
102
|
16 |
|
$configuration = $this->getConfiguration($configs, $container); |
|
103
|
16 |
|
$config = $this->processConfiguration($configuration, $configs); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
16 |
|
|
|
106
|
|
|
return $config; |
|
107
|
16 |
|
} |
|
108
|
16 |
|
|
|
109
|
1 |
|
private function setSecurity(array $config, ContainerBuilder $container) |
|
110
|
|
|
{ |
|
111
|
1 |
|
$container->setParameter($this->getAlias().'.query_max_depth', $config['security']['query_max_depth']); |
|
112
|
1 |
|
$container->setParameter($this->getAlias().'.query_max_complexity', $config['security']['query_max_complexity']); |
|
113
|
1 |
|
} |
|
114
|
1 |
|
|
|
115
|
1 |
|
private function setGraphiQLTemplate(array $config, ContainerBuilder $container) |
|
116
|
16 |
|
{ |
|
117
|
16 |
|
$container->setParameter($this->getAlias().'.graphiql_template', $config['templates']['graphiql']); |
|
118
|
|
|
} |
|
119
|
16 |
|
|
|
120
|
|
|
private function setErrorHandlerArguments(array $config, ContainerBuilder $container) |
|
121
|
16 |
|
{ |
|
122
|
|
|
$errorHandlerDefinition = $container->getDefinition($this->getAlias().'.error_handler'); |
|
123
|
16 |
|
|
|
124
|
16 |
|
if (isset($config['definitions']['internal_error_message'])) { |
|
125
|
16 |
|
$errorHandlerDefinition->replaceArgument(0, $config['definitions']['internal_error_message']); |
|
126
|
16 |
|
} |
|
127
|
|
|
|
|
128
|
16 |
|
if (isset($config['definitions']['exceptions'])) { |
|
129
|
|
|
$errorHandlerDefinition |
|
130
|
|
|
->replaceArgument(2, $this->buildExceptionMap($config['definitions']['exceptions'])) |
|
131
|
16 |
|
->addMethodCall('setUserWarningClass', [$config['definitions']['exceptions']['types']['warnings']]) |
|
132
|
|
|
->addMethodCall('setUserErrorClass', [$config['definitions']['exceptions']['types']['errors']]) |
|
133
|
16 |
|
; |
|
134
|
16 |
|
} |
|
135
|
16 |
|
} |
|
136
|
16 |
|
|
|
137
|
|
|
private function setSchemaBuilderArguments(array $config, ContainerBuilder $container) |
|
138
|
16 |
|
{ |
|
139
|
|
|
$container->getDefinition($this->getAlias().'.schema_builder') |
|
140
|
16 |
|
->replaceArgument(1, $config['definitions']['config_validation']); |
|
141
|
16 |
|
} |
|
142
|
|
|
|
|
143
|
16 |
|
private function setSchemaArguments(array $config, ContainerBuilder $container) |
|
144
|
|
|
{ |
|
145
|
16 |
|
if (isset($config['definitions']['schema'])) { |
|
146
|
|
|
$executorDefinition = $container->getDefinition($this->getAlias().'.request_executor'); |
|
147
|
16 |
|
|
|
148
|
1 |
|
foreach ($config['definitions']['schema'] as $schemaName => $schemaConfig) { |
|
149
|
1 |
|
$schemaID = sprintf('%s.schema_%s', $this->getAlias(), $schemaName); |
|
150
|
|
|
$definition = new Definition('GraphQL\Schema'); |
|
151
|
16 |
|
$definition->setFactory([new Reference('overblog_graphql.schema_builder'), 'create']); |
|
152
|
|
|
$definition->setArguments([$schemaConfig['query'], $schemaConfig['mutation'], $schemaConfig['subscription']]); |
|
153
|
16 |
|
$definition->setPublic(false); |
|
154
|
16 |
|
$container->setDefinition($schemaID, $definition); |
|
155
|
16 |
|
|
|
156
|
|
|
$executorDefinition->addMethodCall('addSchema', [$schemaName, new Reference($schemaID)]); |
|
157
|
16 |
|
} |
|
158
|
16 |
|
} |
|
159
|
|
|
} |
|
160
|
16 |
|
|
|
161
|
|
|
private function setServicesAliases(array $config, ContainerBuilder $container) |
|
162
|
16 |
|
{ |
|
163
|
16 |
|
if (isset($config['services'])) { |
|
164
|
16 |
|
foreach ($config['services'] as $name => $id) { |
|
165
|
|
|
$alias = sprintf('%s.%s', $this->getAlias(), $name); |
|
166
|
16 |
|
$container->setAlias($alias, $id); |
|
167
|
|
|
} |
|
168
|
16 |
|
} |
|
169
|
16 |
|
} |
|
170
|
|
|
|
|
171
|
16 |
|
public function getAlias() |
|
172
|
13 |
|
{ |
|
173
|
13 |
|
return 'overblog_graphql'; |
|
174
|
13 |
|
} |
|
175
|
13 |
|
|
|
176
|
13 |
|
public function getConfiguration(array $config, ContainerBuilder $container) |
|
177
|
13 |
|
{ |
|
178
|
|
|
return new Configuration($container->getParameter('kernel.debug')); |
|
179
|
13 |
|
} |
|
180
|
16 |
|
|
|
181
|
16 |
|
/** |
|
182
|
16 |
|
* Returns a list of custom exceptions mapped to error/warning classes. |
|
183
|
|
|
* |
|
184
|
16 |
|
* @param array $exceptionConfig |
|
185
|
|
|
* |
|
186
|
16 |
|
* @return array Custom exception map, [exception => UserError/UserWarning] |
|
187
|
16 |
|
*/ |
|
188
|
16 |
|
private function buildExceptionMap(array $exceptionConfig) |
|
189
|
16 |
|
{ |
|
190
|
16 |
|
$exceptionMap = []; |
|
191
|
16 |
|
$typeMap = $exceptionConfig['types']; |
|
192
|
16 |
|
|
|
193
|
|
|
foreach ($exceptionConfig as $type => $exceptionList) { |
|
194
|
|
|
if ('types' === $type) { |
|
195
|
|
|
continue; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
foreach ($exceptionList as $exception) { |
|
199
|
|
|
$exceptionMap[$exception] = $typeMap[$type]; |
|
200
|
|
|
} |
|
201
|
16 |
|
} |
|
202
|
|
|
|
|
203
|
16 |
|
return $exceptionMap; |
|
204
|
16 |
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
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.