1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Modelarium\Laravel\Directives; |
4
|
|
|
|
5
|
|
|
use Faker\Provider\File; |
6
|
|
|
use Formularium\ExtradataParameter; |
7
|
|
|
use Formularium\Field; |
8
|
|
|
use GraphQL\Type\Definition\ObjectType; |
9
|
|
|
use GraphQL\Type\Definition\UnionType; |
10
|
|
|
use Illuminate\Support\Str; |
11
|
|
|
use Modelarium\Exception\Exception; |
12
|
|
|
use Modelarium\Parser; |
13
|
|
|
use Modelarium\Datatypes\RelationshipFactory; |
14
|
|
|
use Modelarium\Exception\DirectiveException; |
15
|
|
|
use Modelarium\Laravel\Targets\Interfaces\MigrationDirectiveInterface; |
16
|
|
|
use Modelarium\Laravel\Targets\ModelGenerator; |
17
|
|
|
use Modelarium\Laravel\Targets\SeedGenerator; |
18
|
|
|
use Modelarium\Laravel\Targets\Interfaces\ModelDirectiveInterface; |
19
|
|
|
use Modelarium\Laravel\Targets\Interfaces\SeedDirectiveInterface; |
20
|
|
|
use Modelarium\Laravel\Targets\MigrationCodeFragment; |
21
|
|
|
use Modelarium\Laravel\Targets\MigrationGenerator; |
22
|
|
|
|
23
|
|
|
class MorphToDirective implements MigrationDirectiveInterface, ModelDirectiveInterface, SeedDirectiveInterface |
24
|
|
|
{ |
25
|
|
|
public static function processMigrationTypeDirective( |
26
|
|
|
MigrationGenerator $generator, |
27
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
28
|
|
|
): void { |
29
|
|
|
throw new DirectiveException("Directive not supported here"); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public static function processMigrationFieldDirective( |
33
|
|
|
MigrationGenerator $generator, |
34
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
35
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive, |
36
|
|
|
MigrationCodeFragment $code |
37
|
|
|
): void { |
38
|
|
|
throw new DirectiveException("Directive not supported here"); |
39
|
|
|
} |
40
|
|
|
|
41
|
2 |
|
public static function processMigrationRelationshipDirective( |
42
|
|
|
MigrationGenerator $generator, |
43
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
44
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive, |
45
|
|
|
MigrationCodeFragment $codeFragment |
46
|
|
|
): void { |
47
|
2 |
|
$lowerName = mb_strtolower($generator->getInflector()->singularize($field->name)); |
48
|
2 |
|
list($type, $isRequired) = Parser::getUnwrappedType($field->type); |
49
|
2 |
|
$relation = Parser::getDirectiveArgumentByName($directive, 'relation', $lowerName); |
50
|
2 |
|
$codeFragment->appendBase('->unsignedBigInteger("' . $relation . '_id")'); |
51
|
2 |
|
$codeFragment->appendExtraLine( |
52
|
2 |
|
'$table->string("' . $relation . '_type")' . |
53
|
2 |
|
($isRequired ? '' : '->nullable()') . ';' |
54
|
|
|
); |
55
|
2 |
|
} |
56
|
|
|
|
57
|
|
|
public static function processModelTypeDirective( |
58
|
|
|
ModelGenerator $generator, |
59
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
60
|
|
|
): void { |
61
|
|
|
// nothing |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
public static function processModelFieldDirective( |
65
|
|
|
ModelGenerator $generator, |
66
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
67
|
|
|
\Formularium\Field $fieldFormularium, |
68
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
69
|
|
|
): void { |
70
|
2 |
|
list($type, $isRequired) = Parser::getUnwrappedType($field->type); |
71
|
2 |
|
$typeName = $type->name; |
72
|
|
|
|
73
|
2 |
|
if (!($type instanceof UnionType)) { |
74
|
|
|
throw new Exception("$typeName is declared as @morphTo target but it is not a union type."); |
75
|
|
|
} |
76
|
2 |
|
$unionTypes = $type->getTypes(); |
77
|
2 |
|
$morphableTargets = []; |
78
|
2 |
|
foreach ($unionTypes as $t) { |
79
|
2 |
|
if (!($t instanceof ObjectType)) { |
80
|
|
|
throw new Exception("$typeName is declared in a @morphTo union but it's not an object type."); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @var ObjectType $t |
85
|
|
|
*/ |
86
|
2 |
|
$morphableTargets[] = $t->name; |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
$fieldFormularium->getExtradata('morphTo') |
90
|
2 |
|
->appendParameter(new ExtradataParameter('targetModels', implode(',', $morphableTargets))); |
91
|
2 |
|
} |
92
|
|
|
|
93
|
2 |
|
public static function processModelRelationshipDirective( |
94
|
|
|
ModelGenerator $generator, |
95
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
96
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
97
|
|
|
): string { |
98
|
2 |
|
$lowerName = mb_strtolower($generator->getInflector()->singularize($field->name)); |
99
|
|
|
|
100
|
2 |
|
$sourceTypeName = $generator->getLowerName(); |
101
|
2 |
|
$targetTypeName = $lowerName; |
102
|
2 |
|
$relationship = null; |
|
|
|
|
103
|
2 |
|
$isInverse = false; |
|
|
|
|
104
|
2 |
|
$generateRandom = true; // TODO |
|
|
|
|
105
|
|
|
|
106
|
2 |
|
$relationship = RelationshipFactory::MORPH_ONE_TO_MANY; // TODO |
107
|
2 |
|
$isInverse = true; |
108
|
2 |
|
$generator->class->addMethod($field->name) |
109
|
2 |
|
->setReturnType('\\Illuminate\\Database\\Eloquent\\Relations\\MorphTo') |
110
|
2 |
|
->setPublic() |
111
|
2 |
|
->setBody("return \$this->morphTo();"); |
112
|
|
|
|
113
|
2 |
|
return $generator->getRelationshipDatatypeName( |
114
|
2 |
|
$relationship, |
115
|
|
|
$isInverse, |
116
|
|
|
$sourceTypeName, |
117
|
|
|
$targetTypeName |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
2 |
|
public static function processSeedFieldDirective( |
122
|
|
|
SeedGenerator $generator, |
123
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
124
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
125
|
|
|
): void { |
126
|
2 |
|
$type1 = $generator->getLowerName(); |
127
|
2 |
|
$type2 = mb_strtolower($generator->getInflector()->singularize($field->name)); |
128
|
|
|
|
129
|
2 |
|
if (strcasecmp($type1, $type2) < 0) { // TODO: check this, might not work |
130
|
2 |
|
$relationship = mb_strtolower($generator->getInflector()->pluralize($field->name)); |
131
|
2 |
|
$generator->extraCode[] = self::makeManyToManySeed($type1, $type2, $relationship); |
132
|
|
|
} |
133
|
2 |
|
} |
134
|
|
|
|
135
|
2 |
|
protected static function makeManyToManySeed(string $sourceModel, string $targetModel, string $relationship): string |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
return <<<EOF |
138
|
|
|
|
139
|
|
|
try { |
140
|
2 |
|
\${$targetModel}Items = App\\Models\\$targetModel::all(); |
141
|
2 |
|
\$model->{$relationship}()->attach( |
142
|
2 |
|
\${$targetModel}Items->random(rand(1, 3))->pluck('id')->toArray() |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
catch (\InvalidArgumentException \$e) { |
146
|
2 |
|
\$model->{$relationship}()->attach( |
147
|
2 |
|
\${$targetModel}Items->random(1)->pluck('id')->toArray() |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
EOF; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|