1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DeInternetJongens\LighthouseUtils\Generators\Classes; |
4
|
|
|
|
5
|
|
|
class ParseDefinitions |
6
|
|
|
{ |
7
|
|
|
/** @var ParsePermissions */ |
8
|
|
|
private $permissionsParser; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* ParseDefinitions constructor. |
12
|
|
|
* |
13
|
|
|
* @param ParsePermissions $permissionsParser |
14
|
|
|
*/ |
15
|
|
|
public function __construct(ParsePermissions $permissionsParser) |
16
|
|
|
{ |
17
|
|
|
$this->permissionsParser = $permissionsParser; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string $path |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
|
|
public function parseCustomQueriesFrom(string $path) |
25
|
|
|
{ |
26
|
|
|
return $this->parseCustomSchemaFrom($path, 'Query'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $path |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
|
|
public function parseCustomMutationsFrom(string $path) |
34
|
|
|
{ |
35
|
|
|
return $this->parseCustomSchemaFrom($path, 'Mutation'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $typePath |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public function getGraphqlDefinitionFilePaths(string $typePath): array |
43
|
|
|
{ |
44
|
|
|
$files = []; |
45
|
|
|
foreach (glob(sprintf('%s/%s/*.graphql', base_path(), $typePath)) as $file) { |
46
|
|
|
$files[] = $file; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $files; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $customSchemaPath |
54
|
|
|
* @param string $type |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
private function parseCustomSchemaFrom(string $customSchemaPath, string $type) |
58
|
|
|
{ |
59
|
|
|
$customSchema = []; |
60
|
|
|
foreach ($this->getGraphqlDefinitionFilePaths($customSchemaPath) as $path) { |
61
|
|
|
$returnData = $this->extractSchema($type, $this->getSchemaFrom($path)); |
62
|
|
|
|
63
|
|
|
$customSchema = array_merge($customSchema, $returnData); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $customSchema; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $path |
71
|
|
|
* @return bool|string |
72
|
|
|
*/ |
73
|
|
|
private function getSchemaFrom(string $path) |
74
|
|
|
{ |
75
|
|
|
$file = fopen($path, "r"); |
76
|
|
|
|
77
|
|
|
$fileContents = fread($file, filesize($path)); |
|
|
|
|
78
|
|
|
|
79
|
|
|
fclose($file); |
|
|
|
|
80
|
|
|
|
81
|
|
|
return $fileContents; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $type |
86
|
|
|
* @param string $fileContents |
87
|
|
|
* @return array |
88
|
|
|
* @throws \GraphQL\Error\SyntaxError |
89
|
|
|
*/ |
90
|
|
|
private function extractSchema(string $type, string $fileContents): array |
91
|
|
|
{ |
92
|
|
|
$this->permissionsParser->register($fileContents); |
93
|
|
|
|
94
|
|
|
$rawSchemaRows = $this->extractRawRowsFromSchema($type, $fileContents); |
95
|
|
|
|
96
|
|
|
$whitespaceRemovedRows = $this->removeWhitespaceFromRows($rawSchemaRows); |
97
|
|
|
|
98
|
|
|
$cleared = array_filter($whitespaceRemovedRows); |
99
|
|
|
|
100
|
|
|
return $this->indentRows($cleared); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param array $rawSchemaRows |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
private function removeWhitespaceFromRows(array $rawSchemaRows): array |
108
|
|
|
{ |
109
|
|
|
return array_map( |
110
|
|
|
'trim', |
111
|
|
|
$rawSchemaRows |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param array $cleared |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
private function indentRows(array $cleared): array |
120
|
|
|
{ |
121
|
|
|
return array_map(function ($row) { |
122
|
|
|
return ' ' . $row; |
123
|
|
|
}, $cleared); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $type |
128
|
|
|
* @param string $fileContents |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
|
|
private function extractRawRowsFromSchema(string $type, string $fileContents): array |
132
|
|
|
{ |
133
|
|
|
return explode( |
134
|
|
|
"\n", |
135
|
|
|
str_replace(["type", $type, "{", "}"], "", $fileContents) |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|