1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Modelarium\Laravel\Targets; |
4
|
|
|
|
5
|
|
|
use GraphQL\Type\Definition\ObjectType; |
6
|
|
|
use Modelarium\BaseGenerator; |
7
|
|
|
use Modelarium\Exception\Exception; |
8
|
|
|
use Modelarium\GeneratedCollection; |
9
|
|
|
use Modelarium\GeneratedItem; |
10
|
|
|
use Modelarium\Parser; |
11
|
|
|
|
12
|
|
|
class SeedGenerator extends BaseGenerator |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
8 |
|
*/ |
17
|
|
|
protected $stubDir = __DIR__ . "/stubs/"; |
18
|
8 |
|
|
19
|
8 |
|
/** |
20
|
8 |
|
* Extra seed code generated by parser |
21
|
8 |
|
* |
22
|
8 |
|
* @var string[] |
23
|
|
|
*/ |
24
|
|
|
protected $extraCode = []; |
25
|
|
|
|
26
|
|
|
public function generate(): GeneratedCollection |
27
|
9 |
|
{ |
28
|
|
|
if (!($this->type instanceof ObjectType)) { |
29
|
9 |
|
throw new Exception('Invalid type on seed generator:' . get_class($this->type)); |
30
|
|
|
} |
31
|
|
|
/** |
32
|
8 |
|
* @var ObjectType $t |
33
|
|
|
*/ |
34
|
8 |
|
$t = $this->type; |
35
|
|
|
foreach ($t->getFields() as $field) { |
36
|
|
|
$directives = $field->astNode->directives; |
37
|
|
|
$this->processDirectives($field, $directives); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return new GeneratedCollection( |
41
|
|
|
[ new GeneratedItem( |
42
|
|
|
GeneratedItem::TYPE_SEED, |
43
|
|
|
$this->generateString(), |
44
|
|
|
$this->getGenerateFilename() |
45
|
|
|
)] |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function processDirectives( |
50
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
51
|
|
|
\GraphQL\Language\AST\NodeList $directives |
52
|
|
|
): void { |
53
|
|
|
$lowerName = mb_strtolower($this->getInflector()->singularize($field->name)); |
54
|
|
|
foreach ($directives as $directive) { |
55
|
|
|
$name = $directive->name->value; |
56
|
|
|
switch ($name) { |
57
|
|
|
case 'belongsToMany': |
58
|
|
|
$type1 = $this->lowerName; |
59
|
|
|
$type2 = $lowerName; |
60
|
|
|
if (strcasecmp($type1, $type2) < 0) { // TODO: check this, might not work |
61
|
|
|
$relationship = mb_strtolower($this->getInflector()->pluralize($field->name)); |
62
|
|
|
$this->extraCode[] = $this->makeManyToManySeed($type1, $type2, $relationship); |
63
|
|
|
} |
64
|
|
|
break; |
65
|
|
|
case 'morphedByMany': |
66
|
|
|
// TODO $relation = Parser::getDirectiveArgumentByName($directive, 'relation', $lowerName); |
67
|
|
|
break; |
68
|
|
|
default: |
69
|
|
|
break; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected static function makeManyToManySeed(string $sourceModel, string $targetModel, string $relationship): string |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
return <<<EOF |
77
|
|
|
|
78
|
|
|
try { |
79
|
|
|
\${$targetModel}Items = App\\Models\\$targetModel::all(); |
80
|
|
|
\$model->{$relationship}()->attach( |
81
|
|
|
\${$targetModel}Items->random(rand(1, 3))->pluck('id')->toArray() |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
catch (\InvalidArgumentException \$e) { |
85
|
|
|
\$model->{$relationship}()->attach( |
86
|
|
|
\${$targetModel}Items->random(1)->pluck('id')->toArray() |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
EOF; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function generateString(): string |
93
|
|
|
{ |
94
|
|
|
return $this->templateStub( |
95
|
|
|
'seed', |
96
|
|
|
[ |
97
|
|
|
'extraCode' => join("\n", $this->extraCode) |
98
|
|
|
] |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getGenerateFilename(): string |
103
|
|
|
{ |
104
|
|
|
return $this->getBasePath('database/seeds/'. $this->studlyName . 'Seeder.php'); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.