Skip to content

Commit 313a813

Browse files
René Hrdinamghoneimy
authored andcommitted
feat: allow to specify a custom namespace for generated classes
additionally, allow to specify all user input via command line options
1 parent d2d7c52 commit 313a813

File tree

1 file changed

+41
-15
lines changed

1 file changed

+41
-15
lines changed

bin/generate_schema_objects

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,47 @@
11
#!/usr/bin/env php
22
<?php
33

4+
use GraphQL\Client;
5+
use GraphQL\SchemaGenerator\CodeGenerator\ObjectBuilderInterface;
6+
use GraphQL\SchemaGenerator\SchemaClassGenerator;
7+
48
$autoLoadFiles = [__DIR__ . '/../vendor/autoload.php', __DIR__ . '/../../../autoload.php'];
59

10+
function readConfig()
11+
{
12+
$shortOptions = implode("", [
13+
'u:',
14+
'h:',
15+
'v:',
16+
'd:',
17+
'n:'
18+
]);
19+
20+
$longOptions = [
21+
'url:',
22+
'authorization-header-name:',
23+
'authorization-header-value:',
24+
'directory:',
25+
'namespace:',
26+
];
27+
28+
$options = getopt($shortOptions, $longOptions);
29+
30+
$url = $options['url'] ?? $options['u'] ?? readline('GraphlQL endpoint URL: ');
31+
$customWriteDir = $options['directory'] ?? $options['d'] ?? readline('Custom classes writing dir (optional): ');
32+
$authHeaderName = $options['authorization-header-name'] ?? $options['h'] ?? readline('Authorization header name: ');
33+
$authHeaderValue = $options['authorization-header-value'] ?? $options['v'] ?? readline('Authorization header value: ');
34+
$namespace = $options['n'] ?? $options['namespace'] ?? trim(readline('Custom namespace (optional): '));
35+
36+
$authHeaders = [];
37+
38+
if (!empty($authHeaderName)) {
39+
$authHeaders = [$authHeaderName => $authHeaderValue];
40+
}
41+
42+
return [$url, empty($customWriteDir) ? "" : $customWriteDir, $authHeaders, empty($namespace) ? ObjectBuilderInterface::DEFAULT_NAMESPACE : $namespace];
43+
}
44+
645
// Require autoload.php depending on environment
746
$autoLoadFound = false;
847
foreach ($autoLoadFiles as $autoLoadFile) {
@@ -16,23 +55,10 @@ if (!$autoLoadFound) {
1655
throw new RuntimeException('Could not find vendor/autoload.php');
1756
}
1857

19-
use GraphQL\Client;
20-
use GraphQL\SchemaGenerator\SchemaClassGenerator;
21-
22-
$endpointUrl = readline('GraphlQL endpoint URL: ');
23-
24-
$authHeaders = [];
25-
$authHeaderName = readline('Authorization header name: ');
26-
if (!empty($authHeaderName)) {
27-
$authHeaderValue = readline('Authorization header value: ');
28-
$authHeaders = [$authHeaderName => $authHeaderValue];
29-
}
30-
31-
$customWriteDir = readline('Custom classes writing dir (optional): ');
32-
if (empty($customWriteDir)) $customWriteDir = '';
58+
[$endpointUrl, $customWriteDir, $authHeaders, $namespace] = readConfig();
3359

3460
$client = new Client($endpointUrl, $authHeaders);
35-
$scanner = new SchemaClassGenerator($client, $customWriteDir);
61+
$scanner = new SchemaClassGenerator($client, $customWriteDir, $namespace);
3662

3763
print "-------------------------------------------\n";
3864
print "Generating schema objects from schema types\n";

0 commit comments

Comments
 (0)