1111
1212namespace Overblog \GraphQLBundle \DependencyInjection ;
1313
14+ use Overblog \GraphQLBundle \OverblogGraphQLBundle ;
1415use Symfony \Component \Config \Resource \FileResource ;
1516use Symfony \Component \DependencyInjection \ContainerBuilder ;
1617use Symfony \Component \Finder \Finder ;
@@ -23,6 +24,20 @@ class OverblogGraphQLTypesExtension extends Extension
2324
2425 private static $ typeExtensions = ['yaml ' => '{yaml,yml} ' , 'xml ' => 'xml ' ];
2526
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+
2641 public function load (array $ configs , ContainerBuilder $ container )
2742 {
2843 $ configuration = $ this ->getConfiguration ($ configs , $ container );
@@ -59,27 +74,48 @@ private function prependExtensionConfigFromFiles($type, $files, ContainerBuilder
5974
6075 private function mappingConfig (array $ config , ContainerBuilder $ container )
6176 {
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 ' ];
6382
6483 // 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 ' )) {
6685 $ typesMappings [] = ['dir ' => $ container ->getParameter ('kernel.root_dir ' ).'/config/graphql ' , 'type ' => null ];
6786 }
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+ }
7197
7298 // 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 (
74107 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+ );
76114
77115 return $ params ;
78116 },
79117 $ typesMappings
80118 ));
81-
82- return $ typesMappings ;
83119 }
84120
85121 private function mappingFromBundles (ContainerBuilder $ container )
@@ -89,8 +125,7 @@ private function mappingFromBundles(ContainerBuilder $container)
89125
90126 // auto detect from bundle
91127 foreach ($ bundles as $ name => $ class ) {
92- $ bundle = new \ReflectionClass ($ class );
93- $ bundleDir = dirname ($ bundle ->getFileName ());
128+ $ bundleDir = $ this ->bundleDir ($ class );
94129
95130 // only config files (yml or xml)
96131 $ typesMappings [] = ['dir ' => $ bundleDir .'/Resources/config/graphql ' , 'type ' => null ];
@@ -99,7 +134,7 @@ private function mappingFromBundles(ContainerBuilder $container)
99134 return $ typesMappings ;
100135 }
101136
102- private function detectFilesByType (ContainerBuilder $ container , $ path , $ type = null )
137+ private function detectFilesByType (ContainerBuilder $ container , $ path , $ type, $ suffix )
103138 {
104139 // add the closest existing directory as a resource
105140 $ resource = $ path ;
@@ -114,7 +149,7 @@ private function detectFilesByType(ContainerBuilder $container, $path, $type = n
114149
115150 foreach ($ types as $ type ) {
116151 try {
117- $ finder ->files ()->in ($ path )->name ('*.types . ' .self ::$ typeExtensions [$ type ]);
152+ $ finder ->files ()->in ($ path )->name ('* ' . $ suffix . ' . ' .self ::$ typeExtensions [$ type ]);
118153 } catch (\InvalidArgumentException $ e ) {
119154 continue ;
120155 }
@@ -129,6 +164,14 @@ private function detectFilesByType(ContainerBuilder $container, $path, $type = n
129164 return ;
130165 }
131166
167+ private function bundleDir ($ bundleClass )
168+ {
169+ $ bundle = new \ReflectionClass ($ bundleClass );
170+ $ bundleDir = dirname ($ bundle ->getFileName ());
171+
172+ return $ bundleDir ;
173+ }
174+
132175 public function getAliasPrefix ()
133176 {
134177 return 'overblog_graphql ' ;
0 commit comments