Skip to content

Commit 58ee021

Browse files
authored
Merge pull request #200 from mcg-web/auto_discover_settings
Optimize types config files auto discover configuration
2 parents 8b005d1 + 4d950af commit 58ee021

File tree

10 files changed

+78
-16
lines changed

10 files changed

+78
-16
lines changed

DependencyInjection/Configuration.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ public function getConfigTreeBuilder()
9595
->end()
9696
->arrayNode('mappings')
9797
->children()
98+
->arrayNode('auto_discover')
99+
->treatFalseLike(['bundles' => false, 'root_dir' => false])
100+
->treatTrueLike(['bundles' => true, 'root_dir' => true])
101+
->treatNullLike(['bundles' => true, 'root_dir' => true])
102+
->addDefaultsIfNotSet()
103+
->children()
104+
->booleanNode('bundles')->defaultTrue()->end()
105+
->booleanNode('root_dir')->defaultTrue()->end()
106+
->end()
107+
->end()
98108
->arrayNode('types')
99109
->prototype('array')
100110
->addDefaultsIfNotSet()
@@ -109,8 +119,9 @@ public function getConfigTreeBuilder()
109119
})
110120
->end()
111121
->children()
112-
->enumNode('type')->isRequired()->values(['yaml', 'xml'])->end()
122+
->enumNode('type')->values(['yaml', 'xml'])->defaultNull()->end()
113123
->scalarNode('dir')->defaultNull()->end()
124+
->scalarNode('suffix')->defaultValue(OverblogGraphQLTypesExtension::DEFAULT_TYPES_SUFFIX)->end()
114125
->end()
115126
->end()
116127
->end()

DependencyInjection/OverblogGraphQLTypesExtension.php

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Overblog\GraphQLBundle\DependencyInjection;
1313

14+
use Overblog\GraphQLBundle\OverblogGraphQLBundle;
1415
use Symfony\Component\Config\Resource\FileResource;
1516
use Symfony\Component\DependencyInjection\ContainerBuilder;
1617
use 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';

Resources/config/services.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
parameters:
2+
overblog_graphql_types.config: []
3+
14
services:
25
overblog_graphql.error_handler:
36
class: Overblog\GraphQLBundle\Error\ErrorHandler

Resources/doc/definitions/type-system/index.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ Types can be define 3 different ways:
2525
overblog_graphql:
2626
definitions:
2727
mappings:
28+
# auto_discover: false # to disable bundles and root dir auto discover
2829
types:
2930
-
30-
type: yaml # or xml
31+
type: yaml # or xml or null
3132
dir: "%kernel.root_dir%/.../mapping"
33+
# suffix: .types # use to change default file suffix
3234
```
3335

3436
2. **The PHP way**

Tests/Functional/App/config/config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ overblog_graphql:
1212
definitions:
1313
config_validation: true
1414
auto_mapping: false
15+
mappings:
16+
auto_discover: false
1517

1618
services:
1719
#disable twig error pages

Tests/Functional/App/config/node/config.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ overblog_graphql:
1818
query: Query
1919
mutation: ~
2020
mappings:
21+
auto_discover: true
2122
types:
2223
-
23-
type: yaml
2424
dir: "%kernel.root_dir%/config/node/mapping"
25+
suffix: _type
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)