|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Modelarium\Laravel\Targets; |
|
4
|
|
|
|
|
5
|
|
|
use Formularium\Datatype; |
|
6
|
|
|
use Formularium\Extradata; |
|
7
|
|
|
use Formularium\ExtradataParameter; |
|
8
|
|
|
use Formularium\Field; |
|
9
|
|
|
use Formularium\Model; |
|
10
|
|
|
use Illuminate\Support\Str; |
|
11
|
|
|
use GraphQL\Type\Definition\ListOfType; |
|
12
|
|
|
use GraphQL\Type\Definition\NonNull; |
|
13
|
|
|
use GraphQL\Type\Definition\ObjectType; |
|
14
|
|
|
use GraphQL\Type\Definition\UnionType; |
|
15
|
|
|
use Modelarium\BaseGenerator; |
|
16
|
|
|
use Modelarium\Datatypes\Datatype_relationship; |
|
17
|
|
|
use Modelarium\Datatypes\RelationshipFactory; |
|
18
|
|
|
use Modelarium\Exception\Exception; |
|
19
|
|
|
use Modelarium\FormulariumUtils; |
|
20
|
|
|
use Modelarium\GeneratedCollection; |
|
21
|
|
|
use Modelarium\GeneratedItem; |
|
22
|
|
|
use Modelarium\Parser; |
|
23
|
|
|
use Modelarium\Types\FormulariumScalarType; |
|
24
|
|
|
use Nette\PhpGenerator\Method; |
|
25
|
|
|
use GraphQL\Language\AST\DirectiveNode; |
|
26
|
|
|
|
|
27
|
|
|
class ModelGenerator extends BaseGenerator |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $stubDir = __DIR__ . "/stubs/"; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected static $modelDir = 'app/Models/'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var ObjectType |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $type = null; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var \Nette\PhpGenerator\ClassType |
|
46
|
|
|
*/ |
|
47
|
|
|
public $class = null; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* fillable attributes |
|
51
|
|
|
* |
|
52
|
|
|
* @var array |
|
53
|
|
|
*/ |
|
54
|
|
|
public $fillable = []; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* fillable attributes |
|
58
|
|
|
* |
|
59
|
|
|
* @var array |
|
60
|
|
|
*/ |
|
61
|
|
|
public $hidden = []; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* cast attributes |
|
65
|
|
|
* |
|
66
|
|
|
* @var array |
|
67
|
|
|
*/ |
|
68
|
|
|
public $casts = []; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* |
|
72
|
|
|
* @var string |
|
73
|
|
|
*/ |
|
74
|
|
|
public $parentClassName = '\Illuminate\Database\Eloquent\Model'; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* fields |
|
78
|
|
|
* |
|
79
|
|
|
* @var Model |
|
80
|
|
|
*/ |
|
81
|
|
|
public $fModel = null; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* |
|
85
|
|
|
* @var array |
|
86
|
|
|
*/ |
|
87
|
|
|
public $traits = []; |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Random generation |
|
91
|
|
|
* |
|
92
|
|
|
* @var Method |
|
93
|
|
|
*/ |
|
94
|
|
|
protected $methodRandom = null; |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Do we have a 'can' attribute? |
|
98
|
|
|
* |
|
99
|
|
|
* @var boolean |
|
100
|
|
|
*/ |
|
101
|
|
|
protected $hasCan = true; |
|
102
|
|
|
|
|
103
|
10 |
|
/** |
|
104
|
|
|
* If true, we have timestamps on the migration. |
|
105
|
10 |
|
* |
|
106
|
10 |
|
* @var boolean |
|
107
|
10 |
|
*/ |
|
108
|
10 |
|
protected $migrationTimestamps = false; |
|
109
|
10 |
|
|
|
110
|
10 |
|
public function generate(): GeneratedCollection |
|
111
|
|
|
{ |
|
112
|
10 |
|
$this->fModel = Model::create($this->studlyName); |
|
113
|
10 |
|
$x = new GeneratedCollection([ |
|
114
|
10 |
|
new GeneratedItem( |
|
115
|
10 |
|
GeneratedItem::TYPE_MODEL, |
|
116
|
10 |
|
$this->generateString(), |
|
117
|
|
|
$this->getGenerateFilename() |
|
118
|
|
|
), |
|
119
|
10 |
|
new GeneratedItem( |
|
120
|
|
|
GeneratedItem::TYPE_MODEL, |
|
121
|
|
|
$this->templateStub('model'), |
|
122
|
10 |
|
$this->getGenerateFilename(false), |
|
123
|
|
|
true |
|
124
|
|
|
) |
|
125
|
|
|
]); |
|
126
|
|
|
return $x; |
|
127
|
|
|
} |
|
128
|
10 |
|
|
|
129
|
|
|
protected function processField( |
|
130
|
10 |
|
string $typeName, |
|
131
|
10 |
|
\GraphQL\Type\Definition\FieldDefinition $field, |
|
132
|
|
|
\GraphQL\Language\AST\NodeList $directives, |
|
133
|
|
|
bool $isRequired |
|
134
|
10 |
|
): void { |
|
135
|
|
|
$fieldName = $field->name; |
|
136
|
|
|
|
|
137
|
|
|
if ($typeName === 'ID') { |
|
138
|
|
|
return; |
|
139
|
10 |
|
} |
|
140
|
10 |
|
|
|
141
|
|
|
$scalarType = $this->parser->getScalarType($typeName); |
|
142
|
8 |
|
|
|
143
|
8 |
|
/** |
|
144
|
8 |
|
* @var Field $field |
|
145
|
8 |
|
*/ |
|
146
|
|
|
$field = null; |
|
147
|
5 |
|
if (!$scalarType) { |
|
148
|
5 |
|
// probably another model |
|
149
|
5 |
|
$field = FormulariumUtils::getFieldFromDirectives( |
|
150
|
5 |
|
$fieldName, |
|
151
|
5 |
|
$typeName, |
|
152
|
|
|
$directives |
|
153
|
|
|
); |
|
154
|
|
|
} elseif ($scalarType instanceof FormulariumScalarType) { |
|
155
|
|
|
$field = FormulariumUtils::getFieldFromDirectives( |
|
156
|
|
|
$fieldName, |
|
157
|
10 |
|
$scalarType->getDatatype()->getName(), |
|
158
|
10 |
|
$directives |
|
159
|
10 |
|
); |
|
160
|
10 |
|
} else { |
|
161
|
10 |
|
return; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
if ($isRequired) { |
|
165
|
10 |
|
$field->setValidatorOption( |
|
166
|
10 |
|
Datatype::REQUIRED, |
|
167
|
|
|
'value', |
|
168
|
10 |
|
true |
|
169
|
|
|
); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
10 |
|
$this->fModel->appendField($field); |
|
173
|
|
|
} |
|
174
|
10 |
|
|
|
175
|
|
|
protected function processFieldDirectives( |
|
176
|
10 |
|
\GraphQL\Type\Definition\FieldDefinition $field, |
|
177
|
|
|
\GraphQL\Language\AST\NodeList $directives |
|
178
|
|
|
): void { |
|
179
|
|
|
$fieldName = $field->name; |
|
180
|
|
|
|
|
181
|
|
|
list($type, $isRequired) = Parser::getUnwrappedType($field->type); |
|
182
|
|
|
|
|
183
|
|
|
foreach ($directives as $directive) { |
|
184
|
|
|
$name = $directive->name->value; |
|
185
|
|
|
$className = $this->getDirectiveClass($name); |
|
186
|
|
|
if ($className) { |
|
187
|
|
|
$methodName = "$className::processModelFieldDirective"; |
|
188
|
|
|
$methodName( |
|
189
|
|
|
$this, |
|
190
|
|
|
$field, |
|
191
|
|
|
$directive |
|
192
|
|
|
); |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
// TODO: convert to separate classes |
|
197
|
|
|
foreach ($directives as $directive) { |
|
198
|
|
|
$name = $directive->name->value; |
|
199
|
|
|
switch ($name) { |
|
200
|
|
|
|
|
201
|
|
|
case 'casts': |
|
202
|
|
|
foreach ($directive->arguments as $arg) { |
|
203
|
|
|
/** |
|
204
|
|
|
* @var \GraphQL\Language\AST\ArgumentNode $arg |
|
205
|
|
|
*/ |
|
206
|
|
|
|
|
207
|
|
|
$value = $arg->value->value; |
|
|
|
|
|
|
208
|
|
|
|
|
209
|
|
|
switch ($arg->name->value) { |
|
210
|
|
|
case 'type': |
|
211
|
|
|
$this->casts[$fieldName] = $value; |
|
212
|
|
|
} |
|
213
|
10 |
|
} |
|
214
|
10 |
|
break; |
|
215
|
10 |
|
} |
|
216
|
|
|
} |
|
217
|
8 |
|
|
|
218
|
|
|
$typeName = $type->name; |
|
219
|
|
|
$this->processField($typeName, $field, $directives, $isRequired); |
|
220
|
|
|
} |
|
221
|
8 |
|
|
|
222
|
8 |
|
protected function processRelationship( |
|
223
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
|
224
|
8 |
|
\GraphQL\Language\AST\NodeList $directives |
|
225
|
|
|
): void { |
|
226
|
8 |
|
$lowerName = mb_strtolower($this->getInflector()->singularize($field->name)); |
|
227
|
8 |
|
$lowerNamePlural = $this->getInflector()->pluralize($lowerName); |
|
228
|
|
|
|
|
229
|
|
|
$targetClass = '\\App\\Models\\' . Str::studly($this->getInflector()->singularize($field->name)); |
|
230
|
8 |
|
|
|
231
|
|
|
list($type, $isRequired) = Parser::getUnwrappedType($field->type); |
|
232
|
|
|
$typeName = $type->name; |
|
233
|
|
|
|
|
234
|
8 |
|
// special types that should be skipped. |
|
235
|
8 |
|
if ($typeName === 'Can') { |
|
236
|
8 |
|
$this->hasCan = true; |
|
237
|
8 |
|
return; |
|
238
|
8 |
|
} |
|
239
|
|
|
|
|
240
|
8 |
|
$generateRandom = false; |
|
241
|
8 |
|
$sourceTypeName = $this->lowerName; |
|
242
|
8 |
|
$targetTypeName = $lowerName; |
|
243
|
8 |
|
$relationship = null; |
|
244
|
4 |
|
$isInverse = false; |
|
245
|
4 |
|
|
|
246
|
4 |
|
// TODO: convert to separate classes |
|
247
|
4 |
|
foreach ($directives as $directive) { |
|
248
|
4 |
|
$name = $directive->name->value; |
|
249
|
4 |
|
switch ($name) { |
|
250
|
4 |
|
case 'belongsTo': |
|
251
|
4 |
|
$generateRandom = true; |
|
252
|
|
|
$relationship = RelationshipFactory::RELATIONSHIP_ONE_TO_MANY; |
|
253
|
8 |
|
$isInverse = true; |
|
254
|
1 |
|
$this->class->addMethod($lowerName) |
|
255
|
1 |
|
->setPublic() |
|
256
|
1 |
|
->setReturnType('\\Illuminate\\Database\\Eloquent\\Relations\\BelongsTo') |
|
257
|
1 |
|
->setBody("return \$this->belongsTo($targetClass::class);"); |
|
258
|
1 |
|
break; |
|
259
|
1 |
|
|
|
260
|
1 |
|
case 'belongsToMany': |
|
261
|
1 |
|
$generateRandom = true; |
|
262
|
|
|
$relationship = RelationshipFactory::RELATIONSHIP_MANY_TO_MANY; |
|
263
|
7 |
|
$isInverse = true; |
|
264
|
3 |
|
$this->class->addMethod($lowerNamePlural) |
|
265
|
3 |
|
->setPublic() |
|
266
|
3 |
|
->setReturnType('\\Illuminate\\Database\\Eloquent\\Relations\\BelongsToMany') |
|
267
|
3 |
|
->setBody("return \$this->belongsToMany($targetClass::class);"); |
|
268
|
3 |
|
break; |
|
269
|
3 |
|
|
|
270
|
3 |
|
case 'hasOne': |
|
271
|
|
|
$relationship = RelationshipFactory::RELATIONSHIP_ONE_TO_ONE; |
|
272
|
7 |
|
$isInverse = false; |
|
273
|
1 |
|
$this->class->addMethod($lowerName) |
|
274
|
1 |
|
->setPublic() |
|
275
|
1 |
|
->setReturnType('\\Illuminate\\Database\\Eloquent\\Relations\\HasOne') |
|
276
|
1 |
|
->setBody("return \$this->hasOne($targetClass::class);"); |
|
277
|
1 |
|
break; |
|
278
|
1 |
|
|
|
279
|
1 |
|
case 'hasMany': |
|
280
|
1 |
|
$relationship = RelationshipFactory::RELATIONSHIP_ONE_TO_MANY; |
|
281
|
|
|
$isInverse = false; |
|
282
|
7 |
|
$target = $this->getInflector()->singularize($targetClass); |
|
283
|
7 |
|
$this->class->addMethod($lowerNamePlural) |
|
284
|
7 |
|
->setPublic() |
|
285
|
3 |
|
->setReturnType('\\Illuminate\\Database\\Eloquent\\Relations\\HasMany') |
|
286
|
1 |
|
->setBody("return \$this->hasMany($target::class);"); |
|
287
|
|
|
break; |
|
288
|
2 |
|
|
|
289
|
|
|
case 'morphOne': |
|
290
|
3 |
|
case 'morphMany': |
|
291
|
|
|
case 'morphToMany': |
|
292
|
3 |
|
if ($name === 'morphOne') { |
|
293
|
3 |
|
$relationship = RelationshipFactory::MORPH_ONE_TO_ONE; |
|
294
|
|
|
} else { |
|
295
|
3 |
|
$relationship = RelationshipFactory::MORPH_ONE_TO_MANY; |
|
296
|
|
|
} |
|
297
|
|
|
$isInverse = false; |
|
298
|
3 |
|
|
|
299
|
3 |
|
$targetType = $this->parser->getType($typeName); |
|
300
|
3 |
|
if (!$targetType) { |
|
301
|
3 |
|
throw new Exception("Cannot get type {$typeName} as a relationship to {$this->baseName}"); |
|
302
|
3 |
|
} elseif (!($targetType instanceof ObjectType)) { |
|
303
|
3 |
|
throw new Exception("{$typeName} is not a type for a relationship to {$this->baseName}"); |
|
304
|
|
|
} |
|
305
|
|
|
$targetField = null; |
|
306
|
3 |
|
foreach ($targetType->getFields() as $subField) { |
|
307
|
|
|
$subDir = Parser::getDirectives($subField->astNode->directives); |
|
308
|
|
|
if (array_key_exists('morphTo', $subDir) || array_key_exists('morphedByMany', $subDir)) { |
|
309
|
|
|
$targetField = $subField->name; |
|
310
|
3 |
|
break; |
|
311
|
|
|
} |
|
312
|
3 |
|
} |
|
313
|
3 |
|
if (!$targetField) { |
|
314
|
3 |
|
throw new Exception("{$targetType} does not have a '@morphTo' or '@morphToMany' field"); |
|
315
|
|
|
} |
|
316
|
7 |
|
|
|
317
|
2 |
|
$this->class->addMethod($field->name) |
|
318
|
2 |
|
// TODO: return type |
|
319
|
2 |
|
->setPublic() |
|
320
|
2 |
|
->setBody("return \$this->{$name}($typeName::class, '$targetField');"); |
|
321
|
2 |
|
break; |
|
322
|
2 |
|
|
|
323
|
2 |
|
case 'morphTo': |
|
324
|
|
|
$relationship = RelationshipFactory::MORPH_ONE_TO_MANY; // TODO |
|
325
|
5 |
|
$isInverse = true; |
|
326
|
1 |
|
$this->class->addMethod($field->name) |
|
327
|
1 |
|
->setReturnType('\\Illuminate\\Database\\Eloquent\\Relations\\MorphTo') |
|
328
|
1 |
|
->setPublic() |
|
329
|
|
|
->setBody("return \$this->morphTo();"); |
|
330
|
1 |
|
break; |
|
331
|
1 |
|
|
|
332
|
1 |
|
case 'morphedByMany': |
|
333
|
|
|
$relationship = RelationshipFactory::MORPH_MANY_TO_MANY; // TODO |
|
334
|
|
|
$isInverse = true; |
|
335
|
|
|
$typeMap = $this->parser->getSchema()->getTypeMap(); |
|
336
|
|
|
|
|
337
|
|
|
foreach ($typeMap as $name => $object) { |
|
338
|
|
|
if (!($object instanceof ObjectType) || $name === 'Query' || $name === 'Mutation' || $name === 'Subscription') { |
|
339
|
1 |
|
continue; |
|
340
|
|
|
} |
|
341
|
1 |
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* @var ObjectType $object |
|
344
|
1 |
|
*/ |
|
345
|
1 |
|
|
|
346
|
|
|
if (str_starts_with((string)$name, '__')) { |
|
347
|
1 |
|
// internal type |
|
348
|
1 |
|
continue; |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
1 |
|
foreach ($object->getFields() as $subField) { |
|
352
|
1 |
|
$subDirectives = Parser::getDirectives($subField->astNode->directives); |
|
353
|
1 |
|
|
|
354
|
1 |
|
if (!array_key_exists('morphToMany', $subDirectives)) { |
|
355
|
1 |
|
continue; |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
1 |
|
$methodName = $this->getInflector()->pluralize(mb_strtolower((string)$name)); |
|
359
|
|
|
$this->class->addMethod($methodName) |
|
360
|
|
|
->setReturnType('\\Illuminate\\Database\\Eloquent\\Relations\\MorphToMany') |
|
361
|
4 |
|
->setPublic() |
|
362
|
|
|
->setBody("return \$this->morphedByMany($name::class, '$lowerName');"); |
|
363
|
|
|
} |
|
364
|
8 |
|
} |
|
365
|
|
|
break; |
|
366
|
|
|
|
|
367
|
|
|
case 'laravelMediaLibraryData': |
|
368
|
8 |
|
$collection = 'images'; |
|
369
|
8 |
|
$customFields = []; |
|
370
|
|
|
$studlyFieldName = Str::studly($field->name); |
|
371
|
8 |
|
|
|
372
|
|
|
foreach ($directive->arguments as $arg) { |
|
373
|
8 |
|
/** |
|
374
|
5 |
|
* @var \GraphQL\Language\AST\ArgumentNode $arg |
|
375
|
|
|
*/ |
|
376
|
|
|
|
|
377
|
4 |
|
switch ($arg->name->value) { |
|
378
|
4 |
|
case 'collection': |
|
379
|
4 |
|
$collection = $arg->value->value; |
|
|
|
|
|
|
380
|
4 |
|
break; |
|
381
|
|
|
case 'fields': |
|
382
|
|
|
foreach ($arg->value->values as $item) { |
|
|
|
|
|
|
383
|
|
|
$customFields[] = $item->value; |
|
384
|
8 |
|
} |
|
385
|
|
|
break; |
|
386
|
10 |
|
} |
|
387
|
|
|
} |
|
388
|
|
|
$studlyCollection = Str::studly($collection); |
|
389
|
10 |
|
|
|
390
|
1 |
|
// registration |
|
391
|
1 |
|
if (!$this->class->hasMethod("registerMediaCollections")) { |
|
392
|
|
|
$registerMediaCollections = $this->class->addMethod("registerMediaCollections") |
|
393
|
1 |
|
->setPublic() |
|
394
|
1 |
|
->setReturnType('void') |
|
395
|
1 |
|
->addComment("Configures Laravel media-library"); |
|
396
|
1 |
|
} else { |
|
397
|
1 |
|
$registerMediaCollections = $this->class->getMethod("registerMediaCollections"); |
|
398
|
|
|
} |
|
399
|
|
|
$registerMediaCollections->addBody("\$this->addMediaCollection(?);\n", [$collection]); |
|
400
|
1 |
|
|
|
401
|
|
|
// all image models for this collection |
|
402
|
|
|
$this->class->addMethod("getMedia{$studlyCollection}Collection") |
|
403
|
1 |
|
->setPublic() |
|
404
|
1 |
|
->setReturnType('\\Spatie\\MediaLibrary\\MediaCollections\\Models\\Collections\\MediaCollection') |
|
405
|
1 |
|
->addComment("Returns a collection media from Laravel-MediaLibrary") |
|
406
|
1 |
|
->setBody("return \$this->getMedia(?);", [$collection]); |
|
407
|
1 |
|
|
|
408
|
1 |
|
// custom fields |
|
409
|
|
|
$this->class->addMethod("getMedia{$studlyCollection}CustomFields") |
|
410
|
|
|
->setPublic() |
|
411
|
|
|
->setReturnType('array') |
|
412
|
|
|
->addComment("Returns custom fields for the media") |
|
413
|
|
|
->setBody("return ?;", [$customFields]); |
|
414
|
|
|
|
|
415
|
|
|
$this->class->addMethod("get{$studlyFieldName}urlAttribute") |
|
416
|
|
|
->setPublic() |
|
417
|
|
|
->setReturnType('string') |
|
418
|
|
|
->addComment("Returns the media attribute (url) for the $collection") |
|
419
|
|
|
->setBody( /** @lang PHP */ |
|
420
|
|
|
<<< PHP |
|
421
|
|
|
\$image = \$this->getMedia{$studlyCollection}Collection()->first(); |
|
422
|
|
|
if (\$image) { |
|
423
|
|
|
return \$image->getUrl(); |
|
424
|
|
|
} |
|
425
|
|
|
return ''; |
|
426
|
|
|
PHP |
|
427
|
|
|
); |
|
428
|
|
|
|
|
429
|
|
|
// all image models for this collection |
|
430
|
|
|
$this->class->addMethod("get{$studlyFieldName}Attribute") |
|
431
|
|
|
->setPublic() |
|
432
|
|
|
->setReturnType('array') |
|
433
|
|
|
->addComment("Returns media attribute for the $collection media with custom fields") |
|
434
|
|
|
->setBody( /** @lang PHP */ |
|
435
|
|
|
<<< PHP |
|
436
|
10 |
|
\$image = \$this->getMedia{$studlyCollection}Collection()->first(); |
|
437
|
|
|
if (\$image) { |
|
438
|
10 |
|
\$customFields = []; |
|
439
|
|
|
foreach (\$this->getMedia{$studlyCollection}CustomFields() as \$c) { |
|
440
|
10 |
|
\$customFields[\$c] = \$image->getCustomProperty(\$c); |
|
441
|
10 |
|
} |
|
442
|
10 |
|
return [ |
|
443
|
10 |
|
'url' => \$image->getUrl(), |
|
444
|
10 |
|
'fields' => json_encode(\$customFields) |
|
445
|
10 |
|
]; |
|
446
|
10 |
|
} |
|
447
|
10 |
|
return []; |
|
448
|
10 |
|
PHP |
|
449
|
10 |
|
); |
|
450
|
|
|
return; |
|
451
|
10 |
|
|
|
452
|
10 |
|
default: |
|
453
|
10 |
|
break; |
|
454
|
10 |
|
} |
|
455
|
|
|
} |
|
456
|
10 |
|
if (!$relationship) { |
|
457
|
10 |
|
throw new Exception("Could not find a relationship in {$typeName} for {$field->name} in {$sourceTypeName}"); |
|
458
|
10 |
|
} |
|
459
|
|
|
|
|
460
|
|
|
$relationshipDatatype = "relationship:" . ($isInverse ? "inverse:" : "") . |
|
461
|
10 |
|
"$relationship:$sourceTypeName:$targetTypeName"; |
|
462
|
|
|
|
|
463
|
|
|
$this->processField($relationshipDatatype, $field, $directives, $isRequired); |
|
464
|
10 |
|
|
|
465
|
|
|
if ($generateRandom) { |
|
466
|
10 |
|
if ($relationship == RelationshipFactory::RELATIONSHIP_MANY_TO_MANY || $relationship == RelationshipFactory::MORPH_MANY_TO_MANY) { |
|
467
|
1 |
|
// TODO: do we generate it? seed should do it? |
|
468
|
|
|
} else { |
|
469
|
|
|
$this->methodRandom->addBody( |
|
470
|
10 |
|
'$data["' . $lowerName . '_id"] = function () {' . "\n" . |
|
471
|
10 |
|
' return factory(' . $targetClass . '::class)->create()->id;' . "\n" . |
|
472
|
10 |
|
'};' |
|
473
|
10 |
|
); |
|
474
|
10 |
|
} |
|
475
|
|
|
} |
|
476
|
10 |
|
} |
|
477
|
10 |
|
|
|
478
|
10 |
|
protected function processDirectives( |
|
479
|
10 |
|
\GraphQL\Language\AST\NodeList $directives |
|
480
|
10 |
|
): void { |
|
481
|
|
|
// TODO: convert to separate classes |
|
482
|
10 |
|
foreach ($directives as $directive) { |
|
483
|
9 |
|
$name = $directive->name->value; |
|
484
|
9 |
|
$this->fModel->appendExtradata(FormulariumUtils::directiveToExtradata($directive)); |
|
485
|
9 |
|
|
|
486
|
9 |
|
switch ($name) { |
|
487
|
9 |
|
case 'migrationSoftDeletes': |
|
488
|
|
|
$this->traits[] = '\Illuminate\Database\Eloquent\SoftDeletes'; |
|
489
|
|
|
break; |
|
490
|
10 |
|
case 'modelNotifiable': |
|
491
|
|
|
$this->traits[] = '\Illuminate\Notifications\Notifiable'; |
|
492
|
|
|
break; |
|
493
|
|
|
case 'modelMustVerifyEmail': |
|
494
|
|
|
$this->traits[] = '\Illuminate\Notifications\MustVerifyEmail'; |
|
495
|
|
|
break; |
|
496
|
|
|
case 'migrationRememberToken': |
|
497
|
|
|
$this->hidden[] = 'remember_token'; |
|
498
|
10 |
|
break; |
|
499
|
10 |
|
case 'migrationTimestamps': |
|
500
|
10 |
|
$this->migrationTimestamps = true; |
|
501
|
10 |
|
break; |
|
502
|
10 |
|
case 'modelExtends': |
|
503
|
10 |
|
foreach ($directive->arguments as $arg) { |
|
504
|
10 |
|
/** |
|
505
|
|
|
* @var \GraphQL\Language\AST\ArgumentNode $arg |
|
506
|
10 |
|
*/ |
|
507
|
|
|
|
|
508
|
|
|
$value = $arg->value->value; |
|
|
|
|
|
|
509
|
|
|
|
|
510
|
10 |
|
switch ($arg->name->value) { |
|
511
|
10 |
|
case 'class': |
|
512
|
10 |
|
$this->parentClassName = $value; |
|
513
|
10 |
|
} |
|
514
|
10 |
|
} |
|
515
|
10 |
|
break; |
|
516
|
|
|
case 'renderable': |
|
517
|
10 |
|
foreach ($directive->arguments as $arg) { |
|
518
|
|
|
/** |
|
519
|
|
|
* @var \GraphQL\Language\AST\ArgumentNode $arg |
|
520
|
10 |
|
*/ |
|
521
|
|
|
|
|
522
|
|
|
$argName = $arg->name->value; |
|
523
|
10 |
|
$argValue = $arg->value->value; /** @phpstan-ignore-line */ |
|
524
|
10 |
|
$this->fModel->appendRenderable($argName, $argValue); |
|
|
|
|
|
|
525
|
10 |
|
} |
|
526
|
10 |
|
break; |
|
527
|
10 |
|
} |
|
528
|
10 |
|
} |
|
529
|
10 |
|
} |
|
530
|
|
|
|
|
531
|
10 |
|
public function generateString(): string |
|
532
|
10 |
|
{ |
|
533
|
10 |
|
$namespace = new \Nette\PhpGenerator\PhpNamespace('App\\Models'); |
|
534
|
10 |
|
$namespace->addUse('\\Illuminate\\Database\\Eloquent\\Relations\\BelongsTo'); |
|
535
|
10 |
|
$namespace->addUse('\\Illuminate\\Database\\Eloquent\\Relations\\HasOne'); |
|
536
|
|
|
$namespace->addUse('\\Illuminate\\Database\\Eloquent\\Relations\\HasMany'); |
|
537
|
|
|
$namespace->addUse('\\Illuminate\\Database\\Eloquent\\Relations\\MorphTo'); |
|
538
|
|
|
$namespace->addUse('\\Illuminate\\Database\\Eloquent\\Relations\\MorphOne'); |
|
539
|
|
|
$namespace->addUse('\\Illuminate\\Database\\Eloquent\\Relations\\MorphToMany'); |
|
540
|
|
|
$namespace->addUse('\\Illuminate\\Support\\Facades\\Auth'); |
|
541
|
10 |
|
$namespace->addUse('\\Formularium\\Exception\\NoRandomException'); |
|
542
|
|
|
$namespace->addUse('\\Modelarium\\Laravel\\Datatypes\\Datatype_relationship'); |
|
543
|
|
|
|
|
544
|
10 |
|
$this->class = $namespace->addClass('Base' . $this->studlyName); |
|
545
|
10 |
|
$this->class |
|
546
|
10 |
|
->addComment("This file was automatically generated by Modelarium.") |
|
547
|
10 |
|
->setAbstract(); |
|
548
|
10 |
|
|
|
549
|
10 |
|
$this->methodRandom = new Method('getRandomData'); |
|
550
|
10 |
|
$this->methodRandom->addBody( |
|
551
|
10 |
|
'$data = static::getFormularium()->getRandom(get_called_class() . \'::getRandomFieldData\');' . "\n" |
|
552
|
10 |
|
); |
|
553
|
10 |
|
|
|
554
|
|
|
$this->processGraphql(); |
|
555
|
|
|
|
|
556
|
10 |
|
// this might have changed |
|
557
|
10 |
|
$this->class->setExtends($this->parentClassName); |
|
558
|
|
|
|
|
559
|
|
|
foreach ($this->traits as $trait) { |
|
560
|
10 |
|
$this->class->addTrait($trait); |
|
561
|
|
|
} |
|
562
|
10 |
|
|
|
563
|
10 |
|
$this->class->addProperty('fillable') |
|
564
|
|
|
->setProtected() |
|
565
|
10 |
|
->setValue($this->fillable) |
|
566
|
10 |
|
->setComment("The attributes that are mass assignable.\n@var array") |
|
567
|
10 |
|
->setInitialized(); |
|
568
|
10 |
|
|
|
569
|
10 |
|
$this->class->addProperty('hidden') |
|
570
|
10 |
|
->setProtected() |
|
571
|
10 |
|
->setValue($this->hidden) |
|
572
|
|
|
->setComment("The attributes that should be hidden for arrays.\n@var array") |
|
573
|
|
|
->setInitialized(); |
|
574
|
|
|
|
|
575
|
8 |
|
if (!$this->migrationTimestamps) { |
|
576
|
|
|
$this->class->addProperty('timestamps') |
|
577
|
10 |
|
->setPublic() |
|
578
|
|
|
->setValue(false) |
|
579
|
|
|
->setComment("Do not set timestamps.\n@var boolean") |
|
580
|
|
|
->setInitialized(); |
|
581
|
|
|
} |
|
582
|
|
|
|
|
583
|
|
|
if ($this->casts) { |
|
|
|
|
|
|
584
|
10 |
|
$this->class->addProperty('casts') |
|
585
|
10 |
|
->setProtected() |
|
586
|
10 |
|
->setValue($this->casts) |
|
587
|
|
|
->setComment("The attributes that should be cast.\n@var array") |
|
588
|
10 |
|
->setInitialized(); |
|
589
|
|
|
} |
|
590
|
10 |
|
|
|
591
|
|
|
$this->class->addMethod('getFields') |
|
592
|
10 |
|
->setPublic() |
|
593
|
|
|
->setStatic() |
|
594
|
|
|
->setReturnType('array') |
|
595
|
|
|
->addComment('@return array') |
|
596
|
|
|
->addBody( |
|
597
|
|
|
"return ?;\n", |
|
598
|
|
|
[ |
|
599
|
|
|
$this->fModel->serialize() |
|
600
|
|
|
] |
|
601
|
|
|
); |
|
602
|
|
|
|
|
603
|
|
|
$this->class->addMethod('getFormularium') |
|
604
|
|
|
->setPublic() |
|
605
|
|
|
->setStatic() |
|
606
|
|
|
->setReturnType('\Formularium\Model') |
|
607
|
|
|
->addComment('@return \Formularium\Model') |
|
608
|
|
|
->addBody( |
|
609
|
|
|
'$model = \Formularium\Model::fromStruct(static::getFields());' . "\n" . |
|
610
|
|
|
'return $model;', |
|
611
|
|
|
[ |
|
612
|
|
|
//$this->studlyName, |
|
613
|
|
|
] |
|
614
|
|
|
); |
|
615
|
|
|
|
|
616
|
|
|
$this->methodRandom |
|
617
|
|
|
->addComment('@return array') |
|
618
|
|
|
->setPublic() |
|
619
|
|
|
->setStatic() |
|
620
|
|
|
->setReturnType('array') |
|
621
|
|
|
->addBody('return $data;'); |
|
622
|
|
|
$this->class->addMember($this->methodRandom); |
|
623
|
|
|
|
|
624
|
|
|
$this->class->addMethod('getRandomFieldData') |
|
625
|
|
|
->setPublic() |
|
626
|
|
|
->setStatic() |
|
627
|
|
|
->addComment("Filters fields and generate random data. Throw NoRandomException for fields you don't want to generate random data, or return a valid value.") |
|
628
|
|
|
->addBody(' |
|
629
|
|
|
$d = $f->getDatatype(); |
|
630
|
|
|
if ($d instanceof Datatype_relationship) { |
|
631
|
|
|
throw new NoRandomException($f->getName()); |
|
632
|
|
|
} |
|
633
|
|
|
return $f->getDatatype()->getRandom();') |
|
634
|
|
|
->addParameter('f')->setType('Formularium\Field'); |
|
635
|
|
|
|
|
636
|
|
|
// TODO perhaps we can use PolicyGenerator->policyClasses to auto generate |
|
637
|
|
|
if ($this->hasCan) { |
|
638
|
|
|
$this->class->addMethod('getCanAttribute') |
|
639
|
|
|
->setPublic() |
|
640
|
|
|
->setReturnType('array') |
|
641
|
|
|
->addComment("Returns the policy permissions for actions such as editing or deleting.\n@return \Formularium\Model") |
|
642
|
|
|
->addBody( |
|
643
|
|
|
'$policy = new \\App\\Policies\\' . $this->studlyName . 'Policy();' . "\n" . |
|
644
|
|
|
'$user = Auth::user();' . "\n" . |
|
645
|
|
|
'return [' . "\n" . |
|
646
|
|
|
' //[ "ability" => "create", "value" => $policy->create($user) ]' . "\n" . |
|
647
|
|
|
'];' |
|
648
|
|
|
); |
|
649
|
|
|
} |
|
650
|
|
|
|
|
651
|
|
|
$printer = new \Nette\PhpGenerator\PsrPrinter; |
|
652
|
|
|
return $this->phpHeader() . $printer->printNamespace($namespace); |
|
653
|
|
|
} |
|
654
|
|
|
|
|
655
|
|
|
protected function processGraphql(): void |
|
656
|
|
|
{ |
|
657
|
|
|
foreach ($this->type->getFields() as $field) { |
|
658
|
|
|
$directives = $field->astNode->directives; |
|
659
|
|
|
if ( |
|
660
|
|
|
($field->type instanceof ObjectType) || |
|
661
|
|
|
($field->type instanceof ListOfType) || |
|
662
|
|
|
($field->type instanceof UnionType) || |
|
663
|
|
|
($field->type instanceof NonNull && ( |
|
664
|
|
|
($field->type->getWrappedType() instanceof ObjectType) || |
|
665
|
|
|
($field->type->getWrappedType() instanceof ListOfType) || |
|
666
|
|
|
($field->type->getWrappedType() instanceof UnionType) |
|
667
|
|
|
)) |
|
668
|
|
|
) { |
|
669
|
|
|
// relationship |
|
670
|
|
|
$this->processRelationship($field, $directives); |
|
671
|
|
|
} else { |
|
672
|
|
|
$this->processFieldDirectives($field, $directives); |
|
673
|
|
|
} |
|
674
|
|
|
} |
|
675
|
|
|
|
|
676
|
|
|
/** |
|
677
|
|
|
* @var \GraphQL\Language\AST\NodeList|null |
|
678
|
|
|
*/ |
|
679
|
|
|
$directives = $this->type->astNode->directives; |
|
680
|
|
|
if ($directives) { |
|
681
|
|
|
$this->processDirectives($directives); |
|
|
|
|
|
|
682
|
|
|
} |
|
683
|
|
|
} |
|
684
|
|
|
|
|
685
|
|
|
public function getGenerateFilename(bool $base = true): string |
|
686
|
|
|
{ |
|
687
|
|
|
return $this->getBasePath(self::$modelDir . '/' . ($base ? 'Base' : '') . $this->studlyName . '.php'); |
|
688
|
|
|
} |
|
689
|
|
|
|
|
690
|
|
|
public static function setModelDir(string $dir): void |
|
691
|
|
|
{ |
|
692
|
|
|
self::$modelDir = $dir; |
|
693
|
|
|
} |
|
694
|
|
|
} |
|
695
|
|
|
|