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
|
45 |
|
public function load(array $configs, ContainerBuilder $container) |
23
|
|
|
{ |
24
|
45 |
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
25
|
45 |
|
$loader->load('services.yml'); |
26
|
45 |
|
$loader->load('graphql_types.yml'); |
27
|
45 |
|
$loader->load('graphql_fields.yml'); |
28
|
45 |
|
$loader->load('graphql_args.yml'); |
29
|
|
|
|
30
|
45 |
|
$configuration = $this->getConfiguration($configs, $container); |
31
|
45 |
|
$config = $this->processConfiguration($configuration, $configs); |
32
|
|
|
|
33
|
45 |
|
$this->setServicesAliases($config, $container); |
34
|
45 |
|
$this->setSchemaBuilderArguments($config, $container); |
35
|
45 |
|
$this->setSchemaArguments($config, $container); |
36
|
45 |
|
$this->setErrorHandlerArguments($config, $container); |
37
|
45 |
|
$this->setGraphiQLTemplate($config, $container); |
38
|
45 |
|
$this->setSecurity($config, $container); |
39
|
45 |
|
} |
40
|
|
|
|
41
|
45 |
|
public function prepend(ContainerBuilder $container) |
42
|
|
|
{ |
43
|
45 |
|
$configs = $container->getExtensionConfig($this->getAlias()); |
44
|
45 |
|
$configs = $container->getParameterBag()->resolveValue($configs); |
45
|
45 |
|
$configuration = $this->getConfiguration($configs, $container); |
46
|
45 |
|
$config = $this->processConfiguration($configuration, $configs); |
47
|
|
|
|
48
|
|
|
/** @var OverblogGraphQLTypesExtension $typesExtension */ |
49
|
45 |
|
$typesExtension = $container->getExtension($this->getAlias().'_types'); |
50
|
45 |
|
$typesExtension->containerPrependExtensionConfig($config, $container); |
51
|
45 |
|
} |
52
|
|
|
|
53
|
45 |
|
private function setSecurity(array $config, ContainerBuilder $container) |
54
|
|
|
{ |
55
|
45 |
|
$container->setParameter($this->getAlias().'.query_max_depth', $config['security']['query_max_depth']); |
56
|
45 |
|
$container->setParameter($this->getAlias().'.query_max_complexity', $config['security']['query_max_complexity']); |
57
|
45 |
|
} |
58
|
|
|
|
59
|
45 |
|
private function setGraphiQLTemplate(array $config, ContainerBuilder $container) |
60
|
|
|
{ |
61
|
45 |
|
$container->setParameter($this->getAlias().'.graphiql_template', $config['templates']['graphiql']); |
62
|
45 |
|
} |
63
|
|
|
|
64
|
45 |
|
private function setErrorHandlerArguments(array $config, ContainerBuilder $container) |
65
|
|
|
{ |
66
|
45 |
|
if (isset($config['definitions']['internal_error_message'])) { |
67
|
|
|
$container |
68
|
|
|
->getDefinition($this->getAlias().'.error_handler') |
69
|
|
|
->replaceArgument(0, $config['definitions']['internal_error_message']) |
70
|
|
|
->setPublic(true) |
71
|
|
|
; |
72
|
|
|
} |
73
|
45 |
|
} |
74
|
|
|
|
75
|
45 |
|
private function setSchemaBuilderArguments(array $config, ContainerBuilder $container) |
76
|
|
|
{ |
77
|
45 |
|
$container->getDefinition($this->getAlias().'.schema_builder') |
78
|
45 |
|
->replaceArgument(1, $config['definitions']['config_validation']); |
79
|
45 |
|
} |
80
|
|
|
|
81
|
45 |
|
private function setSchemaArguments(array $config, ContainerBuilder $container) |
82
|
|
|
{ |
83
|
45 |
|
if (isset($config['definitions']['schema'])) { |
84
|
|
|
$container |
85
|
45 |
|
->getDefinition($this->getAlias().'.schema') |
86
|
45 |
|
->replaceArgument(0, $config['definitions']['schema']['query']) |
87
|
45 |
|
->replaceArgument(1, $config['definitions']['schema']['mutation']) |
88
|
45 |
|
->replaceArgument(2, $config['definitions']['schema']['subscription']) |
89
|
45 |
|
->setPublic(true) |
90
|
|
|
; |
91
|
45 |
|
} |
92
|
45 |
|
} |
93
|
|
|
|
94
|
45 |
|
private function setServicesAliases(array $config, ContainerBuilder $container) |
95
|
|
|
{ |
96
|
45 |
|
if (isset($config['services'])) { |
97
|
45 |
|
foreach ($config['services'] as $name => $id) { |
98
|
45 |
|
$alias = sprintf('%s.%s', $this->getAlias(), $name); |
99
|
45 |
|
$container->setAlias($alias, $id); |
100
|
45 |
|
} |
101
|
45 |
|
} |
102
|
45 |
|
} |
103
|
|
|
|
104
|
45 |
|
public function getAlias() |
105
|
|
|
{ |
106
|
45 |
|
return 'overblog_graphql'; |
107
|
|
|
} |
108
|
|
|
|
109
|
45 |
|
public function getConfiguration(array $config, ContainerBuilder $container) |
110
|
|
|
{ |
111
|
45 |
|
return new Configuration($container->getParameter('kernel.debug')); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|