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\Resource\FileResource; |
15
|
|
|
use Symfony\Component\Config\Util\XmlUtils; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
17
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
18
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
19
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
20
|
|
|
use Symfony\Component\Finder\Finder; |
21
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
22
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
23
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
24
|
|
|
use Symfony\Component\Yaml\Parser as YamlParser; |
25
|
|
|
|
26
|
|
|
class OverblogGraphQLTypesExtension extends Extension |
27
|
|
|
{ |
28
|
|
|
private $yamlParser; |
29
|
|
|
|
30
|
27 |
|
public function load(array $configs, ContainerBuilder $container) |
31
|
|
|
{ |
32
|
27 |
|
$configuration = $this->getConfiguration($configs, $container); |
33
|
27 |
|
$config = $this->processConfiguration($configuration, $configs); |
34
|
|
|
|
35
|
27 |
|
$builderId = $this->getAliasPrefix().'.type_builder'; |
36
|
|
|
|
37
|
27 |
|
foreach ($config as $name => $options) { |
38
|
27 |
|
$customTypeId = sprintf('%s.definition.custom_%s_type', $this->getAliasPrefix(), $container->underscore($name)); |
39
|
|
|
|
40
|
27 |
|
$options['config']['name'] = $name; |
41
|
|
|
|
42
|
|
|
$container |
43
|
27 |
|
->setDefinition($customTypeId, new Definition('GraphQL\\Type\\Definition\\Type')) |
44
|
27 |
|
->setFactory([new Reference($builderId), 'create']) |
45
|
27 |
|
->setArguments([$options['type'], $options['config']]) |
46
|
27 |
|
->addTag($this->getAliasPrefix().'.type', ['alias' => $name]) |
47
|
|
|
; |
48
|
27 |
|
} |
49
|
27 |
|
} |
50
|
|
|
|
51
|
31 |
|
public function containerPrependExtensionConfig(array $config, ContainerBuilder $container) |
52
|
|
|
{ |
53
|
31 |
|
$typesMappings = array_merge( |
54
|
31 |
|
$this->typesConfigsMappingFromConfig($config, $container), |
55
|
31 |
|
$this->typesConfigsMappingFromBundles($container) |
56
|
31 |
|
); |
57
|
|
|
|
58
|
|
|
// treats mappings |
59
|
31 |
|
foreach ($typesMappings as $params) { |
60
|
27 |
|
$this->prependExtensionConfigFromFiles($params['type'], $params['files'], $container); |
61
|
31 |
|
} |
62
|
|
|
|
63
|
|
|
// TODO remove when types mapping 100% functional |
64
|
31 |
|
if (isset($config['definitions']['types'])) { |
65
|
|
|
$container->prependExtensionConfig($this->getAlias(), $config['definitions']['types']); |
66
|
|
|
} |
67
|
31 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $type |
71
|
|
|
* @param SplFileInfo[] $files |
72
|
|
|
* @param ContainerBuilder $container |
73
|
|
|
*/ |
74
|
27 |
|
private function prependExtensionConfigFromFiles($type, $files, ContainerBuilder $container) |
75
|
|
|
{ |
76
|
27 |
|
$typesConfig = []; |
77
|
|
|
/** @var SplFileInfo $file */ |
78
|
27 |
|
foreach ($files as $file) { |
79
|
27 |
|
$typesConfig = array_merge( |
80
|
27 |
|
$typesConfig, |
81
|
27 |
|
'yml' === $type ? $this->typesConfigFromYml($file, $container) : $this->typesConfigFromXml($file, $container) |
82
|
27 |
|
); |
83
|
27 |
|
} |
84
|
|
|
|
85
|
27 |
|
$container->prependExtensionConfig($this->getAlias(), $typesConfig); |
86
|
27 |
|
} |
87
|
|
|
|
88
|
1 |
|
private function typesConfigFromXml(SplFileInfo $file, ContainerBuilder $container) |
89
|
|
|
{ |
90
|
|
|
$typesConfig = []; |
91
|
|
|
|
92
|
|
|
try { |
93
|
|
|
//@todo fix xml validateSchema |
94
|
|
|
$xml = XmlUtils::loadFile($file->getRealPath()); |
95
|
|
|
foreach ($xml->documentElement->childNodes as $node) { |
96
|
|
|
if (!$node instanceof \DOMElement) { |
97
|
|
|
continue; |
98
|
|
|
} |
99
|
|
|
$values = XmlUtils::convertDomElementToArray($node); |
100
|
|
|
if (!is_array($values)) { |
101
|
1 |
|
continue; |
102
|
|
|
} |
103
|
|
|
$typesConfig = array_merge($typesConfig, $values); |
104
|
|
|
} |
105
|
|
|
$container->addResource(new FileResource($file->getRealPath())); |
106
|
|
|
} catch (\InvalidArgumentException $e) { |
107
|
|
|
throw new InvalidArgumentException(sprintf('Unable to parse file "%s".', $file), $e->getCode(), $e); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $typesConfig; |
111
|
|
|
} |
112
|
|
|
|
113
|
27 |
|
private function typesConfigFromYml(SplFileInfo $file, ContainerBuilder $container) |
114
|
|
|
{ |
115
|
27 |
|
if (null === $this->yamlParser) { |
116
|
27 |
|
$this->yamlParser = new YamlParser(); |
117
|
27 |
|
} |
118
|
|
|
|
119
|
|
|
try { |
120
|
27 |
|
$typesConfig = $this->yamlParser->parse($file->getContents()); |
121
|
27 |
|
$container->addResource(new FileResource($file->getRealPath())); |
122
|
27 |
|
} catch (ParseException $e) { |
123
|
|
|
throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e); |
124
|
|
|
} |
125
|
|
|
|
126
|
27 |
|
return $typesConfig; |
127
|
|
|
} |
128
|
|
|
|
129
|
31 |
|
private function typesConfigsMappingFromConfig(array $config, ContainerBuilder $container) |
130
|
|
|
{ |
131
|
31 |
|
$typesMappings = []; |
132
|
|
|
// from config |
133
|
31 |
|
if (!empty($config['definitions']['mappings']['types'])) { |
134
|
27 |
|
$typesMappings = array_filter(array_map( |
135
|
27 |
|
function (array $typeMapping) use ($container) { |
136
|
|
|
|
137
|
27 |
|
$params = $this->detectConfigFiles($container, $typeMapping['dir'], $typeMapping['type']); |
138
|
|
|
|
139
|
27 |
|
return $params; |
140
|
27 |
|
}, |
141
|
27 |
|
$config['definitions']['mappings']['types'] |
142
|
27 |
|
)); |
143
|
27 |
|
} |
144
|
|
|
|
145
|
31 |
|
return $typesMappings; |
146
|
|
|
} |
147
|
|
|
|
148
|
31 |
|
private function typesConfigsMappingFromBundles(ContainerBuilder $container) |
149
|
|
|
{ |
150
|
31 |
|
$typesMappings = []; |
151
|
31 |
|
$bundles = $container->getParameter('kernel.bundles'); |
152
|
|
|
|
153
|
|
|
// auto detect from bundle |
154
|
31 |
|
foreach ($bundles as $name => $class) { |
155
|
31 |
|
$bundle = new \ReflectionClass($class); |
156
|
31 |
|
$bundleDir = dirname($bundle->getFileName()); |
157
|
|
|
|
158
|
31 |
|
$configPath = $bundleDir.'/'.$this->getMappingResourceConfigDirectory(); |
159
|
31 |
|
$params = $this->detectConfigFiles($container, $configPath); |
160
|
|
|
|
161
|
31 |
|
if (null === $params) { |
162
|
31 |
|
continue; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$typesMappings[] = $params; |
166
|
31 |
|
} |
167
|
|
|
|
168
|
31 |
|
return $typesMappings; |
169
|
|
|
} |
170
|
|
|
|
171
|
31 |
|
private function detectConfigFiles(ContainerBuilder $container, $configPath, $type = null) |
172
|
|
|
{ |
173
|
|
|
// add the closest existing directory as a resource |
174
|
31 |
|
$resource = $configPath; |
175
|
31 |
|
while (!is_dir($resource)) { |
176
|
31 |
|
$resource = dirname($resource); |
177
|
31 |
|
} |
178
|
31 |
|
$container->addResource(new FileResource($resource)); |
179
|
|
|
|
180
|
31 |
|
$extension = $this->getMappingResourceExtension(); |
181
|
31 |
|
$finder = new Finder(); |
182
|
|
|
|
183
|
31 |
|
$types = null === $type ? ['yml', 'xml'] : [$type]; |
184
|
|
|
|
185
|
31 |
|
foreach ($types as $type) { |
186
|
|
|
try { |
187
|
31 |
|
$finder->files()->in($configPath)->name('*.'.$extension.'.'.$type); |
188
|
31 |
|
} catch (\InvalidArgumentException $e) { |
189
|
31 |
|
continue; |
190
|
|
|
} |
191
|
27 |
|
if (0 === $finder->count()) { |
192
|
|
|
continue; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return [ |
196
|
27 |
|
'type' => $type, |
197
|
27 |
|
'files' => $finder, |
198
|
27 |
|
]; |
199
|
31 |
|
} |
200
|
|
|
|
201
|
31 |
|
return; |
202
|
|
|
} |
203
|
|
|
|
204
|
31 |
|
private function getMappingResourceConfigDirectory() |
205
|
|
|
{ |
206
|
31 |
|
return 'Resources/config/graphql'; |
207
|
|
|
} |
208
|
|
|
|
209
|
31 |
|
private function getMappingResourceExtension() |
210
|
|
|
{ |
211
|
31 |
|
return 'types'; |
212
|
|
|
} |
213
|
|
|
|
214
|
31 |
|
public function getAliasPrefix() |
215
|
|
|
{ |
216
|
31 |
|
return 'overblog_graphql'; |
217
|
|
|
} |
218
|
|
|
|
219
|
31 |
|
public function getAlias() |
220
|
|
|
{ |
221
|
31 |
|
return $this->getAliasPrefix().'_types'; |
222
|
|
|
} |
223
|
|
|
|
224
|
27 |
|
public function getConfiguration(array $config, ContainerBuilder $container) |
225
|
|
|
{ |
226
|
27 |
|
return new TypesConfiguration(); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|