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