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 Symfony\Component\Config\FileLocator; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
16
|
|
|
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; |
17
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
18
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
19
|
|
|
|
20
|
|
|
class OverblogGraphQLExtension extends Extension implements PrependExtensionInterface |
21
|
|
|
{ |
22
|
36 |
|
public function load(array $configs, ContainerBuilder $container) |
23
|
|
|
{ |
24
|
36 |
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
25
|
36 |
|
$loader->load('services.yml'); |
26
|
36 |
|
$loader->load('graphql_types.yml'); |
27
|
36 |
|
$loader->load('graphql_fields.yml'); |
28
|
36 |
|
$loader->load('graphql_args.yml'); |
29
|
|
|
|
30
|
36 |
|
$configuration = $this->getConfiguration($configs, $container); |
31
|
36 |
|
$config = $this->processConfiguration($configuration, $configs); |
32
|
|
|
|
33
|
36 |
|
$this->setServicesAliases($config, $container); |
34
|
36 |
|
$this->setSchemaBuilderArguments($config, $container); |
35
|
36 |
|
$this->setSchemaArguments($config, $container); |
36
|
36 |
|
$this->setErrorHandlerArguments($config, $container); |
37
|
36 |
|
$this->setGraphiQLTemplate($config, $container); |
38
|
36 |
|
$this->setSecurity($config, $container); |
39
|
|
|
} |
40
|
36 |
|
|
41
|
|
|
public function prepend(ContainerBuilder $container) |
42
|
36 |
|
{ |
43
|
36 |
|
$configs = $container->getExtensionConfig($this->getAlias()); |
44
|
36 |
|
$configs = $container->getParameterBag()->resolveValue($configs); |
45
|
36 |
|
$configuration = $this->getConfiguration($configs, $container); |
46
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
47
|
|
|
|
48
|
36 |
|
/** @var OverblogGraphQLTypesExtension $typesExtension */ |
49
|
36 |
|
$typesExtension = $container->getExtension($this->getAlias().'_types'); |
50
|
36 |
|
$typesExtension->containerPrependExtensionConfig($config, $container); |
51
|
|
|
} |
52
|
36 |
|
|
53
|
|
View Code Duplication |
private function setSecurity(array $config, ContainerBuilder $container) |
|
|
|
|
54
|
36 |
|
{ |
55
|
36 |
|
if (isset($config['security']['query_max_depth'])) { |
56
|
36 |
|
$container |
57
|
36 |
|
->getDefinition($this->getAlias().'.request_validator_rule_max_query_depth') |
58
|
|
|
->addMethodCall('setMaxQueryDepth', [$config['security']['query_max_depth']]) |
59
|
36 |
|
->setPublic(true) |
60
|
|
|
; |
61
|
36 |
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function setGraphiQLTemplate(array $config, ContainerBuilder $container) |
65
|
|
|
{ |
66
|
|
|
if (isset($config['templates']['graphiql'])) { |
67
|
|
|
$container->setParameter('overblog_graphql.graphiql_template', $config['templates']['graphiql']); |
68
|
36 |
|
} |
69
|
|
|
} |
70
|
36 |
|
|
71
|
|
View Code Duplication |
private function setErrorHandlerArguments(array $config, ContainerBuilder $container) |
|
|
|
|
72
|
36 |
|
{ |
73
|
36 |
|
if (isset($config['definitions']['internal_error_message'])) { |
74
|
36 |
|
$container |
75
|
|
|
->getDefinition($this->getAlias().'.error_handler') |
76
|
36 |
|
->replaceArgument(0, $config['definitions']['internal_error_message']) |
77
|
|
|
->setPublic(true) |
78
|
36 |
|
; |
79
|
|
|
} |
80
|
36 |
|
} |
81
|
36 |
|
|
82
|
36 |
|
private function setSchemaBuilderArguments(array $config, ContainerBuilder $container) |
83
|
36 |
|
{ |
84
|
36 |
|
$container->getDefinition($this->getAlias().'.schema_builder') |
85
|
|
|
->replaceArgument(1, $config['definitions']['config_validation']); |
86
|
36 |
|
} |
87
|
36 |
|
|
88
|
|
|
private function setSchemaArguments(array $config, ContainerBuilder $container) |
89
|
36 |
|
{ |
90
|
|
|
if (isset($config['definitions']['schema'])) { |
91
|
36 |
|
$container |
92
|
36 |
|
->getDefinition($this->getAlias().'.schema') |
93
|
36 |
|
->replaceArgument(0, $config['definitions']['schema']['query']) |
94
|
36 |
|
->replaceArgument(1, $config['definitions']['schema']['mutation']) |
95
|
36 |
|
->replaceArgument(2, $config['definitions']['schema']['subscription']) |
96
|
36 |
|
->setPublic(true) |
97
|
36 |
|
; |
98
|
|
|
} |
99
|
36 |
|
} |
100
|
|
|
|
101
|
36 |
|
private function setServicesAliases(array $config, ContainerBuilder $container) |
102
|
|
|
{ |
103
|
|
|
if (isset($config['services'])) { |
104
|
36 |
|
foreach ($config['services'] as $name => $id) { |
105
|
|
|
$alias = sprintf('%s.%s', $this->getAlias(), $name); |
106
|
36 |
|
$container->setAlias($alias, $id); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function getAlias() |
112
|
|
|
{ |
113
|
|
|
return 'overblog_graphql'; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function getConfiguration(array $config, ContainerBuilder $container) |
117
|
|
|
{ |
118
|
|
|
return new Configuration($container->getParameter('kernel.debug')); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.