1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Modelarium; |
4
|
|
|
|
5
|
|
|
use GraphQL\Type\Definition\Type; |
6
|
|
|
use Illuminate\Console\Concerns\InteractsWithIO; |
7
|
|
|
use Modelarium\Exception\Exception; |
8
|
|
|
use Modelarium\Exception\SkipGenerationException; |
9
|
|
|
use Modelarium\Parser; |
10
|
|
|
|
11
|
|
|
use function Safe\class_implements; |
12
|
|
|
use function Safe\date; |
13
|
|
|
|
14
|
|
|
abstract class BaseGenerator implements GeneratorInterface |
15
|
|
|
{ |
16
|
|
|
use GeneratorNameTrait; |
17
|
|
|
use InteractsWithIO; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $stubDir = null; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Parser |
26
|
|
|
*/ |
27
|
|
|
public $parser = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Type |
31
|
|
|
*/ |
32
|
|
|
protected $type = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \Symfony\Component\Console\Output\ConsoleOutput |
36
|
|
|
*/ |
37
|
|
|
public $outputSymfony; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var \Illuminate\Console\OutputStyle |
41
|
|
|
*/ |
42
|
|
|
public $outputStyle; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param Parser $parser |
46
|
|
|
* @param string $name The target type name. |
47
|
|
|
* @param Type|string $type |
48
|
|
|
*/ |
49
|
30 |
|
public function __construct(Parser $parser, string $name, $type = null) |
50
|
|
|
{ |
51
|
30 |
|
$this->parser = $parser; |
52
|
30 |
|
$this->setBaseName($name); |
53
|
|
|
|
54
|
30 |
|
if ($type instanceof Type) { |
55
|
11 |
|
$this->type = $type; |
56
|
19 |
|
} elseif (!$type) { |
57
|
19 |
|
$this->type = $parser->getSchema()->getType($name); |
58
|
|
|
} else { |
59
|
|
|
throw new Exception('Invalid model'); |
60
|
|
|
} |
61
|
30 |
|
$this->input = new \Symfony\Component\Console\Input\StringInput(""); |
62
|
30 |
|
$this->outputSymfony = new \Symfony\Component\Console\Output\ConsoleOutput(); |
63
|
30 |
|
$this->outputStyle = new \Illuminate\Console\OutputStyle($this->input, $this->outputSymfony); |
64
|
30 |
|
$this->output = $this->outputStyle; |
65
|
30 |
|
} |
66
|
|
|
|
67
|
10 |
|
protected function phpHeader(): string |
68
|
|
|
{ |
69
|
10 |
|
$date = date('c'); |
|
|
|
|
70
|
|
|
return <<<EOF |
71
|
10 |
|
<?php declare(strict_types=1); |
72
|
|
|
/** |
73
|
|
|
* This file was automatically generated by Modelarium. |
74
|
|
|
*/ |
75
|
|
|
|
76
|
|
|
EOF; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Gets the classname for a directive implementation interface class. |
81
|
|
|
* |
82
|
|
|
* @param string $directive The directive name. |
83
|
|
|
* @param string $type The type, such as 'Seeder' or 'Model'. |
84
|
|
|
* @return string|null |
85
|
|
|
*/ |
86
|
20 |
|
public function getDirectiveClass( |
87
|
|
|
string $directive, |
88
|
|
|
string $type = '' |
89
|
|
|
): ?string { |
90
|
20 |
|
foreach (Modelarium::getGeneratorDirectiveNamespaces() as $ns) { |
91
|
20 |
|
$className = $ns . '\\' . ucfirst($directive) . 'Directive'; |
92
|
20 |
|
if (!$type) { |
93
|
12 |
|
$parts = explode("\\", get_called_class()); |
94
|
12 |
|
$type = end($parts); |
95
|
12 |
|
$type = str_replace('Generator', '', $type); |
96
|
|
|
} |
97
|
20 |
|
if (class_exists($className) |
98
|
20 |
|
&& array_key_exists('Modelarium\\Laravel\\Targets\\Interfaces\\' . $type . 'DirectiveInterface', class_implements($className)) |
99
|
|
|
) { |
100
|
19 |
|
return $className; |
101
|
|
|
} |
102
|
|
|
} |
103
|
9 |
|
return null; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Process all directives from list with directive classes. |
108
|
|
|
* |
109
|
|
|
* @param \GraphQL\Language\AST\NodeList $directives The directive list |
110
|
|
|
* @param string $generatorType The generatorType, like 'Seed' or 'Model' |
111
|
|
|
* @return void |
112
|
|
|
* @throws SkipGenerationException |
113
|
|
|
*/ |
114
|
22 |
|
protected function processTypeDirectives( |
115
|
|
|
\GraphQL\Language\AST\NodeList $directives, |
116
|
|
|
string $generatorType |
117
|
|
|
): void { |
118
|
22 |
|
foreach ($directives as $directive) { |
119
|
7 |
|
$name = $directive->name->value; |
120
|
|
|
|
121
|
7 |
|
$className = $this->getDirectiveClass($name, $generatorType); |
122
|
7 |
|
if ($className) { |
123
|
7 |
|
$methodName = "$className::process{$generatorType}TypeDirective"; |
124
|
|
|
/** @phpstan-ignore-next-line */ |
125
|
7 |
|
$methodName( |
126
|
7 |
|
$this, |
127
|
|
|
$directive |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
} |
131
|
22 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Process all directives from list with directive classes. |
135
|
|
|
* |
136
|
|
|
* @param \GraphQL\Type\Definition\FieldDefinition $field |
137
|
|
|
* @param \GraphQL\Language\AST\NodeList $directives The directive list |
138
|
|
|
* @param string $generatorType The generatorType, like 'Seed' or 'Model' |
139
|
|
|
* @return void |
140
|
|
|
* @throws SkipGenerationException |
141
|
|
|
*/ |
142
|
9 |
|
public function processFieldDirectives( |
143
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
144
|
|
|
\GraphQL\Language\AST\NodeList $directives, |
145
|
|
|
string $generatorType |
146
|
|
|
): void { |
147
|
9 |
|
foreach ($directives as $directive) { |
148
|
9 |
|
$name = $directive->name->value; |
149
|
9 |
|
$className = $this->getDirectiveClass($name); |
150
|
9 |
|
if ($className) { |
151
|
9 |
|
$methodName = "$className::process{$generatorType}FieldDirective"; |
152
|
|
|
/** @phpstan-ignore-next-line */ |
153
|
9 |
|
$methodName( |
154
|
9 |
|
$this, |
155
|
|
|
$field, |
156
|
|
|
$directive |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
} |
160
|
9 |
|
} |
161
|
|
|
} |
162
|
|
|
|