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\OverblogGraphQLBundle; |
15
|
|
|
use Symfony\Component\Config\Resource\FileResource; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
17
|
|
|
use Symfony\Component\Finder\Finder; |
18
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
19
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
20
|
|
|
|
21
|
|
|
class OverblogGraphQLTypesExtension extends Extension |
22
|
|
|
{ |
23
|
|
|
private static $configTypes = ['yaml', 'xml']; |
24
|
|
|
|
25
|
|
|
private static $typeExtensions = ['yaml' => '{yaml,yml}', 'xml' => 'xml']; |
26
|
|
|
|
27
|
|
|
private static $defaultDefaultConfig = [ |
28
|
|
|
'definitions' => [ |
29
|
|
|
'mappings' => [ |
30
|
|
|
'auto_discover' => [ |
31
|
|
|
'root_dir' => true, |
32
|
|
|
'bundles' => true, |
33
|
|
|
], |
34
|
|
|
'types' => [], |
35
|
|
|
], |
36
|
|
|
], |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
const DEFAULT_TYPES_SUFFIX = '.types'; |
40
|
|
|
|
41
|
25 |
|
public function load(array $configs, ContainerBuilder $container) |
42
|
|
|
{ |
43
|
25 |
|
$configuration = $this->getConfiguration($configs, $container); |
44
|
25 |
|
$config = $this->processConfiguration($configuration, $configs); |
|
|
|
|
45
|
|
|
|
46
|
19 |
|
$container->setParameter($this->getAlias().'.config', $config); |
47
|
19 |
|
} |
48
|
|
|
|
49
|
20 |
|
public function containerPrependExtensionConfig(array $config, ContainerBuilder $container) |
50
|
|
|
{ |
51
|
20 |
|
$typesMappings = $this->mappingConfig($config, $container); |
52
|
|
|
|
53
|
|
|
// treats mappings |
54
|
20 |
|
foreach ($typesMappings as $params) { |
55
|
20 |
|
$this->prependExtensionConfigFromFiles($params['type'], $params['files'], $container); |
56
|
|
|
} |
57
|
18 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param $type |
61
|
|
|
* @param SplFileInfo[] $files |
62
|
|
|
* @param ContainerBuilder $container |
63
|
|
|
*/ |
64
|
20 |
|
private function prependExtensionConfigFromFiles($type, $files, ContainerBuilder $container) |
65
|
|
|
{ |
66
|
|
|
/** @var SplFileInfo $file */ |
67
|
20 |
|
foreach ($files as $file) { |
68
|
20 |
|
$parserClass = sprintf('Overblog\\GraphQLBundle\\Config\\Parser\\%sParser', ucfirst($type)); |
69
|
|
|
|
70
|
20 |
|
$typeConfig = call_user_func($parserClass.'::parse', $file, $container); |
71
|
18 |
|
$container->prependExtensionConfig($this->getAlias(), $typeConfig); |
72
|
|
|
} |
73
|
18 |
|
} |
74
|
|
|
|
75
|
20 |
|
private function mappingConfig(array $config, ContainerBuilder $container) |
76
|
|
|
{ |
77
|
|
|
// use default value if needed |
78
|
20 |
|
$config = array_replace_recursive(self::$defaultDefaultConfig, $config); |
79
|
|
|
|
80
|
20 |
|
$mappingConfig = $config['definitions']['mappings']; |
81
|
20 |
|
$typesMappings = $mappingConfig['types']; |
82
|
|
|
|
83
|
|
|
// app only config files (yml or xml) |
84
|
20 |
|
if ($mappingConfig['auto_discover']['root_dir'] && $container->hasParameter('kernel.root_dir')) { |
85
|
1 |
|
$typesMappings[] = ['dir' => $container->getParameter('kernel.root_dir').'/config/graphql', 'type' => null]; |
86
|
|
|
} |
87
|
20 |
|
if ($mappingConfig['auto_discover']['bundles']) { |
88
|
3 |
|
$mappingFromBundles = $this->mappingFromBundles($container); |
89
|
3 |
|
$typesMappings = array_merge($typesMappings, $mappingFromBundles); |
90
|
|
|
} else { |
91
|
|
|
// enabled only for this bundle |
92
|
17 |
|
$typesMappings[] = [ |
93
|
17 |
|
'dir' => $this->bundleDir(OverblogGraphQLBundle::class).'/Resources/config/graphql', |
94
|
17 |
|
'type' => 'yaml', |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// from config |
99
|
20 |
|
$typesMappings = $this->detectFilesFromTypesMappings($typesMappings, $container); |
100
|
|
|
|
101
|
20 |
|
return $typesMappings; |
102
|
|
|
} |
103
|
|
|
|
104
|
20 |
|
private function detectFilesFromTypesMappings(array $typesMappings, ContainerBuilder $container) |
105
|
|
|
{ |
106
|
20 |
|
return array_filter(array_map( |
107
|
20 |
|
function (array $typeMapping) use ($container) { |
108
|
20 |
|
$params = $this->detectFilesByType( |
109
|
20 |
|
$container, |
110
|
20 |
|
$typeMapping['dir'], |
111
|
20 |
|
$typeMapping['type'], |
112
|
20 |
|
isset($typeMapping['suffix']) ? $typeMapping['suffix'] : '' |
113
|
|
|
); |
114
|
|
|
|
115
|
20 |
|
return $params; |
116
|
20 |
|
}, |
117
|
20 |
|
$typesMappings |
118
|
|
|
)); |
119
|
|
|
} |
120
|
|
|
|
121
|
3 |
|
private function mappingFromBundles(ContainerBuilder $container) |
122
|
|
|
{ |
123
|
3 |
|
$typesMappings = []; |
124
|
3 |
|
$bundles = $container->getParameter('kernel.bundles'); |
125
|
|
|
|
126
|
|
|
// auto detect from bundle |
127
|
3 |
|
foreach ($bundles as $name => $class) { |
128
|
1 |
|
$bundleDir = $this->bundleDir($class); |
129
|
|
|
|
130
|
|
|
// only config files (yml or xml) |
131
|
1 |
|
$typesMappings[] = ['dir' => $bundleDir.'/Resources/config/graphql', 'type' => null]; |
132
|
|
|
} |
133
|
|
|
|
134
|
3 |
|
return $typesMappings; |
135
|
|
|
} |
136
|
|
|
|
137
|
20 |
|
private function detectFilesByType(ContainerBuilder $container, $path, $type, $suffix) |
138
|
|
|
{ |
139
|
|
|
// add the closest existing directory as a resource |
140
|
20 |
|
$resource = $path; |
141
|
20 |
|
while (!is_dir($resource)) { |
142
|
1 |
|
$resource = dirname($resource); |
143
|
|
|
} |
144
|
20 |
|
$container->addResource(new FileResource($resource)); |
145
|
|
|
|
146
|
20 |
|
$finder = new Finder(); |
147
|
|
|
|
148
|
20 |
|
$types = null === $type ? self::$configTypes : [$type]; |
149
|
|
|
|
150
|
20 |
|
foreach ($types as $type) { |
151
|
|
|
try { |
152
|
20 |
|
$finder->files()->in($path)->name('*'.$suffix.'.'.self::$typeExtensions[$type]); |
153
|
1 |
|
} catch (\InvalidArgumentException $e) { |
154
|
1 |
|
continue; |
155
|
|
|
} |
156
|
20 |
|
if ($finder->count() > 0) { |
157
|
|
|
return [ |
158
|
20 |
|
'type' => $type, |
159
|
20 |
|
'files' => $finder, |
160
|
|
|
]; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
return; |
165
|
|
|
} |
166
|
|
|
|
167
|
18 |
|
private function bundleDir($bundleClass) |
168
|
|
|
{ |
169
|
18 |
|
$bundle = new \ReflectionClass($bundleClass); |
170
|
18 |
|
$bundleDir = dirname($bundle->getFileName()); |
171
|
|
|
|
172
|
18 |
|
return $bundleDir; |
173
|
|
|
} |
174
|
|
|
|
175
|
19 |
|
public function getAliasPrefix() |
176
|
|
|
{ |
177
|
19 |
|
return 'overblog_graphql'; |
178
|
|
|
} |
179
|
|
|
|
180
|
19 |
|
public function getAlias() |
181
|
|
|
{ |
182
|
19 |
|
return $this->getAliasPrefix().'_types'; |
183
|
|
|
} |
184
|
|
|
|
185
|
25 |
|
public function getConfiguration(array $config, ContainerBuilder $container) |
186
|
|
|
{ |
187
|
25 |
|
return new TypesConfiguration(); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: