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