|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Modelarium\Laravel\Directives; |
|
4
|
|
|
|
|
5
|
|
|
use GraphQL\Type\Definition\ObjectType; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use Modelarium\Datatypes\RelationshipFactory; |
|
8
|
|
|
use Modelarium\Exception\DirectiveException; |
|
9
|
|
|
use Modelarium\Laravel\Targets\Interfaces\MigrationDirectiveInterface; |
|
10
|
|
|
use Modelarium\Laravel\Targets\ModelGenerator; |
|
11
|
|
|
use Modelarium\Laravel\Targets\SeedGenerator; |
|
12
|
|
|
use Modelarium\Laravel\Targets\Interfaces\ModelDirectiveInterface; |
|
13
|
|
|
use Modelarium\Laravel\Targets\Interfaces\SeedDirectiveInterface; |
|
14
|
|
|
use Modelarium\Laravel\Targets\MigrationCodeFragment; |
|
15
|
|
|
use Modelarium\Laravel\Targets\MigrationGenerator; |
|
16
|
|
|
use Modelarium\Parser; |
|
17
|
|
|
|
|
18
|
|
|
class BelongsToDirective implements MigrationDirectiveInterface, ModelDirectiveInterface, SeedDirectiveInterface |
|
19
|
|
|
{ |
|
20
|
|
|
public static function processMigrationTypeDirective( |
|
21
|
4 |
|
MigrationGenerator $generator, |
|
22
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
|
23
|
|
|
): void { |
|
24
|
|
|
throw new DirectiveException("Directive not supported here"); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public static function processMigrationFieldDirective( |
|
28
|
4 |
|
MigrationGenerator $generator, |
|
29
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
|
30
|
4 |
|
\GraphQL\Language\AST\DirectiveNode $directive, |
|
31
|
|
|
MigrationCodeFragment $code |
|
32
|
|
|
): void { |
|
33
|
|
|
throw new DirectiveException("Directive not supported here"); |
|
34
|
|
|
} |
|
35
|
4 |
|
|
|
36
|
4 |
|
public static function processMigrationRelationshipDirective( |
|
37
|
|
|
MigrationGenerator $generator, |
|
38
|
4 |
|
\GraphQL\Type\Definition\FieldDefinition $field, |
|
39
|
4 |
|
\GraphQL\Language\AST\DirectiveNode $directive, |
|
40
|
4 |
|
MigrationCodeFragment $codeFragment |
|
41
|
4 |
|
): void { |
|
42
|
|
|
$lowerName = mb_strtolower($generator->getInflector()->singularize($field->name)); |
|
43
|
4 |
|
$fieldName = $lowerName . '_id'; |
|
44
|
4 |
|
|
|
45
|
4 |
|
list($type, $isRequired) = Parser::getUnwrappedType($field->type); |
|
46
|
4 |
|
$typeName = $type->name; |
|
47
|
4 |
|
$tableName = MigrationGenerator::toTableName($typeName); |
|
48
|
4 |
|
|
|
49
|
4 |
|
$targetType = $generator->parser->getType($typeName); |
|
50
|
4 |
|
if (!$targetType) { |
|
|
|
|
|
|
51
|
|
|
throw new DirectiveException("Cannot get type {$typeName} as a relationship to {$this->baseName}"); |
|
|
|
|
|
|
52
|
4 |
|
} elseif (!($targetType instanceof ObjectType)) { |
|
53
|
4 |
|
throw new DirectiveException("{$typeName} is not a type for a relationship to {$this->baseName}"); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
// we don't know what is the reverse relationship name at this point. so let's guess all possibilities |
|
56
|
|
|
try { |
|
57
|
|
|
$targetField = $targetType->getField($tableName); |
|
58
|
|
|
} catch (\GraphQL\Error\InvariantViolation $e) { |
|
59
|
|
|
try { |
|
60
|
4 |
|
$targetField = $targetType->getField($generator->getTableName()); |
|
61
|
|
|
} catch (\GraphQL\Error\InvariantViolation $e) { |
|
62
|
|
|
// one to one |
|
63
|
|
|
$targetField = $targetType->getField($generator->getLowerName()); |
|
64
|
|
|
} |
|
65
|
4 |
|
} |
|
66
|
4 |
|
|
|
67
|
|
|
$targetDirectives = $targetField->astNode->directives; |
|
68
|
4 |
|
foreach ($targetDirectives as $targetDirective) { |
|
69
|
4 |
|
switch ($targetDirective->name->value) { |
|
70
|
4 |
|
case 'hasOne': |
|
71
|
|
|
case 'hasMany': |
|
72
|
4 |
|
$codeFragment->appendBase('->unsignedBigInteger("' . $fieldName . '")'); |
|
73
|
|
|
break; |
|
74
|
4 |
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public static function processModelTypeDirective( |
|
79
|
4 |
|
ModelGenerator $generator, |
|
80
|
4 |
|
\GraphQL\Language\AST\DirectiveNode $directive |
|
81
|
4 |
|
): void { |
|
82
|
|
|
// nothing |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
4 |
|
public static function processModelFieldDirective( |
|
86
|
4 |
|
ModelGenerator $generator, |
|
87
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
|
88
|
|
|
\Formularium\Field $fieldFormularium, |
|
89
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
|
90
|
|
|
): void { |
|
91
|
|
|
// nothing |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public static function processModelRelationshipDirective( |
|
95
|
|
|
ModelGenerator $generator, |
|
96
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
|
97
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
|
98
|
|
|
): string { |
|
99
|
|
|
$lowerName = mb_strtolower($generator->getInflector()->singularize($field->name)); |
|
100
|
|
|
$lowerNamePlural = $generator->getInflector()->pluralize($lowerName); |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
$sourceTypeName = $generator->getLowerName(); |
|
103
|
|
|
$targetTypeName = $lowerName; |
|
104
|
|
|
$relationship = null; |
|
|
|
|
|
|
105
|
|
|
$isInverse = false; |
|
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
$targetClass = Str::studly($generator->getInflector()->singularize($field->name)); |
|
108
|
|
|
$generateRandom = true; // TODO |
|
|
|
|
|
|
109
|
|
|
$relationship = RelationshipFactory::RELATIONSHIP_ONE_TO_MANY; |
|
110
|
|
|
$isInverse = true; |
|
111
|
|
|
$generator->class->addMethod($lowerName) |
|
112
|
|
|
->setPublic() |
|
113
|
|
|
->setReturnType('\\Illuminate\\Database\\Eloquent\\Relations\\BelongsTo') |
|
114
|
|
|
->setBody("return \$this->belongsTo($targetClass::class);"); |
|
115
|
|
|
|
|
116
|
|
|
return $generator->getRelationshipDatatypeName( |
|
117
|
|
|
$relationship, |
|
118
|
|
|
$isInverse, |
|
119
|
|
|
$sourceTypeName, |
|
120
|
|
|
$targetTypeName |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public static function processSeedFieldDirective( |
|
125
|
|
|
SeedGenerator $generator, |
|
126
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
|
127
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
|
128
|
|
|
): void { |
|
129
|
|
|
$type1 = $generator->getLowerName(); |
|
130
|
|
|
$type2 = mb_strtolower($generator->getInflector()->singularize($field->name)); |
|
131
|
|
|
|
|
132
|
|
|
if (strcasecmp($type1, $type2) < 0) { // TODO: check this, might not work |
|
133
|
|
|
$relationship = mb_strtolower($generator->getInflector()->pluralize($field->name)); |
|
134
|
|
|
$generator->extraCode[] = self::makeManyToManySeed($type1, $type2, $relationship); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
protected static function makeManyToManySeed(string $sourceModel, string $targetModel, string $relationship): string |
|
|
|
|
|
|
139
|
|
|
{ |
|
140
|
|
|
return <<<EOF |
|
141
|
|
|
|
|
142
|
|
|
try { |
|
143
|
|
|
\${$targetModel}Items = App\\Models\\$targetModel::all(); |
|
144
|
|
|
\$model->{$relationship}()->attach( |
|
145
|
|
|
\${$targetModel}Items->random(rand(1, 3))->pluck('id')->toArray() |
|
146
|
|
|
); |
|
147
|
|
|
} |
|
148
|
|
|
catch (\InvalidArgumentException \$e) { |
|
149
|
|
|
\$model->{$relationship}()->attach( |
|
150
|
|
|
\${$targetModel}Items->random(1)->pluck('id')->toArray() |
|
151
|
|
|
); |
|
152
|
|
|
} |
|
153
|
|
|
EOF; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|