|
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
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Generate the scalar Graphql file |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $ns The namespace of the PHP classes |
|
89
|
|
|
* @param string $path The path to the file |
|
90
|
|
|
* @return void |
|
91
|
|
|
*/ |
|
92
|
|
|
public static function generateScalarFile(string $ns, string $path): void |
|
93
|
|
|
{ |
|
94
|
|
|
// regenerate graphql |
|
95
|
|
|
$datatypes = []; |
|
96
|
|
|
/** @var array<class-string> $classesInNamespace */ |
|
97
|
|
|
$classesInNamespace = ClassFinder::getClassesInNamespace($ns . '\\Types'); |
|
98
|
|
|
foreach ($classesInNamespace as $class) { |
|
99
|
|
|
$reflection = new \ReflectionClass($class); |
|
100
|
|
|
if (!$reflection->isInstantiable()) { |
|
101
|
|
|
continue; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$datatypes[$class] = mb_substr($class, strpos($class, "Datatype_") + mb_strlen("Datatype_")); |
|
105
|
|
|
} |
|
106
|
|
|
$scalars = \Modelarium\Util::scalars( |
|
107
|
|
|
$datatypes, |
|
108
|
|
|
$ns . '\\Types' |
|
109
|
|
|
); |
|
110
|
|
|
\Safe\file_put_contents($path, $scalars); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|