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