1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\Config\Parser; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use GraphQL\Language\AST\DefinitionNode; |
9
|
|
|
use GraphQL\Language\AST\EnumTypeDefinitionNode; |
10
|
|
|
use GraphQL\Language\AST\InputObjectTypeDefinitionNode; |
11
|
|
|
use GraphQL\Language\AST\NodeKind; |
12
|
|
|
use GraphQL\Language\AST\ObjectTypeDefinitionNode; |
13
|
|
|
use GraphQL\Language\Parser; |
14
|
|
|
use SplFileInfo; |
15
|
|
|
use Symfony\Component\Config\Resource\FileResource; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
17
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
18
|
|
|
use function array_keys; |
19
|
|
|
use function array_pop; |
20
|
|
|
use function call_user_func; |
21
|
|
|
use function explode; |
22
|
|
|
use function file_get_contents; |
23
|
|
|
use function get_class; |
24
|
|
|
use function in_array; |
25
|
|
|
use function preg_replace; |
26
|
|
|
use function sprintf; |
27
|
|
|
use function trim; |
28
|
|
|
use function ucfirst; |
29
|
|
|
|
30
|
|
|
class GraphQLParser implements ParserInterface |
31
|
|
|
{ |
32
|
|
|
private const DEFINITION_TYPE_MAPPING = [ |
33
|
|
|
NodeKind::OBJECT_TYPE_DEFINITION => 'object', |
34
|
|
|
NodeKind::INTERFACE_TYPE_DEFINITION => 'interface', |
35
|
|
|
NodeKind::ENUM_TYPE_DEFINITION => 'enum', |
36
|
|
|
NodeKind::UNION_TYPE_DEFINITION => 'union', |
37
|
|
|
NodeKind::INPUT_OBJECT_TYPE_DEFINITION => 'inputObject', |
38
|
|
|
NodeKind::SCALAR_TYPE_DEFINITION => 'customScalar', |
39
|
|
|
]; |
40
|
|
|
|
41
|
5 |
|
public static function parse(SplFileInfo $file, ContainerBuilder $container, array $configs = []): array |
42
|
|
|
{ |
43
|
5 |
|
$container->addResource(new FileResource($file->getRealPath())); |
44
|
5 |
|
$content = trim(file_get_contents($file->getPathname())); |
45
|
5 |
|
$typesConfig = []; |
46
|
|
|
|
47
|
|
|
// allow empty files |
48
|
5 |
|
if (empty($content)) { |
49
|
1 |
|
return []; |
50
|
|
|
} |
51
|
|
|
try { |
52
|
4 |
|
$ast = Parser::parse($content); |
53
|
1 |
|
} catch (Exception $e) { |
54
|
1 |
|
throw new InvalidArgumentException(sprintf('An error occurred while parsing the file "%s".', $file), $e->getCode(), $e); |
55
|
|
|
} |
56
|
|
|
|
57
|
3 |
|
foreach ($ast->definitions as $typeDef) { |
58
|
|
|
/** |
59
|
|
|
* @var ObjectTypeDefinitionNode|InputObjectTypeDefinitionNode|EnumTypeDefinitionNode $typeDef |
60
|
|
|
*/ |
61
|
3 |
|
if (isset($typeDef->kind) && in_array($typeDef->kind, array_keys(self::DEFINITION_TYPE_MAPPING))) { |
62
|
2 |
|
$class = sprintf('\\%s\\GraphQL\\ASTConverter\\%sNode', __NAMESPACE__, ucfirst(self::DEFINITION_TYPE_MAPPING[$typeDef->kind])); |
63
|
2 |
|
$astConverterCallable = [$class, 'toConfig']; |
64
|
|
|
if (is_callable($astConverterCallable)) { |
65
|
1 |
|
$typesConfig[$typeDef->name->value] = call_user_func($astConverterCallable, $typeDef); |
66
|
|
|
} else { |
67
|
|
|
self::throwUnsupportedDefinitionNode($typeDef); |
68
|
|
|
} |
69
|
2 |
|
} else { |
70
|
|
|
self::throwUnsupportedDefinitionNode($typeDef); |
71
|
|
|
} |
72
|
1 |
|
} |
73
|
|
|
|
74
|
1 |
|
return $typesConfig; |
75
|
1 |
|
} |
76
|
1 |
|
|
77
|
|
|
private static function throwUnsupportedDefinitionNode(DefinitionNode $typeDef): void |
78
|
1 |
|
{ |
79
|
|
|
$path = explode('\\', get_class($typeDef)); |
80
|
|
|
throw new InvalidArgumentException( |
81
|
|
|
sprintf( |
82
|
|
|
'%s definition is not supported right now.', |
83
|
|
|
preg_replace('@DefinitionNode$@', '', array_pop($path)) |
84
|
|
|
) |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|