|
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 GraphQL\Type\Definition\ListOfType; |
|
11
|
|
|
use GraphQL\Type\Definition\NonNull; |
|
12
|
|
|
use GraphQL\Type\Definition\ObjectType; |
|
13
|
|
|
use GraphQL\Type\Definition\UnionType; |
|
14
|
|
|
use GraphQL\Language\AST\DirectiveNode; |
|
15
|
|
|
use Modelarium\BaseGenerator; |
|
16
|
|
|
use Modelarium\Exception\Exception; |
|
17
|
|
|
use Modelarium\FormulariumUtils; |
|
18
|
|
|
use Modelarium\GeneratedCollection; |
|
19
|
|
|
use Modelarium\GeneratedItem; |
|
20
|
|
|
use Modelarium\Parser; |
|
21
|
|
|
use Modelarium\Types\FormulariumScalarType; |
|
22
|
|
|
use Nette\PhpGenerator\Method; |
|
23
|
|
|
|
|
24
|
|
|
class ModelGenerator extends BaseGenerator |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $stubDir = __DIR__ . "/stubs/"; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected static $modelDir = 'app/Models/'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var ObjectType |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $type = null; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var \Nette\PhpGenerator\ClassType |
|
44
|
|
|
*/ |
|
45
|
|
|
public $class = null; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* fillable attributes |
|
49
|
|
|
* |
|
50
|
|
|
* @var array |
|
51
|
|
|
*/ |
|
52
|
|
|
public $fillable = []; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* fillable attributes |
|
56
|
|
|
* |
|
57
|
|
|
* @var array |
|
58
|
|
|
*/ |
|
59
|
|
|
public $hidden = []; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* cast attributes |
|
63
|
|
|
* |
|
64
|
|
|
* @var array |
|
65
|
|
|
*/ |
|
66
|
|
|
public $casts = []; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* |
|
70
|
|
|
* @var string |
|
71
|
|
|
*/ |
|
72
|
|
|
public $parentClassName = '\Illuminate\Database\Eloquent\Model'; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* fields |
|
76
|
|
|
* |
|
77
|
|
|
* @var Model |
|
78
|
|
|
*/ |
|
79
|
|
|
public $fModel = null; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* traits to include |
|
83
|
|
|
* @var array |
|
84
|
|
|
*/ |
|
85
|
|
|
public $traits = []; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Eager loading |
|
89
|
|
|
* |
|
90
|
|
|
* @var string[] |
|
91
|
|
|
*/ |
|
92
|
|
|
public $with = []; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Random generation |
|
96
|
|
|
* |
|
97
|
|
|
* @var Method |
|
98
|
|
|
*/ |
|
99
|
|
|
protected $methodRandom = null; |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Do we have a 'can' attribute? |
|
103
|
|
|
* |
|
104
|
|
|
* @var boolean |
|
105
|
|
|
*/ |
|
106
|
|
|
protected $hasCan = false; |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* If true, we have timestamps on the migration. |
|
110
|
|
|
* |
|
111
|
|
|
* @var boolean |
|
112
|
|
|
*/ |
|
113
|
|
|
public $migrationTimestamps = false; |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Undocumented variable |
|
117
|
|
|
* |
|
118
|
|
|
* @var GeneratedCollection |
|
119
|
|
|
*/ |
|
120
|
|
|
public $generatedCollection = null; |
|
121
|
|
|
|
|
122
|
10 |
|
public function generate(): GeneratedCollection |
|
123
|
|
|
{ |
|
124
|
10 |
|
$this->generatedCollection = new GeneratedCollection(); |
|
125
|
10 |
|
$this->fModel = Model::create($this->studlyName); |
|
126
|
10 |
|
$this->generatedCollection->push(new GeneratedItem( |
|
127
|
10 |
|
GeneratedItem::TYPE_MODEL, |
|
128
|
10 |
|
$this->generateString(), |
|
129
|
10 |
|
$this->getGenerateFilename() |
|
130
|
|
|
)); |
|
131
|
10 |
|
$this->generatedCollection->push(new GeneratedItem( |
|
132
|
10 |
|
GeneratedItem::TYPE_MODEL, |
|
133
|
10 |
|
$this->templateStub('model'), |
|
134
|
10 |
|
$this->getGenerateFilename(false), |
|
135
|
10 |
|
true |
|
136
|
|
|
)); |
|
137
|
10 |
|
return $this->generatedCollection; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Override to insert extradata |
|
142
|
|
|
* |
|
143
|
|
|
* @param \GraphQL\Language\AST\NodeList<\GraphQL\Language\AST\DirectiveNode> $directives |
|
144
|
|
|
* @param string $generatorType |
|
145
|
|
|
* @return void |
|
146
|
|
|
*/ |
|
147
|
10 |
|
protected function processTypeDirectives( |
|
148
|
|
|
\GraphQL\Language\AST\NodeList $directives, |
|
149
|
|
|
string $generatorType |
|
150
|
|
|
): void { |
|
151
|
10 |
|
foreach ($directives as $directive) { |
|
152
|
1 |
|
$name = $directive->name->value; |
|
153
|
1 |
|
$this->fModel->appendExtradata(FormulariumUtils::directiveToExtradata($directive)); |
|
154
|
|
|
|
|
155
|
1 |
|
$className = $this->getDirectiveClass($name, $generatorType); |
|
156
|
1 |
|
if ($className) { |
|
157
|
1 |
|
$methodName = "$className::process{$generatorType}TypeDirective"; |
|
158
|
|
|
/** @phpstan-ignore-next-line */ |
|
159
|
1 |
|
$methodName( |
|
160
|
1 |
|
$this, |
|
161
|
|
|
$directive |
|
162
|
|
|
); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
10 |
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @param string $typeName |
|
169
|
|
|
* @param \GraphQL\Type\Definition\FieldDefinition $field |
|
170
|
|
|
* @param \GraphQL\Language\AST\NodeList<DirectiveNode> $directives |
|
171
|
|
|
* @param boolean $isRequired |
|
172
|
|
|
* @return void |
|
173
|
|
|
*/ |
|
174
|
10 |
|
protected function processField( |
|
175
|
|
|
string $typeName, |
|
176
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
|
177
|
|
|
\GraphQL\Language\AST\NodeList $directives, |
|
178
|
|
|
bool $isRequired |
|
179
|
|
|
): void { |
|
180
|
10 |
|
$fieldName = $field->name; |
|
181
|
|
|
|
|
182
|
10 |
|
if ($typeName === 'ID') { |
|
183
|
10 |
|
return; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
10 |
|
$scalarType = $this->parser->getScalarType($typeName); |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @var Field $fieldFormularium |
|
190
|
|
|
*/ |
|
191
|
10 |
|
$fieldFormularium = null; |
|
192
|
10 |
|
if (!$scalarType) { |
|
193
|
|
|
// probably another model |
|
194
|
8 |
|
$fieldFormularium = FormulariumUtils::getFieldFromDirectives( |
|
195
|
8 |
|
$fieldName, |
|
196
|
8 |
|
$typeName, |
|
197
|
8 |
|
$directives |
|
198
|
|
|
); |
|
199
|
5 |
|
} elseif ($scalarType instanceof FormulariumScalarType) { |
|
200
|
5 |
|
$fieldFormularium = FormulariumUtils::getFieldFromDirectives( |
|
201
|
5 |
|
$fieldName, |
|
202
|
5 |
|
$scalarType->getDatatype()->getName(), |
|
203
|
5 |
|
$directives |
|
204
|
|
|
); |
|
205
|
|
|
} else { |
|
206
|
|
|
return; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
10 |
|
if ($isRequired) { |
|
210
|
10 |
|
$fieldFormularium->setValidatorOption( |
|
211
|
10 |
|
Datatype::REQUIRED, |
|
212
|
10 |
|
'value', |
|
213
|
10 |
|
true |
|
214
|
|
|
); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
10 |
|
foreach ($directives as $directive) { |
|
218
|
8 |
|
$name = $directive->name->value; |
|
219
|
8 |
|
$className = $this->getDirectiveClass($name); |
|
220
|
8 |
|
if ($className) { |
|
221
|
8 |
|
$methodName = "$className::processModelFieldDirective"; |
|
222
|
|
|
/** @phpstan-ignore-next-line */ |
|
223
|
8 |
|
$methodName( |
|
224
|
8 |
|
$this, |
|
225
|
|
|
$field, |
|
226
|
|
|
$fieldFormularium, |
|
227
|
|
|
$directive |
|
228
|
|
|
); |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
10 |
|
$this->fModel->appendField($fieldFormularium); |
|
233
|
10 |
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* @param \GraphQL\Type\Definition\FieldDefinition $field |
|
237
|
|
|
* @param \GraphQL\Language\AST\NodeList<DirectiveNode> $directives |
|
238
|
|
|
* @return void |
|
239
|
|
|
*/ |
|
240
|
8 |
|
protected function processRelationship( |
|
241
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
|
242
|
|
|
\GraphQL\Language\AST\NodeList $directives |
|
243
|
|
|
): void { |
|
244
|
8 |
|
list($type, $isRequired) = Parser::getUnwrappedType($field->getType()); |
|
245
|
8 |
|
$typeName = $type->name; |
|
246
|
|
|
|
|
247
|
|
|
// special types that should be skipped. |
|
248
|
8 |
|
if ($typeName === 'Can') { |
|
249
|
|
|
$this->hasCan = true; |
|
250
|
|
|
$this->fModel->appendExtradata( |
|
251
|
|
|
new Extradata( |
|
252
|
|
|
'hasCan', |
|
253
|
|
|
[ new ExtradataParameter('value', true) ] |
|
254
|
|
|
) |
|
255
|
|
|
); |
|
256
|
|
|
return; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
8 |
|
$relationshipDatatype = null; |
|
260
|
|
|
|
|
261
|
8 |
|
foreach ($directives as $directive) { |
|
262
|
8 |
|
$name = $directive->name->value; |
|
263
|
|
|
|
|
264
|
8 |
|
$className = $this->getDirectiveClass($name); |
|
265
|
8 |
|
if ($className) { |
|
266
|
8 |
|
$methodName = "$className::processModelRelationshipDirective"; |
|
267
|
|
|
/** @phpstan-ignore-next-line */ |
|
268
|
8 |
|
$r = $methodName( |
|
269
|
8 |
|
$this, |
|
270
|
|
|
$field, |
|
271
|
|
|
$directive, |
|
272
|
|
|
$relationshipDatatype |
|
273
|
|
|
); |
|
274
|
8 |
|
if ($r) { |
|
275
|
8 |
|
if ($relationshipDatatype) { |
|
276
|
|
|
throw new Exception("Overwriting relationship in {$typeName} for {$field->name} in {$this->lowerName}"); |
|
277
|
|
|
} |
|
278
|
8 |
|
$relationshipDatatype = $r; |
|
279
|
|
|
} |
|
280
|
8 |
|
continue; |
|
281
|
|
|
} |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
8 |
|
if (!$relationshipDatatype) { |
|
285
|
|
|
// if target is a model... |
|
286
|
|
|
$targetType = $this->parser->getSchema()->getType($typeName); |
|
287
|
|
|
$directives = $targetType->astNode->directives; |
|
|
|
|
|
|
288
|
|
|
$skip = false; |
|
289
|
|
|
foreach ($directives as $directive) { |
|
290
|
|
|
$dName = $directive->name->value; |
|
291
|
|
|
if ($dName === 'typeSkip') { |
|
292
|
|
|
$skip = true; |
|
293
|
|
|
break; |
|
294
|
|
|
} |
|
295
|
|
|
} |
|
296
|
|
|
if ($skip == false) { |
|
|
|
|
|
|
297
|
|
|
$this->warn("Could not find a relationship {$typeName} for {$field->name} in {$this->baseName}. Consider adding a @modelAccessor or declaring the relationship (e.g. @hasMany, @belongsTo)."); |
|
298
|
|
|
} |
|
299
|
|
|
return; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
8 |
|
$this->processField($relationshipDatatype->getName(), $field, $directives, $isRequired); |
|
303
|
|
|
|
|
304
|
|
|
// TODO |
|
305
|
|
|
// if ($generateRandom) { |
|
306
|
|
|
// if ($relationship == RelationshipFactory::RELATIONSHIP_MANY_TO_MANY || $relationship == RelationshipFactory::MORPH_MANY_TO_MANY) { |
|
307
|
|
|
// // TODO: do we generate it? seed should do it? |
|
308
|
|
|
// } else { |
|
309
|
|
|
// $this->methodRandom->addBody( |
|
310
|
|
|
// '$data["' . $lowerName . '_id"] = function () {' . "\n" . |
|
311
|
|
|
// ' return factory(' . $targetClass . '::class)->create()->id;' . "\n" . |
|
312
|
|
|
// '};' |
|
313
|
|
|
// ); |
|
314
|
|
|
// } |
|
315
|
|
|
// } |
|
316
|
8 |
|
} |
|
317
|
|
|
|
|
318
|
8 |
|
public static function getRelationshipDatatypeName( |
|
319
|
|
|
string $relationship, |
|
320
|
|
|
bool $isInverse, |
|
321
|
|
|
string $sourceTypeName, |
|
322
|
|
|
string $targetTypeName |
|
323
|
|
|
): string { |
|
324
|
8 |
|
return "relationship:" . ($isInverse ? "inverse:" : "") . |
|
325
|
8 |
|
"$relationship:$sourceTypeName:$targetTypeName"; |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
10 |
|
public function generateString(): string |
|
329
|
|
|
{ |
|
330
|
10 |
|
$namespace = new \Nette\PhpGenerator\PhpNamespace('App\\Models'); |
|
331
|
10 |
|
$namespace->addUse('\\Illuminate\\Database\\Eloquent\\Relations\\BelongsTo'); |
|
332
|
10 |
|
$namespace->addUse('\\Illuminate\\Database\\Eloquent\\Relations\\BelongsToMany'); |
|
333
|
10 |
|
$namespace->addUse('\\Illuminate\\Database\\Eloquent\\Relations\\HasOne'); |
|
334
|
10 |
|
$namespace->addUse('\\Illuminate\\Database\\Eloquent\\Relations\\HasMany'); |
|
335
|
10 |
|
$namespace->addUse('\\Illuminate\\Database\\Eloquent\\Relations\\MorphTo'); |
|
336
|
10 |
|
$namespace->addUse('\\Illuminate\\Database\\Eloquent\\Relations\\MorphOne'); |
|
337
|
10 |
|
$namespace->addUse('\\Illuminate\\Database\\Eloquent\\Relations\\MorphToMany'); |
|
338
|
10 |
|
$namespace->addUse('\\Illuminate\\Database\\Eloquent\\Builder'); |
|
339
|
10 |
|
$namespace->addUse('\\Illuminate\\Support\\Facades\\Auth'); |
|
340
|
10 |
|
$namespace->addUse('\\Formularium\\Exception\\NoRandomException'); |
|
341
|
10 |
|
$namespace->addUse('\\Modelarium\\Laravel\\Datatypes\\Datatype_relationship'); |
|
342
|
|
|
|
|
343
|
10 |
|
$this->class = $namespace->addClass('Base' . $this->studlyName); |
|
344
|
10 |
|
$this->class |
|
345
|
10 |
|
->addComment("This file was automatically generated by Modelarium.") |
|
346
|
10 |
|
->setAbstract(); |
|
347
|
|
|
|
|
348
|
10 |
|
$this->methodRandom = new Method('getRandomData'); |
|
349
|
10 |
|
$this->methodRandom->addBody( |
|
350
|
10 |
|
'$data = static::getFormularium()->getRandom(get_called_class() . \'::getRandomFieldData\');' . "\n" |
|
351
|
|
|
); |
|
352
|
|
|
|
|
353
|
10 |
|
$this->processGraphql(); |
|
354
|
|
|
|
|
355
|
|
|
// this might have changed |
|
356
|
10 |
|
$this->class->setExtends($this->parentClassName); |
|
357
|
|
|
|
|
358
|
10 |
|
foreach ($this->traits as $trait) { |
|
359
|
1 |
|
$this->class->addTrait($trait); |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
10 |
|
$this->class->addProperty('fillable') |
|
363
|
10 |
|
->setProtected() |
|
364
|
10 |
|
->setValue($this->fillable) |
|
365
|
10 |
|
->setComment("The attributes that are mass assignable.\n@var array") |
|
366
|
10 |
|
->setInitialized(); |
|
367
|
|
|
|
|
368
|
10 |
|
$this->class->addProperty('hidden') |
|
369
|
10 |
|
->setProtected() |
|
370
|
10 |
|
->setValue($this->hidden) |
|
371
|
10 |
|
->setComment("The attributes that should be hidden for arrays.\n@var array") |
|
372
|
10 |
|
->setInitialized(); |
|
373
|
|
|
|
|
374
|
10 |
|
$this->class->addProperty('with') |
|
375
|
10 |
|
->setProtected() |
|
376
|
10 |
|
->setValue($this->with) |
|
377
|
10 |
|
->setComment("Eager load these relationships.\n@var array") |
|
378
|
10 |
|
->setInitialized(); |
|
379
|
|
|
|
|
380
|
10 |
|
if (!$this->migrationTimestamps) { |
|
381
|
9 |
|
$this->class->addProperty('timestamps') |
|
382
|
9 |
|
->setPublic() |
|
383
|
9 |
|
->setValue(false) |
|
384
|
9 |
|
->setComment("Do not set timestamps.\n@var boolean") |
|
385
|
9 |
|
->setInitialized(); |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
10 |
|
if ($this->casts) { |
|
|
|
|
|
|
389
|
|
|
$this->class->addProperty('casts') |
|
390
|
|
|
->setProtected() |
|
391
|
|
|
->setValue($this->casts) |
|
392
|
|
|
->setComment("The attributes that should be cast.\n@var array") |
|
393
|
|
|
->setInitialized(); |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
10 |
|
$this->class->addMethod('getFields') |
|
397
|
10 |
|
->setPublic() |
|
398
|
10 |
|
->setStatic() |
|
399
|
10 |
|
->setReturnType('array') |
|
400
|
10 |
|
->addComment('@return array') |
|
401
|
10 |
|
->addBody( |
|
402
|
10 |
|
"return ?;\n", |
|
403
|
|
|
[ |
|
404
|
10 |
|
$this->fModel->serialize() |
|
405
|
|
|
] |
|
406
|
|
|
); |
|
407
|
|
|
|
|
408
|
10 |
|
$this->class->addMethod('getFormularium') |
|
409
|
10 |
|
->setPublic() |
|
410
|
10 |
|
->setStatic() |
|
411
|
10 |
|
->setReturnType('\Formularium\Model') |
|
412
|
10 |
|
->addComment('@return \Formularium\Model') |
|
413
|
10 |
|
->addBody( |
|
414
|
|
|
'$model = \Formularium\Model::fromStruct(static::getFields());' . "\n" . |
|
415
|
10 |
|
'return $model;', |
|
416
|
|
|
[ |
|
417
|
|
|
//$this->studlyName, |
|
418
|
10 |
|
] |
|
419
|
|
|
); |
|
420
|
|
|
|
|
421
|
10 |
|
$this->methodRandom |
|
422
|
10 |
|
->addComment('@return array') |
|
423
|
10 |
|
->setPublic() |
|
424
|
10 |
|
->setStatic() |
|
425
|
10 |
|
->setReturnType('array') |
|
426
|
10 |
|
->addBody('return $data;'); |
|
427
|
10 |
|
$this->class->addMember($this->methodRandom); |
|
428
|
|
|
|
|
429
|
10 |
|
$getRandomFieldData = $this->class->addMethod('getRandomFieldData') |
|
430
|
10 |
|
->setPublic() |
|
431
|
10 |
|
->setStatic() |
|
432
|
10 |
|
->addComment("Filters fields and generate random data. Throw NoRandomException for fields you don't want to generate random data, or return a valid value.") |
|
433
|
10 |
|
->addBody(' |
|
434
|
|
|
$d = $field->getDatatype(); |
|
435
|
|
|
if ($field->getExtradata("migrationSkip")) { |
|
436
|
|
|
throw new NoRandomException($field->getName()); |
|
437
|
|
|
} |
|
438
|
|
|
if ($d instanceof Datatype_relationship) { |
|
439
|
|
|
if (!$d->getIsInverse() || !$field->getValidatorOption("required", "value", false)) { |
|
440
|
|
|
throw new NoRandomException($field->getName()); |
|
441
|
|
|
} |
|
442
|
|
|
$data[$field->getName() . "_id"] = $field->getDatatype()->getRandom(); |
|
443
|
|
|
} else { |
|
444
|
|
|
$data[$field->getName()] = $field->getDatatype()->getRandom(); |
|
445
|
|
|
}'); |
|
446
|
10 |
|
$getRandomFieldData->addParameter('field')->setType('Formularium\Field'); |
|
447
|
10 |
|
$getRandomFieldData->addParameter('model')->setType('Formularium\Model'); |
|
448
|
10 |
|
$getRandomFieldData->addParameter('data')->setType('array')->setReference(true); |
|
449
|
|
|
|
|
450
|
|
|
// TODO perhaps we can use PolicyGenerator->policyClasses to auto generate |
|
451
|
|
|
|
|
452
|
10 |
|
if ($this->hasCan) { |
|
453
|
|
|
$this->class->addMethod('getCanAttribute') |
|
454
|
|
|
->setPublic() |
|
455
|
|
|
->setReturnType('array') |
|
456
|
|
|
->addComment("Returns the policy permissions for actions such as editing or deleting.\n@return array") |
|
457
|
|
|
->addBody( |
|
458
|
|
|
'$policy = new \\App\\Policies\\' . $this->studlyName . 'Policy();' . "\n" . |
|
459
|
|
|
'$user = Auth::user();' . "\n" . |
|
460
|
|
|
'return [' . "\n" . |
|
461
|
|
|
' //[ "ability" => "create", "value" => $policy->create($user) ]' . "\n" . |
|
462
|
|
|
'];' |
|
463
|
|
|
); |
|
464
|
|
|
|
|
465
|
|
|
/* This creates a policy, but it's not useful. It's an empty file and @can won't patch it for now |
|
466
|
|
|
if (!class_exists('\\App\\Policies\\' . $this->studlyName . 'Policy')) { |
|
467
|
|
|
$policyGenerator = new PolicyGenerator($this->parser, 'Mutation', $this->type); |
|
468
|
|
|
$z = $policyGenerator->getPolicyClass($this->studlyName); |
|
469
|
|
|
$x = $policyGenerator->generate(); |
|
470
|
|
|
$this->generatedCollection = $this->generatedCollection->merge($x); |
|
471
|
|
|
} |
|
472
|
|
|
*/ |
|
473
|
|
|
} |
|
474
|
|
|
|
|
475
|
10 |
|
$printer = new \Nette\PhpGenerator\PsrPrinter; |
|
476
|
10 |
|
return $this->phpHeader() . $printer->printNamespace($namespace); |
|
477
|
|
|
} |
|
478
|
|
|
|
|
479
|
10 |
|
protected function processGraphql(): void |
|
480
|
|
|
{ |
|
481
|
10 |
|
foreach ($this->type->getFields() as $field) { |
|
482
|
10 |
|
$directives = $field->astNode->directives; |
|
483
|
10 |
|
$type = $field->getType(); |
|
484
|
|
|
if ( |
|
485
|
10 |
|
($type instanceof ObjectType) || |
|
486
|
10 |
|
($type instanceof ListOfType) || |
|
487
|
10 |
|
($type instanceof UnionType) || |
|
488
|
10 |
|
($type instanceof NonNull && ( |
|
489
|
10 |
|
($type->getWrappedType() instanceof ObjectType) || |
|
490
|
10 |
|
($type->getWrappedType() instanceof ListOfType) || |
|
491
|
10 |
|
($type->getWrappedType() instanceof UnionType) |
|
492
|
|
|
)) |
|
493
|
|
|
) { |
|
494
|
|
|
// relationship |
|
495
|
8 |
|
$this->processRelationship($field, $directives); |
|
496
|
|
|
} else { |
|
497
|
10 |
|
list($type, $isRequired) = Parser::getUnwrappedType($field->getType()); |
|
498
|
10 |
|
$typeName = $type->name; |
|
499
|
10 |
|
$this->processField($typeName, $field, $directives, $isRequired); |
|
500
|
|
|
} |
|
501
|
|
|
} |
|
502
|
|
|
|
|
503
|
|
|
/** |
|
504
|
|
|
* @var \GraphQL\Language\AST\NodeList<\GraphQL\Language\AST\DirectiveNode>|null |
|
505
|
|
|
*/ |
|
506
|
10 |
|
$directives = $this->type->astNode->directives; |
|
507
|
10 |
|
if ($directives) { |
|
|
|
|
|
|
508
|
10 |
|
$this->processTypeDirectives($directives, 'Model'); |
|
509
|
|
|
} |
|
510
|
10 |
|
} |
|
511
|
|
|
|
|
512
|
10 |
|
public function getGenerateFilename(bool $base = true): string |
|
513
|
|
|
{ |
|
514
|
10 |
|
return $this->getBasePath(self::$modelDir . '/' . ($base ? 'Base' : '') . $this->studlyName . '.php'); |
|
515
|
|
|
} |
|
516
|
|
|
|
|
517
|
|
|
public static function setModelDir(string $dir): void |
|
518
|
|
|
{ |
|
519
|
|
|
self::$modelDir = $dir; |
|
520
|
|
|
} |
|
521
|
|
|
} |
|
522
|
|
|
|