14
14
use GraphQL \Language \AST \ScalarTypeDefinitionNode ;
15
15
use GraphQL \Language \AST \UnionTypeDefinitionNode ;
16
16
use GraphQL \Language \Parser ;
17
+ use Overblog \GraphQLBundle \Config \Parser \GraphQL \ASTConverter \CustomScalarNode ;
18
+ use Overblog \GraphQLBundle \Config \Parser \GraphQL \ASTConverter \EnumNode ;
19
+ use Overblog \GraphQLBundle \Config \Parser \GraphQL \ASTConverter \InputObjectNode ;
20
+ use Overblog \GraphQLBundle \Config \Parser \GraphQL \ASTConverter \InterfaceNode ;
17
21
use Overblog \GraphQLBundle \Config \Parser \GraphQL \ASTConverter \NodeInterface ;
22
+ use Overblog \GraphQLBundle \Config \Parser \GraphQL \ASTConverter \ObjectNode ;
23
+ use Overblog \GraphQLBundle \Config \Parser \GraphQL \ASTConverter \UnionNode ;
18
24
use SplFileInfo ;
19
25
use Symfony \Component \Config \Resource \FileResource ;
20
26
use Symfony \Component \DependencyInjection \ContainerBuilder ;
21
27
use Symfony \Component \DependencyInjection \Exception \InvalidArgumentException ;
28
+
22
29
use function array_pop ;
23
- use function call_user_func ;
24
30
use function explode ;
25
31
use function file_get_contents ;
26
- use function get_class ;
27
32
use function preg_replace ;
28
33
use function sprintf ;
29
34
use function trim ;
30
- use function ucfirst ;
31
35
32
36
class GraphQLParser implements ParserInterface
33
37
{
34
- private const DEFINITION_TYPE_MAPPING = [
35
- NodeKind::OBJECT_TYPE_DEFINITION => ' object ' ,
36
- NodeKind::INTERFACE_TYPE_DEFINITION => ' interface ' ,
37
- NodeKind::ENUM_TYPE_DEFINITION => ' enum ' ,
38
- NodeKind::UNION_TYPE_DEFINITION => ' union ' ,
39
- NodeKind::INPUT_OBJECT_TYPE_DEFINITION => ' inputObject ' ,
40
- NodeKind::SCALAR_TYPE_DEFINITION => ' customScalar ' ,
38
+ protected const DEFINITION_TYPE_MAPPING = [
39
+ NodeKind::OBJECT_TYPE_DEFINITION => ObjectNode::class ,
40
+ NodeKind::INTERFACE_TYPE_DEFINITION => InterfaceNode::class ,
41
+ NodeKind::ENUM_TYPE_DEFINITION => EnumNode::class ,
42
+ NodeKind::UNION_TYPE_DEFINITION => UnionNode::class ,
43
+ NodeKind::INPUT_OBJECT_TYPE_DEFINITION => InputObjectNode::class ,
44
+ NodeKind::SCALAR_TYPE_DEFINITION => CustomScalarNode::class ,
41
45
];
42
46
43
47
public static function parse (SplFileInfo $ file , ContainerBuilder $ container , array $ configs = []): array
@@ -67,19 +71,28 @@ public static function parse(SplFileInfo $file, ContainerBuilder $container, arr
67
71
return $ typesConfig ;
68
72
}
69
73
74
+ protected static function createUnsupportedDefinitionNodeException (DefinitionNode $ typeDef ): InvalidArgumentException
75
+ {
76
+ $ path = explode ('\\' , \get_class ($ typeDef ));
77
+
78
+ return new InvalidArgumentException (
79
+ sprintf (
80
+ '%s definition is not supported right now. ' ,
81
+ preg_replace ('@DefinitionNode$@ ' , '' , array_pop ($ path ))
82
+ )
83
+ );
84
+ }
85
+
70
86
/**
71
87
* @return class-string<NodeInterface>
72
88
*/
73
89
protected static function getNodeClass (DefinitionNode $ typeDef ): string
74
90
{
75
- if (isset ($ typeDef ->kind ) && array_key_exists ($ typeDef ->kind , self ::DEFINITION_TYPE_MAPPING )) {
76
- /**
77
- * @var class-string<NodeInterface> $class
78
- */
79
- return sprintf ('\\%s \\GraphQL \\ASTConverter \\%sNode ' , __NAMESPACE__ , ucfirst (self ::DEFINITION_TYPE_MAPPING [$ typeDef ->kind ]));
91
+ if (isset ($ typeDef ->kind ) && \array_key_exists ($ typeDef ->kind , static ::DEFINITION_TYPE_MAPPING )) {
92
+ return static ::DEFINITION_TYPE_MAPPING [$ typeDef ->kind ];
80
93
}
81
94
82
- self :: throwUnsupportedDefinitionNode ($ typeDef );
95
+ throw static :: createUnsupportedDefinitionNodeException ($ typeDef );
83
96
}
84
97
85
98
/**
@@ -89,17 +102,6 @@ protected static function prepareConfig(DefinitionNode $typeDef): array
89
102
{
90
103
$ nodeClass = static ::getNodeClass ($ typeDef );
91
104
92
- return call_user_func ([$ nodeClass , 'toConfig ' ], $ typeDef );
93
- }
94
-
95
- private static function throwUnsupportedDefinitionNode (DefinitionNode $ typeDef ): void
96
- {
97
- $ path = explode ('\\' , get_class ($ typeDef ));
98
- throw new InvalidArgumentException (
99
- sprintf (
100
- '%s definition is not supported right now. ' ,
101
- preg_replace ('@DefinitionNode$@ ' , '' , array_pop ($ path ))
102
- )
103
- );
105
+ return \call_user_func ([$ nodeClass , 'toConfig ' ], $ typeDef );
104
106
}
105
107
}
0 commit comments