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
|
|
|
*/ |
17
|
|
|
protected $stubDir = __DIR__ . "/stubs/"; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Extra seed code generated by parser |
21
|
|
|
* |
22
|
|
|
* @var string[] |
23
|
|
|
*/ |
24
|
|
|
public $extraCode = []; |
25
|
|
|
|
26
|
8 |
|
public function generate(): GeneratedCollection |
27
|
|
|
{ |
28
|
8 |
|
if (!($this->type instanceof ObjectType)) { |
29
|
|
|
throw new Exception('Invalid type on seed generator:' . get_class($this->type)); |
30
|
|
|
} |
31
|
|
|
/** |
32
|
|
|
* @var ObjectType $t |
33
|
|
|
*/ |
34
|
8 |
|
$t = $this->type; |
35
|
8 |
|
foreach ($t->getFields() as $field) { |
36
|
8 |
|
$directives = $field->astNode->directives; |
37
|
8 |
|
$this->processDirectives($field, $directives); |
38
|
|
|
} |
39
|
|
|
|
40
|
8 |
|
return new GeneratedCollection( |
41
|
8 |
|
[ new GeneratedItem( |
42
|
8 |
|
GeneratedItem::TYPE_SEED, |
43
|
8 |
|
$this->generateString(), |
44
|
8 |
|
$this->getGenerateFilename() |
45
|
|
|
)] |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
8 |
|
public function processDirectives( |
50
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
51
|
|
|
\GraphQL\Language\AST\NodeList $directives |
52
|
|
|
): void { |
53
|
8 |
|
foreach ($directives as $directive) { |
54
|
8 |
|
$name = $directive->name->value; |
55
|
8 |
|
$className = $this->getDirectiveClass($name); |
56
|
8 |
|
if ($className) { |
57
|
8 |
|
call_user_func( |
58
|
1 |
|
[$className, 'processSeedFieldDirective'], |
59
|
1 |
|
[$this, $field, $directive] |
60
|
1 |
|
); |
61
|
1 |
|
} |
62
|
1 |
|
} |
63
|
|
|
} |
64
|
1 |
|
|
65
|
7 |
|
public function generateString(): string |
66
|
|
|
{ |
67
|
1 |
|
return $this->templateStub( |
68
|
|
|
'seed', |
69
|
7 |
|
[ |
70
|
|
|
'extraCode' => join("\n", $this->extraCode) |
71
|
|
|
] |
72
|
8 |
|
); |
73
|
|
|
} |
74
|
1 |
|
|
75
|
|
|
public function getGenerateFilename(): string |
76
|
|
|
{ |
77
|
|
|
return $this->getBasePath('database/seeds/'. $this->studlyName . 'Seeder.php'); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|