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