11
11
12
12
namespace Overblog \GraphQLBundle \DependencyInjection ;
13
13
14
+ use Overblog \GraphQLBundle \OverblogGraphQLBundle ;
14
15
use Symfony \Component \Config \Resource \FileResource ;
15
16
use Symfony \Component \DependencyInjection \ContainerBuilder ;
16
17
use Symfony \Component \Finder \Finder ;
@@ -23,6 +24,20 @@ class OverblogGraphQLTypesExtension extends Extension
23
24
24
25
private static $ typeExtensions = ['yaml ' => '{yaml,yml} ' , 'xml ' => 'xml ' ];
25
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
+
26
41
public function load (array $ configs , ContainerBuilder $ container )
27
42
{
28
43
$ configuration = $ this ->getConfiguration ($ configs , $ container );
@@ -59,27 +74,48 @@ private function prependExtensionConfigFromFiles($type, $files, ContainerBuilder
59
74
60
75
private function mappingConfig (array $ config , ContainerBuilder $ container )
61
76
{
62
- $ typesMappings = empty ($ config ['definitions ' ]['mappings ' ]['types ' ]) ? [] : $ config ['definitions ' ]['mappings ' ]['types ' ];
77
+ // use default value if needed
78
+ $ config = array_replace_recursive (self ::$ defaultDefaultConfig , $ config );
79
+
80
+ $ mappingConfig = $ config ['definitions ' ]['mappings ' ];
81
+ $ typesMappings = $ mappingConfig ['types ' ];
63
82
64
83
// app only config files (yml or xml)
65
- if ($ container ->hasParameter ('kernel.root_dir ' )) {
84
+ if ($ mappingConfig [ ' auto_discover ' ][ ' root_dir ' ] && $ container ->hasParameter ('kernel.root_dir ' )) {
66
85
$ typesMappings [] = ['dir ' => $ container ->getParameter ('kernel.root_dir ' ).'/config/graphql ' , 'type ' => null ];
67
86
}
68
-
69
- $ mappingFromBundles = $ this ->mappingFromBundles ($ container );
70
- $ typesMappings = array_merge ($ typesMappings , $ mappingFromBundles );
87
+ if ($ mappingConfig ['auto_discover ' ]['bundles ' ]) {
88
+ $ mappingFromBundles = $ this ->mappingFromBundles ($ container );
89
+ $ typesMappings = array_merge ($ typesMappings , $ mappingFromBundles );
90
+ } else {
91
+ // enabled only for this bundle
92
+ $ typesMappings [] = [
93
+ 'dir ' => $ this ->bundleDir (OverblogGraphQLBundle::class).'/Resources/config/graphql ' ,
94
+ 'type ' => 'yaml ' ,
95
+ ];
96
+ }
71
97
72
98
// from config
73
- $ typesMappings = array_filter (array_map (
99
+ $ typesMappings = $ this ->detectFilesFromTypesMappings ($ typesMappings , $ container );
100
+
101
+ return $ typesMappings ;
102
+ }
103
+
104
+ private function detectFilesFromTypesMappings (array $ typesMappings , ContainerBuilder $ container )
105
+ {
106
+ return array_filter (array_map (
74
107
function (array $ typeMapping ) use ($ container ) {
75
- $ params = $ this ->detectFilesByType ($ container , $ typeMapping ['dir ' ], $ typeMapping ['type ' ]);
108
+ $ params = $ this ->detectFilesByType (
109
+ $ container ,
110
+ $ typeMapping ['dir ' ],
111
+ $ typeMapping ['type ' ],
112
+ isset ($ typeMapping ['suffix ' ]) ? $ typeMapping ['suffix ' ] : ''
113
+ );
76
114
77
115
return $ params ;
78
116
},
79
117
$ typesMappings
80
118
));
81
-
82
- return $ typesMappings ;
83
119
}
84
120
85
121
private function mappingFromBundles (ContainerBuilder $ container )
@@ -89,8 +125,7 @@ private function mappingFromBundles(ContainerBuilder $container)
89
125
90
126
// auto detect from bundle
91
127
foreach ($ bundles as $ name => $ class ) {
92
- $ bundle = new \ReflectionClass ($ class );
93
- $ bundleDir = dirname ($ bundle ->getFileName ());
128
+ $ bundleDir = $ this ->bundleDir ($ class );
94
129
95
130
// only config files (yml or xml)
96
131
$ typesMappings [] = ['dir ' => $ bundleDir .'/Resources/config/graphql ' , 'type ' => null ];
@@ -99,7 +134,7 @@ private function mappingFromBundles(ContainerBuilder $container)
99
134
return $ typesMappings ;
100
135
}
101
136
102
- private function detectFilesByType (ContainerBuilder $ container , $ path , $ type = null )
137
+ private function detectFilesByType (ContainerBuilder $ container , $ path , $ type, $ suffix )
103
138
{
104
139
// add the closest existing directory as a resource
105
140
$ resource = $ path ;
@@ -114,7 +149,7 @@ private function detectFilesByType(ContainerBuilder $container, $path, $type = n
114
149
115
150
foreach ($ types as $ type ) {
116
151
try {
117
- $ finder ->files ()->in ($ path )->name ('*.types . ' .self ::$ typeExtensions [$ type ]);
152
+ $ finder ->files ()->in ($ path )->name ('* ' . $ suffix . ' . ' .self ::$ typeExtensions [$ type ]);
118
153
} catch (\InvalidArgumentException $ e ) {
119
154
continue ;
120
155
}
@@ -129,6 +164,14 @@ private function detectFilesByType(ContainerBuilder $container, $path, $type = n
129
164
return ;
130
165
}
131
166
167
+ private function bundleDir ($ bundleClass )
168
+ {
169
+ $ bundle = new \ReflectionClass ($ bundleClass );
170
+ $ bundleDir = dirname ($ bundle ->getFileName ());
171
+
172
+ return $ bundleDir ;
173
+ }
174
+
132
175
public function getAliasPrefix ()
133
176
{
134
177
return 'overblog_graphql ' ;
0 commit comments