1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Modelarium\Laravel; |
4
|
|
|
|
5
|
|
|
use GraphQL\Type\Definition\ObjectType; |
6
|
|
|
use GraphQL\Type\Definition\Type; |
7
|
|
|
use HaydenPierce\ClassFinder\ClassFinder; |
8
|
|
|
use Modelarium\Exception\Exception; |
9
|
|
|
use Modelarium\Exception\SkipGenerationException; |
10
|
|
|
use Modelarium\GeneratedCollection; |
11
|
|
|
use Modelarium\GeneratedItem; |
12
|
|
|
use Modelarium\Laravel\Targets\EventGenerator; |
13
|
|
|
use Modelarium\Laravel\Targets\FactoryGenerator; |
14
|
|
|
use Modelarium\Laravel\Targets\MigrationGenerator; |
15
|
|
|
use Modelarium\Laravel\Targets\ModelGenerator; |
16
|
|
|
use Modelarium\Laravel\Targets\PolicyGenerator; |
17
|
|
|
use Modelarium\Laravel\Targets\SeedGenerator; |
18
|
|
|
use Modelarium\Parser; |
19
|
|
|
use Modelarium\Processor as ModelariumProcessor; |
20
|
|
|
use Nette\PhpGenerator\ClassType; |
21
|
|
|
use Nuwave\Lighthouse\Schema\Factories\DirectiveFactory; |
22
|
|
|
|
23
|
|
|
use function Safe\class_implements; |
24
|
|
|
|
25
|
|
|
class Processor extends ModelariumProcessor |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var Parser |
29
|
|
|
*/ |
30
|
|
|
protected $parser = null; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var bool |
34
|
|
|
*/ |
35
|
|
|
protected $runMigration = true; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var bool |
39
|
|
|
*/ |
40
|
|
|
protected $runSeed = true; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var bool |
44
|
|
|
*/ |
45
|
|
|
protected $runFactory = true; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var bool |
49
|
|
|
*/ |
50
|
|
|
protected $runModel = true; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var bool |
54
|
|
|
*/ |
55
|
|
|
protected $runPolicy = true; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var bool |
59
|
|
|
*/ |
60
|
|
|
protected $runEvent = true; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* DatabaseSeeder class for Laravel |
64
|
|
|
* |
65
|
|
|
* @var ClassType |
66
|
|
|
*/ |
67
|
|
|
protected $seederClass = null; |
68
|
|
|
|
69
|
8 |
|
public function __construct() |
70
|
|
|
{ |
71
|
8 |
|
$this->parser = new Parser(); |
72
|
8 |
|
$this->parser->setImport('directives.graphql', self::getDirectivesGraphqlString()); |
73
|
8 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Scan the given namespaces for directive classes. |
77
|
|
|
* |
78
|
|
|
* @param string[] $directiveNamespaces |
79
|
|
|
* @return array<string, string> |
80
|
|
|
*/ |
81
|
8 |
|
public static function getDirectivesGraphql($directiveNamespaces = [ 'Modelarium\Laravel\Lighthouse\Directives' ]): array |
82
|
|
|
{ |
83
|
8 |
|
$directives = []; |
84
|
|
|
|
85
|
8 |
|
foreach ($directiveNamespaces as $directiveNamespace) { |
86
|
|
|
/** @var array<class-string> $classesInNamespace */ |
87
|
8 |
|
$classesInNamespace = ClassFinder::getClassesInNamespace($directiveNamespace); |
88
|
|
|
|
89
|
8 |
|
foreach ($classesInNamespace as $class) { |
90
|
8 |
|
$reflection = new \ReflectionClass($class); |
91
|
8 |
|
if (! $reflection->isInstantiable()) { |
92
|
|
|
continue; |
93
|
|
|
} |
94
|
|
|
|
95
|
8 |
|
if (! is_a($class, \Nuwave\Lighthouse\Schema\Directives\BaseDirective::class, true)) { |
96
|
|
|
continue; |
97
|
|
|
} |
98
|
|
|
|
99
|
8 |
|
$name = DirectiveFactory::directiveName((string)$class); |
100
|
|
|
// @phpstan-ignore-next-line |
101
|
8 |
|
$directives[$name] = trim($class::definition()); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
8 |
|
return $directives; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Scan the given namespaces for directive classes. |
110
|
|
|
* |
111
|
|
|
* @param string[] $directiveNamespaces |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
8 |
|
public static function getDirectivesGraphqlString($directiveNamespaces = [ 'Modelarium\\Laravel\\Lighthouse\\Directives' ]): string |
115
|
|
|
{ |
116
|
8 |
|
return implode("\n\n", self::getDirectivesGraphql($directiveNamespaces)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* |
121
|
|
|
* @param string[] $files |
122
|
|
|
* @return GeneratedCollection |
123
|
|
|
*/ |
124
|
|
|
public function processFiles(array $files): GeneratedCollection |
125
|
|
|
{ |
126
|
|
|
$this->parser->fromFiles($files); |
127
|
|
|
return $this->process(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* |
132
|
|
|
* @param string $data |
133
|
|
|
* @return GeneratedCollection |
134
|
|
|
*/ |
135
|
7 |
|
public function processString(string $data): GeneratedCollection |
136
|
|
|
{ |
137
|
7 |
|
$this->parser->fromString($data); |
138
|
7 |
|
return $this->process(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* |
143
|
|
|
* @param string[] $data |
144
|
|
|
* @return GeneratedCollection |
145
|
|
|
*/ |
146
|
1 |
|
public function processStrings(array $data): GeneratedCollection |
147
|
|
|
{ |
148
|
1 |
|
$this->parser->fromStrings($data); |
149
|
1 |
|
return $this->process(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* |
154
|
|
|
* @return GeneratedCollection |
155
|
|
|
*/ |
156
|
8 |
|
public function process(): GeneratedCollection |
157
|
|
|
{ |
158
|
8 |
|
$schema = $this->parser->getSchema(); |
159
|
8 |
|
$typeMap = $schema->getTypeMap(); |
160
|
|
|
|
161
|
8 |
|
$this->collection = new GeneratedCollection(); |
162
|
8 |
|
if ($this->runSeed) { |
163
|
8 |
|
$this->createSeederClass(); |
164
|
|
|
} |
165
|
|
|
|
166
|
8 |
|
foreach ($typeMap as $name => $object) { |
167
|
8 |
|
if ($object instanceof ObjectType) { |
168
|
8 |
|
if ($name === 'Query' || $name === 'Mutation' || $name === 'Subscription' || $name === 'Can') { |
169
|
1 |
|
continue; |
170
|
|
|
} |
171
|
8 |
|
$g = $this->processType((string)$name, $object); |
172
|
8 |
|
$this->collection = $this->collection->merge($g); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
8 |
|
$this->collection = $this->collection->merge($this->processMutation($schema->getMutationType())); |
177
|
|
|
|
178
|
8 |
|
if ($this->runSeed) { |
179
|
8 |
|
$printer = new \Nette\PhpGenerator\PsrPrinter; |
180
|
8 |
|
$seeder = "<?php\n\n" . $printer->printClass($this->seederClass); |
181
|
8 |
|
$this->collection->add( |
182
|
8 |
|
new GeneratedItem( |
183
|
8 |
|
GeneratedItem::TYPE_SEED, |
184
|
|
|
$seeder, |
185
|
8 |
|
SeedGenerator::getBasePath('database/seeds/DatabaseSeeder.php') |
186
|
|
|
) |
187
|
|
|
); |
188
|
|
|
} |
189
|
|
|
|
190
|
8 |
|
return $this->collection; |
191
|
|
|
} |
192
|
|
|
|
193
|
8 |
|
protected function processType(string $name, ObjectType $object): GeneratedCollection |
194
|
|
|
{ |
195
|
8 |
|
$collection = new GeneratedCollection(); |
196
|
8 |
|
if (str_starts_with($name, '__')) { |
197
|
|
|
// internal type |
198
|
8 |
|
return $collection; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
// @typeSkip |
202
|
8 |
|
$directives = $object->astNode->directives; |
203
|
8 |
|
foreach ($directives as $directive) { |
204
|
|
|
$dName = $directive->name->value; |
205
|
|
|
if ($dName === 'typeSkip') { |
206
|
|
|
return $collection; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
8 |
|
if ($this->runMigration) { |
211
|
|
|
try { |
212
|
8 |
|
$collection = $collection->merge((new MigrationGenerator($this->parser, $name, $object))->generate()); |
213
|
|
|
} catch (SkipGenerationException $e) { |
|
|
|
|
214
|
|
|
} |
215
|
|
|
} |
216
|
8 |
|
if ($this->runSeed) { |
217
|
|
|
try { |
218
|
8 |
|
$generator = new SeedGenerator($this->parser, $name, $object); |
219
|
8 |
|
$collection = $collection->merge($generator->generate()); |
220
|
|
|
|
221
|
8 |
|
$this->seederClass->getMethod('run') |
222
|
8 |
|
->addBody('$this->call(' . $generator->getStudlyName() . 'Seeder::class);'); |
223
|
|
|
} catch (SkipGenerationException $e) { |
|
|
|
|
224
|
|
|
} |
225
|
|
|
} |
226
|
8 |
|
if ($this->runFactory) { |
227
|
|
|
try { |
228
|
8 |
|
$collection = $collection->merge((new FactoryGenerator($this->parser, $name, $object))->generate()); |
229
|
|
|
} catch (SkipGenerationException $e) { |
|
|
|
|
230
|
|
|
} |
231
|
|
|
} |
232
|
8 |
|
if ($this->runModel) { |
233
|
|
|
try { |
234
|
8 |
|
$collection = $collection->merge((new ModelGenerator($this->parser, $name, $object))->generate()); |
235
|
|
|
} catch (SkipGenerationException $e) { |
|
|
|
|
236
|
|
|
} |
237
|
|
|
} |
238
|
8 |
|
return $collection; |
239
|
|
|
} |
240
|
|
|
|
241
|
8 |
|
protected function processMutation(?Type $object): GeneratedCollection |
242
|
|
|
{ |
243
|
8 |
|
$collection = new GeneratedCollection(); |
244
|
8 |
|
if (!$object) { |
245
|
7 |
|
return $collection; |
246
|
|
|
} |
247
|
1 |
|
if ($this->runPolicy) { |
248
|
1 |
|
$collection = $collection->merge((new PolicyGenerator($this->parser, 'Mutation', $object))->generate()); |
249
|
|
|
} |
250
|
1 |
|
if ($this->runEvent) { |
251
|
1 |
|
$collection = $collection->merge((new EventGenerator($this->parser, 'Mutation', $object))->generate()); |
252
|
|
|
} |
253
|
1 |
|
return $collection; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Generates the DatabaseSeeder class. |
258
|
|
|
* |
259
|
|
|
* @return void |
260
|
|
|
*/ |
261
|
8 |
|
protected function createSeederClass(): void |
262
|
|
|
{ |
263
|
8 |
|
$this->seederClass = new \Nette\PhpGenerator\ClassType('DatabaseSeeder'); |
264
|
8 |
|
$this->seederClass->setExtends('Illuminate\Database\Seeder') |
265
|
8 |
|
->addComment("This file was automatically generated by Modelarium."); |
266
|
|
|
|
267
|
8 |
|
$this->seederClass->addMethod('run') |
268
|
8 |
|
->setPublic() |
269
|
8 |
|
->addComment("Seed the application\'s database.\n@return void"); |
270
|
8 |
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Set the value of runMigration |
274
|
|
|
* |
275
|
|
|
* @param bool $runMigration |
276
|
|
|
* |
277
|
|
|
* @return self |
278
|
|
|
*/ |
279
|
|
|
public function setRunMigration(bool $runMigration): self |
280
|
|
|
{ |
281
|
|
|
$this->runMigration = $runMigration; |
282
|
|
|
|
283
|
|
|
return $this; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Set the value of runSeed |
288
|
|
|
* |
289
|
|
|
* @param bool $runSeed |
290
|
|
|
* |
291
|
|
|
* @return self |
292
|
|
|
*/ |
293
|
|
|
public function setRunSeed(bool $runSeed): self |
294
|
|
|
{ |
295
|
|
|
$this->runSeed = $runSeed; |
296
|
|
|
|
297
|
|
|
return $this; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Set the value of runFactory |
302
|
|
|
* |
303
|
|
|
* @param bool $runFactory |
304
|
|
|
* |
305
|
|
|
* @return self |
306
|
|
|
*/ |
307
|
|
|
public function setRunFactory(bool $runFactory): self |
308
|
|
|
{ |
309
|
|
|
$this->runFactory = $runFactory; |
310
|
|
|
|
311
|
|
|
return $this; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Set the value of runModel |
316
|
|
|
* |
317
|
|
|
* @param bool $runModel |
318
|
|
|
* |
319
|
|
|
* @return self |
320
|
|
|
*/ |
321
|
|
|
public function setRunModel(bool $runModel): self |
322
|
|
|
{ |
323
|
|
|
$this->runModel = $runModel; |
324
|
|
|
|
325
|
|
|
return $this; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Set the value of runPolicy |
330
|
|
|
* |
331
|
|
|
* @param bool $runPolicy |
332
|
|
|
* |
333
|
|
|
* @return self |
334
|
|
|
*/ |
335
|
|
|
public function setRunPolicy(bool $runPolicy): self |
336
|
|
|
{ |
337
|
|
|
$this->runPolicy = $runPolicy; |
338
|
|
|
|
339
|
|
|
return $this; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Set the value of runEvent |
344
|
|
|
* |
345
|
|
|
* @param bool $runEvent |
346
|
|
|
* |
347
|
|
|
* @return self |
348
|
|
|
*/ |
349
|
|
|
public function setRunEvent(bool $runEvent): self |
350
|
|
|
{ |
351
|
|
|
$this->runEvent = $runEvent; |
352
|
|
|
|
353
|
|
|
return $this; |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
|