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