|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DeInternetJongens\LighthouseUtils\Generators\Classes; |
|
4
|
|
|
|
|
5
|
|
|
use DeInternetJongens\LighthouseUtils\Models\GraphQLSchema; |
|
6
|
|
|
use GraphQL\Language\AST\ArgumentNode; |
|
7
|
|
|
use GraphQL\Language\AST\DirectiveNode; |
|
8
|
|
|
use GraphQL\Language\AST\DocumentNode; |
|
9
|
|
|
use GraphQL\Language\AST\NodeList; |
|
10
|
|
|
use GraphQL\Language\AST\ObjectTypeDefinitionNode; |
|
11
|
|
|
use GraphQL\Language\Parser; |
|
12
|
|
|
use GraphQL\Language\Source; |
|
13
|
|
|
|
|
14
|
|
|
class ParsePermissions |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @param string $fileContents |
|
18
|
|
|
* @return array |
|
19
|
|
|
* @throws \GraphQL\Error\SyntaxError |
|
20
|
|
|
*/ |
|
21
|
|
|
public function register(string $fileContents): array |
|
22
|
|
|
{ |
|
23
|
|
|
$parser = $this->parseGraphQLSchema($fileContents); |
|
24
|
|
|
|
|
25
|
|
|
if (empty($parser->definitions[0])) { |
|
26
|
|
|
return []; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** @var ObjectTypeDefinitionNode $firstNode */ |
|
30
|
|
|
$wrapper = $parser->definitions[0]; |
|
31
|
|
|
|
|
32
|
|
|
$rows = []; |
|
33
|
|
|
|
|
34
|
|
|
/** @var NodeList $field */ |
|
35
|
|
|
foreach ($wrapper->fields as $field) { |
|
|
|
|
|
|
36
|
|
|
$arguments = []; |
|
37
|
|
|
|
|
38
|
|
|
$model = $field->type->type->name->value ?? $field->type->name->value; |
|
39
|
|
|
|
|
40
|
|
|
/** @var DirectiveNode $directive */ |
|
41
|
|
|
foreach ($field->directives as $directive) { |
|
42
|
|
|
if ($directive->name->value === 'can') { |
|
43
|
|
|
$arguments[] = $directive->arguments; |
|
44
|
|
|
|
|
45
|
|
|
/** @var ArgumentNode $argument */ |
|
46
|
|
|
foreach ($directive->arguments as $argument) { |
|
47
|
|
|
if ($argument->name->value === 'if') { |
|
48
|
|
|
$rows[] = [ |
|
49
|
|
|
'name' => $field->name->value, |
|
50
|
|
|
'model' => $model, |
|
51
|
|
|
'type' => strtolower($wrapper->name->value), |
|
|
|
|
|
|
52
|
|
|
'permission' => $argument->value->value ?? '', |
|
53
|
|
|
]; |
|
54
|
|
|
GraphQLSchema::register( |
|
55
|
|
|
$field->name->value, |
|
56
|
|
|
$model, |
|
57
|
|
|
strtolower($wrapper->name->value), |
|
58
|
|
|
$argument->value->value |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $rows; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $fileContents |
|
71
|
|
|
* @return DocumentNode |
|
72
|
|
|
* @throws \GraphQL\Error\SyntaxError |
|
73
|
|
|
*/ |
|
74
|
|
|
private function parseGraphQLSchema(string $fileContents): DocumentNode |
|
75
|
|
|
{ |
|
76
|
|
|
return Parser::parse(new Source($fileContents)); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|