1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Modelarium; |
4
|
|
|
|
5
|
|
|
use Formularium\Formularium; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Nette\PhpGenerator\PhpNamespace; |
8
|
|
|
use HaydenPierce\ClassFinder\ClassFinder; |
9
|
|
|
|
10
|
|
|
final class Util |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Undocumented function |
14
|
|
|
* |
15
|
|
|
* @param string $name The type name |
16
|
|
|
* @param string $ns The namespace |
17
|
|
|
* @return string |
18
|
|
|
*/ |
19
|
|
|
public static function generateLighthouseTypeFile(string $name, string $ns): string |
20
|
|
|
{ |
21
|
|
|
$date = date('c'); |
22
|
|
|
$HEADER = <<<EOF |
23
|
|
|
<?php declare(strict_types=1); |
24
|
|
|
/** |
25
|
|
|
* This file is automatically generated by Modelarium on {$date}. |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
EOF; |
30
|
|
|
|
31
|
|
|
$printer = new \Nette\PhpGenerator\PsrPrinter; |
32
|
|
|
$typeName = Str::lower($name); |
|
|
|
|
33
|
|
|
|
34
|
|
|
$namespace = new PhpNamespace($ns); |
35
|
|
|
$namespace->addClass('Datatype_' . $name) |
36
|
|
|
->setExtends('\\Modelarium\\Types\\FormulariumScalarType'); |
37
|
|
|
|
38
|
|
|
$stub = $HEADER . $printer->printNamespace($namespace); |
39
|
|
|
|
40
|
|
|
return $stub; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param array<string, string> $datatypes a list class name => datatype. |
45
|
|
|
* @param string $ns The namespace for these types |
46
|
|
|
* @return string[] |
47
|
|
|
*/ |
48
|
|
|
public static function scalars(array $datatypes, string $ns): array |
49
|
|
|
{ |
50
|
|
|
$graphql = [ '# File automatically generated by Modelarium on ' . date('c') . "\n\n" ]; |
51
|
|
|
|
52
|
|
|
foreach ($datatypes as $name) { |
53
|
|
|
$typeName = Str::studly($name); |
54
|
|
|
|
55
|
|
|
if ($typeName === 'String' || |
56
|
|
|
$typeName === 'Boolean' || |
57
|
|
|
$typeName === 'Int' || |
58
|
|
|
$typeName === 'Float' |
59
|
|
|
) { |
60
|
|
|
// base types |
61
|
|
|
continue; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$graphql[] = "scalar $typeName @scalar(class: \"" . |
65
|
|
|
str_replace('\\', '\\\\', $ns . '\\Datatype_' . $name) . |
66
|
|
|
"\")\n"; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $graphql; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* TODO |
74
|
|
|
* @throws |
75
|
|
|
* |
76
|
|
|
public static generateDirectives(string $filename): void |
77
|
|
|
{ |
78
|
|
|
\Safe\file_put_contents( |
79
|
|
|
$filename |
80
|
|
|
LaravelProcessor::getDirectivesGraphqlString() |
81
|
|
|
); |
82
|
|
|
} */ |
83
|
|
|
|
84
|
|
|
public static function generateScalarFiles(string $ns, string $path) |
85
|
|
|
{ |
86
|
|
|
// regenerate graphql |
87
|
|
|
$datatypes = []; |
88
|
|
|
/** @var array<class-string> $classesInNamespace */ |
89
|
|
|
$classesInNamespace = ClassFinder::getClassesInNamespace($ns . '\\Types'); |
90
|
|
|
foreach ($classesInNamespace as $class) { |
91
|
|
|
$reflection = new \ReflectionClass($class); |
92
|
|
|
if (!$reflection->isInstantiable()) { |
93
|
|
|
continue; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$datatypes[$class] = substr($class, strpos($class, "Datatype_") + mb_strlen("Datatype_")); |
97
|
|
|
} |
98
|
|
|
$scalars = \Modelarium\Util::scalars( |
99
|
|
|
$datatypes, |
100
|
|
|
$ns . '\\Types' |
101
|
|
|
); |
102
|
|
|
\Safe\file_put_contents($path, $scalars); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|