From 4d950afe0576453f20242b98184640e8e9c55f72 Mon Sep 17 00:00:00 2001 From: Jeremiah VALERIE Date: Tue, 26 Sep 2017 18:07:36 +0200 Subject: [PATCH] Optimize types config files auto discover configuration --- DependencyInjection/Configuration.php | 13 +++- .../OverblogGraphQLTypesExtension.php | 69 +++++++++++++++---- Resources/config/services.yml | 3 + .../doc/definitions/type-system/index.md | 4 +- Tests/Functional/App/config/config.yml | 2 + Tests/Functional/App/config/node/config.yml | 3 +- .../mapping/{Node.types.yml => node_type.yml} | 0 .../{Photo.types.yml => photo_type.yml} | 0 .../{Query.types.yml => query_type.yml} | 0 .../mapping/{User.types.yml => user_type.yml} | 0 10 files changed, 78 insertions(+), 16 deletions(-) rename Tests/Functional/App/config/node/mapping/{Node.types.yml => node_type.yml} (100%) rename Tests/Functional/App/config/node/mapping/{Photo.types.yml => photo_type.yml} (100%) rename Tests/Functional/App/config/node/mapping/{Query.types.yml => query_type.yml} (100%) rename Tests/Functional/App/config/node/mapping/{User.types.yml => user_type.yml} (100%) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 973e831ff..c466d91b0 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -95,6 +95,16 @@ public function getConfigTreeBuilder() ->end() ->arrayNode('mappings') ->children() + ->arrayNode('auto_discover') + ->treatFalseLike(['bundles' => false, 'root_dir' => false]) + ->treatTrueLike(['bundles' => true, 'root_dir' => true]) + ->treatNullLike(['bundles' => true, 'root_dir' => true]) + ->addDefaultsIfNotSet() + ->children() + ->booleanNode('bundles')->defaultTrue()->end() + ->booleanNode('root_dir')->defaultTrue()->end() + ->end() + ->end() ->arrayNode('types') ->prototype('array') ->addDefaultsIfNotSet() @@ -109,8 +119,9 @@ public function getConfigTreeBuilder() }) ->end() ->children() - ->enumNode('type')->isRequired()->values(['yaml', 'xml'])->end() + ->enumNode('type')->values(['yaml', 'xml'])->defaultNull()->end() ->scalarNode('dir')->defaultNull()->end() + ->scalarNode('suffix')->defaultValue(OverblogGraphQLTypesExtension::DEFAULT_TYPES_SUFFIX)->end() ->end() ->end() ->end() diff --git a/DependencyInjection/OverblogGraphQLTypesExtension.php b/DependencyInjection/OverblogGraphQLTypesExtension.php index 2eff2930e..8801745ca 100644 --- a/DependencyInjection/OverblogGraphQLTypesExtension.php +++ b/DependencyInjection/OverblogGraphQLTypesExtension.php @@ -11,6 +11,7 @@ namespace Overblog\GraphQLBundle\DependencyInjection; +use Overblog\GraphQLBundle\OverblogGraphQLBundle; use Symfony\Component\Config\Resource\FileResource; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\Finder\Finder; @@ -23,6 +24,20 @@ class OverblogGraphQLTypesExtension extends Extension private static $typeExtensions = ['yaml' => '{yaml,yml}', 'xml' => 'xml']; + private static $defaultDefaultConfig = [ + 'definitions' => [ + 'mappings' => [ + 'auto_discover' => [ + 'root_dir' => true, + 'bundles' => true, + ], + 'types' => [], + ], + ], + ]; + + const DEFAULT_TYPES_SUFFIX = '.types'; + public function load(array $configs, ContainerBuilder $container) { $configuration = $this->getConfiguration($configs, $container); @@ -59,27 +74,48 @@ private function prependExtensionConfigFromFiles($type, $files, ContainerBuilder private function mappingConfig(array $config, ContainerBuilder $container) { - $typesMappings = empty($config['definitions']['mappings']['types']) ? [] : $config['definitions']['mappings']['types']; + // use default value if needed + $config = array_replace_recursive(self::$defaultDefaultConfig, $config); + + $mappingConfig = $config['definitions']['mappings']; + $typesMappings = $mappingConfig['types']; // app only config files (yml or xml) - if ($container->hasParameter('kernel.root_dir')) { + if ($mappingConfig['auto_discover']['root_dir'] && $container->hasParameter('kernel.root_dir')) { $typesMappings[] = ['dir' => $container->getParameter('kernel.root_dir').'/config/graphql', 'type' => null]; } - - $mappingFromBundles = $this->mappingFromBundles($container); - $typesMappings = array_merge($typesMappings, $mappingFromBundles); + if ($mappingConfig['auto_discover']['bundles']) { + $mappingFromBundles = $this->mappingFromBundles($container); + $typesMappings = array_merge($typesMappings, $mappingFromBundles); + } else { + // enabled only for this bundle + $typesMappings[] = [ + 'dir' => $this->bundleDir(OverblogGraphQLBundle::class).'/Resources/config/graphql', + 'type' => 'yaml', + ]; + } // from config - $typesMappings = array_filter(array_map( + $typesMappings = $this->detectFilesFromTypesMappings($typesMappings, $container); + + return $typesMappings; + } + + private function detectFilesFromTypesMappings(array $typesMappings, ContainerBuilder $container) + { + return array_filter(array_map( function (array $typeMapping) use ($container) { - $params = $this->detectFilesByType($container, $typeMapping['dir'], $typeMapping['type']); + $params = $this->detectFilesByType( + $container, + $typeMapping['dir'], + $typeMapping['type'], + isset($typeMapping['suffix']) ? $typeMapping['suffix'] : '' + ); return $params; }, $typesMappings )); - - return $typesMappings; } private function mappingFromBundles(ContainerBuilder $container) @@ -89,8 +125,7 @@ private function mappingFromBundles(ContainerBuilder $container) // auto detect from bundle foreach ($bundles as $name => $class) { - $bundle = new \ReflectionClass($class); - $bundleDir = dirname($bundle->getFileName()); + $bundleDir = $this->bundleDir($class); // only config files (yml or xml) $typesMappings[] = ['dir' => $bundleDir.'/Resources/config/graphql', 'type' => null]; @@ -99,7 +134,7 @@ private function mappingFromBundles(ContainerBuilder $container) return $typesMappings; } - private function detectFilesByType(ContainerBuilder $container, $path, $type = null) + private function detectFilesByType(ContainerBuilder $container, $path, $type, $suffix) { // add the closest existing directory as a resource $resource = $path; @@ -114,7 +149,7 @@ private function detectFilesByType(ContainerBuilder $container, $path, $type = n foreach ($types as $type) { try { - $finder->files()->in($path)->name('*.types.'.self::$typeExtensions[$type]); + $finder->files()->in($path)->name('*'.$suffix.'.'.self::$typeExtensions[$type]); } catch (\InvalidArgumentException $e) { continue; } @@ -129,6 +164,14 @@ private function detectFilesByType(ContainerBuilder $container, $path, $type = n return; } + private function bundleDir($bundleClass) + { + $bundle = new \ReflectionClass($bundleClass); + $bundleDir = dirname($bundle->getFileName()); + + return $bundleDir; + } + public function getAliasPrefix() { return 'overblog_graphql'; diff --git a/Resources/config/services.yml b/Resources/config/services.yml index db8b22c16..7c491d30f 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -1,3 +1,6 @@ +parameters: + overblog_graphql_types.config: [] + services: overblog_graphql.error_handler: class: Overblog\GraphQLBundle\Error\ErrorHandler diff --git a/Resources/doc/definitions/type-system/index.md b/Resources/doc/definitions/type-system/index.md index 76a4b1016..077ff499f 100644 --- a/Resources/doc/definitions/type-system/index.md +++ b/Resources/doc/definitions/type-system/index.md @@ -25,10 +25,12 @@ Types can be define 3 different ways: overblog_graphql: definitions: mappings: + # auto_discover: false # to disable bundles and root dir auto discover types: - - type: yaml # or xml + type: yaml # or xml or null dir: "%kernel.root_dir%/.../mapping" + # suffix: .types # use to change default file suffix ``` 2. **The PHP way** diff --git a/Tests/Functional/App/config/config.yml b/Tests/Functional/App/config/config.yml index 38498707b..f12ca148c 100644 --- a/Tests/Functional/App/config/config.yml +++ b/Tests/Functional/App/config/config.yml @@ -12,6 +12,8 @@ overblog_graphql: definitions: config_validation: true auto_mapping: false + mappings: + auto_discover: false services: #disable twig error pages diff --git a/Tests/Functional/App/config/node/config.yml b/Tests/Functional/App/config/node/config.yml index 3ef301b6d..c48e66107 100644 --- a/Tests/Functional/App/config/node/config.yml +++ b/Tests/Functional/App/config/node/config.yml @@ -18,7 +18,8 @@ overblog_graphql: query: Query mutation: ~ mappings: + auto_discover: true types: - - type: yaml dir: "%kernel.root_dir%/config/node/mapping" + suffix: _type diff --git a/Tests/Functional/App/config/node/mapping/Node.types.yml b/Tests/Functional/App/config/node/mapping/node_type.yml similarity index 100% rename from Tests/Functional/App/config/node/mapping/Node.types.yml rename to Tests/Functional/App/config/node/mapping/node_type.yml diff --git a/Tests/Functional/App/config/node/mapping/Photo.types.yml b/Tests/Functional/App/config/node/mapping/photo_type.yml similarity index 100% rename from Tests/Functional/App/config/node/mapping/Photo.types.yml rename to Tests/Functional/App/config/node/mapping/photo_type.yml diff --git a/Tests/Functional/App/config/node/mapping/Query.types.yml b/Tests/Functional/App/config/node/mapping/query_type.yml similarity index 100% rename from Tests/Functional/App/config/node/mapping/Query.types.yml rename to Tests/Functional/App/config/node/mapping/query_type.yml diff --git a/Tests/Functional/App/config/node/mapping/User.types.yml b/Tests/Functional/App/config/node/mapping/user_type.yml similarity index 100% rename from Tests/Functional/App/config/node/mapping/User.types.yml rename to Tests/Functional/App/config/node/mapping/user_type.yml