Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
69 changes: 56 additions & 13 deletions DependencyInjection/OverblogGraphQLTypesExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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];
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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';
Expand Down
3 changes: 3 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
parameters:
overblog_graphql_types.config: []

services:
overblog_graphql.error_handler:
class: Overblog\GraphQLBundle\Error\ErrorHandler
Expand Down
4 changes: 3 additions & 1 deletion Resources/doc/definitions/type-system/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
2 changes: 2 additions & 0 deletions Tests/Functional/App/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ overblog_graphql:
definitions:
config_validation: true
auto_mapping: false
mappings:
auto_discover: false

services:
#disable twig error pages
Expand Down
3 changes: 2 additions & 1 deletion Tests/Functional/App/config/node/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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