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
|
9 |
|
public function load(array $configs, ContainerBuilder $container) |
24
|
|
|
{ |
25
|
9 |
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
26
|
9 |
|
$loader->load('services.yml'); |
27
|
9 |
|
$loader->load('graphql_types.yml'); |
28
|
9 |
|
$loader->load('graphql_resolvers.yml'); |
29
|
|
|
|
30
|
9 |
|
$config = $this->treatConfigs($configs, $container); |
31
|
|
|
|
32
|
9 |
|
$this->setServicesAliases($config, $container); |
|
|
|
|
33
|
9 |
|
$this->setSchemaBuilderArguments($config, $container); |
|
|
|
|
34
|
9 |
|
$this->setSchemaArguments($config, $container); |
|
|
|
|
35
|
9 |
|
$this->setErrorHandlerArguments($config, $container); |
|
|
|
|
36
|
9 |
|
$this->setGraphiQLTemplate($config, $container); |
|
|
|
|
37
|
9 |
|
$this->setSecurity($config, $container); |
|
|
|
|
38
|
9 |
|
$this->setConfigBuilders($config); |
|
|
|
|
39
|
|
|
|
40
|
9 |
|
$container->setParameter($this->getAlias().'.resources_dir', realpath(__DIR__.'/../Resources')); |
41
|
9 |
|
} |
42
|
|
|
|
43
|
8 |
|
public function prepend(ContainerBuilder $container) |
44
|
|
|
{ |
45
|
8 |
|
$configs = $container->getExtensionConfig($this->getAlias()); |
46
|
8 |
|
$configs = $container->getParameterBag()->resolveValue($configs); |
47
|
8 |
|
$config = $this->treatConfigs($configs, $container, true); |
48
|
|
|
|
49
|
|
|
/** @var OverblogGraphQLTypesExtension $typesExtension */ |
50
|
8 |
|
$typesExtension = $container->getExtension($this->getAlias().'_types'); |
51
|
8 |
|
$typesExtension->containerPrependExtensionConfig($config, $container); |
|
|
|
|
52
|
8 |
|
} |
53
|
|
|
|
54
|
9 |
|
private function setConfigBuilders(array $config) |
55
|
|
|
{ |
56
|
9 |
|
foreach (['args', 'field'] as $category) { |
57
|
9 |
|
if (!empty($config['definitions']['builders'][$category])) { |
58
|
1 |
|
$method = 'add'.ucfirst($category).'BuilderClass'; |
59
|
|
|
|
60
|
1 |
|
foreach ($config['definitions']['builders'][$category] as $params) { |
61
|
1 |
|
TypeWithOutputFieldsDefinition::$method($params['alias'], $params['class']); |
62
|
1 |
|
} |
63
|
1 |
|
} |
64
|
9 |
|
} |
65
|
9 |
|
} |
66
|
|
|
|
67
|
9 |
|
private function treatConfigs(array $configs, ContainerBuilder $container, $forceReload = false) |
68
|
|
|
{ |
69
|
9 |
|
static $config = null; |
70
|
|
|
|
71
|
9 |
|
if ($forceReload || null === $config) { |
72
|
9 |
|
$configuration = $this->getConfiguration($configs, $container); |
73
|
9 |
|
$config = $this->processConfiguration($configuration, $configs); |
74
|
9 |
|
} |
75
|
|
|
|
76
|
9 |
|
return $config; |
77
|
|
|
} |
78
|
|
|
|
79
|
9 |
|
private function setSecurity(array $config, ContainerBuilder $container) |
80
|
|
|
{ |
81
|
9 |
|
$container->setParameter($this->getAlias().'.query_max_depth', $config['security']['query_max_depth']); |
82
|
9 |
|
$container->setParameter($this->getAlias().'.query_max_complexity', $config['security']['query_max_complexity']); |
83
|
9 |
|
} |
84
|
|
|
|
85
|
9 |
|
private function setGraphiQLTemplate(array $config, ContainerBuilder $container) |
86
|
|
|
{ |
87
|
9 |
|
$container->setParameter($this->getAlias().'.graphiql_template', $config['templates']['graphiql']); |
88
|
9 |
|
} |
89
|
|
|
|
90
|
9 |
|
private function setErrorHandlerArguments(array $config, ContainerBuilder $container) |
91
|
|
|
{ |
92
|
9 |
|
if (isset($config['definitions']['internal_error_message'])) { |
93
|
|
|
$container |
94
|
1 |
|
->getDefinition($this->getAlias().'.error_handler') |
95
|
1 |
|
->replaceArgument(0, $config['definitions']['internal_error_message']) |
96
|
|
|
; |
97
|
1 |
|
} |
98
|
9 |
|
} |
99
|
|
|
|
100
|
9 |
|
private function setSchemaBuilderArguments(array $config, ContainerBuilder $container) |
101
|
|
|
{ |
102
|
9 |
|
$container->getDefinition($this->getAlias().'.schema_builder') |
103
|
9 |
|
->replaceArgument(1, $config['definitions']['config_validation']); |
104
|
9 |
|
} |
105
|
|
|
|
106
|
9 |
|
private function setSchemaArguments(array $config, ContainerBuilder $container) |
107
|
|
|
{ |
108
|
9 |
|
if (isset($config['definitions']['schema'])) { |
109
|
|
|
$container |
110
|
9 |
|
->getDefinition($this->getAlias().'.schema') |
111
|
9 |
|
->replaceArgument(0, $config['definitions']['schema']['query']) |
112
|
9 |
|
->replaceArgument(1, $config['definitions']['schema']['mutation']) |
113
|
9 |
|
->replaceArgument(2, $config['definitions']['schema']['subscription']) |
114
|
9 |
|
->setPublic(true) |
115
|
|
|
; |
116
|
9 |
|
} |
117
|
9 |
|
} |
118
|
|
|
|
119
|
9 |
|
private function setServicesAliases(array $config, ContainerBuilder $container) |
120
|
|
|
{ |
121
|
9 |
|
if (isset($config['services'])) { |
122
|
9 |
|
foreach ($config['services'] as $name => $id) { |
123
|
9 |
|
$alias = sprintf('%s.%s', $this->getAlias(), $name); |
124
|
9 |
|
$container->setAlias($alias, $id); |
125
|
9 |
|
} |
126
|
9 |
|
} |
127
|
9 |
|
} |
128
|
|
|
|
129
|
9 |
|
public function getAlias() |
130
|
|
|
{ |
131
|
9 |
|
return 'overblog_graphql'; |
132
|
|
|
} |
133
|
|
|
|
134
|
9 |
|
public function getConfiguration(array $config, ContainerBuilder $container) |
135
|
|
|
{ |
136
|
9 |
|
return new Configuration($container->getParameter('kernel.debug')); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
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.