|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Modelarium\Laravel\Targets; |
|
4
|
|
|
|
|
5
|
|
|
use Formularium\Datatype\Datatype_enum; |
|
6
|
|
|
use Formularium\Exception\ClassNotFoundException; |
|
7
|
|
|
use Formularium\Factory\DatatypeFactory; |
|
8
|
|
|
use Illuminate\Support\Str; |
|
9
|
|
|
use GraphQL\Language\AST\DirectiveNode; |
|
10
|
|
|
use GraphQL\Type\Definition\BooleanType; |
|
11
|
|
|
use GraphQL\Type\Definition\CustomScalarType; |
|
12
|
|
|
use GraphQL\Type\Definition\Directive; |
|
13
|
|
|
use GraphQL\Type\Definition\EnumType; |
|
14
|
|
|
use GraphQL\Type\Definition\FloatType; |
|
15
|
|
|
use GraphQL\Type\Definition\IDType; |
|
16
|
|
|
use GraphQL\Type\Definition\IntType; |
|
17
|
|
|
use GraphQL\Type\Definition\ListOfType; |
|
18
|
|
|
use GraphQL\Type\Definition\NonNull; |
|
19
|
|
|
use GraphQL\Type\Definition\ObjectType; |
|
20
|
|
|
use GraphQL\Type\Definition\StringType; |
|
21
|
|
|
use GraphQL\Type\Definition\UnionType; |
|
22
|
|
|
use Modelarium\BaseGenerator; |
|
23
|
|
|
use Modelarium\Exception\Exception; |
|
24
|
|
|
use Modelarium\Exception\ScalarNotFoundException; |
|
25
|
|
|
use Modelarium\GeneratedCollection; |
|
26
|
|
|
use Modelarium\GeneratedItem; |
|
27
|
|
|
use Modelarium\Parser; |
|
28
|
|
|
use Modelarium\Types\FormulariumScalarType; |
|
29
|
|
|
use Nette\PhpGenerator\ClassType; |
|
30
|
|
|
|
|
31
|
|
|
use function Safe\array_combine; |
|
32
|
|
|
use function Safe\rsort; |
|
33
|
|
|
use function Safe\date; |
|
34
|
|
|
|
|
35
|
|
|
function getStringBetween(string $string, string $start, string $end): string |
|
36
|
|
|
{ |
|
37
|
|
|
$ini = mb_strpos($string, $start); |
|
38
|
|
|
if ($ini === false) { |
|
39
|
|
|
return ''; |
|
40
|
|
|
} |
|
41
|
|
|
$ini += mb_strlen($start); |
|
42
|
|
|
$len = mb_strpos($string, $end, $ini) - $ini; |
|
43
|
|
|
return mb_substr($string, $ini, $len); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
function endsWith(string $haystack, string $needle): bool |
|
47
|
|
|
{ |
|
48
|
|
|
return substr_compare($haystack, $needle, -strlen($needle)) === 0; |
|
49
|
|
|
} |
|
50
|
|
|
class MigrationGenerator extends BaseGenerator |
|
51
|
|
|
{ |
|
52
|
|
|
/** |
|
53
|
|
|
* @var string |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $stubDir = __DIR__ . "/stubs/"; |
|
56
|
|
|
|
|
57
|
|
|
protected const MODE_CREATE = 'create'; |
|
58
|
|
|
protected const MODE_PATCH = 'patch'; |
|
59
|
|
|
protected const MODE_NO_CHANGE = 'nochange'; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Unique counter |
|
63
|
|
|
* |
|
64
|
|
|
* @var integer |
|
65
|
|
|
*/ |
|
66
|
|
|
public static $counter = 0; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var ObjectType |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $type = null; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @var GeneratedCollection |
|
75
|
|
|
*/ |
|
76
|
|
|
protected $collection = null; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Code used in the create() call |
|
80
|
|
|
* |
|
81
|
|
|
* @var string[] |
|
82
|
|
|
*/ |
|
83
|
|
|
public $createCode = []; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Code used post the create() call |
|
87
|
|
|
* |
|
88
|
|
|
* @var string[] |
|
89
|
|
|
*/ |
|
90
|
|
|
public $postCreateCode = []; |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* 'create' or 'patch' |
|
94
|
|
|
* |
|
95
|
|
|
* @var string |
|
96
|
|
|
*/ |
|
97
|
|
|
protected $mode = self::MODE_CREATE; |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Code used in the create() call |
|
101
|
|
|
* |
|
102
|
|
|
* @var string |
|
103
|
|
|
*/ |
|
104
|
|
|
protected $currentModel = ''; |
|
105
|
|
|
|
|
106
|
8 |
|
public function generate(): GeneratedCollection |
|
107
|
|
|
{ |
|
108
|
8 |
|
$this->collection = new GeneratedCollection(); |
|
109
|
8 |
|
$this->currentModel = \GraphQL\Language\Printer::doPrint($this->type->astNode); |
|
110
|
8 |
|
$filename = $this->generateFilename($this->lowerName); |
|
111
|
|
|
|
|
112
|
8 |
|
if ($this->mode !== self::MODE_NO_CHANGE) { |
|
113
|
8 |
|
$item = new GeneratedItem( |
|
114
|
8 |
|
GeneratedItem::TYPE_MIGRATION, |
|
115
|
8 |
|
$this->generateString(), |
|
116
|
|
|
$filename |
|
117
|
|
|
); |
|
118
|
8 |
|
$this->collection->prepend($item); |
|
119
|
|
|
} |
|
120
|
8 |
|
return $this->collection; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
21 |
|
protected function processBasetype( |
|
124
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
|
125
|
|
|
\GraphQL\Language\AST\NodeList $directives |
|
126
|
|
|
): void { |
|
127
|
21 |
|
$fieldName = $field->name; |
|
128
|
|
|
|
|
129
|
21 |
|
if ($field->type instanceof NonNull) { |
|
130
|
21 |
|
$type = $field->type->getWrappedType(); |
|
131
|
|
|
} else { |
|
132
|
1 |
|
$type = $field->type; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
21 |
|
$codeFragment = new MigrationCodeFragment(); |
|
136
|
|
|
|
|
137
|
21 |
|
if ($type instanceof IDType) { |
|
138
|
21 |
|
$codeFragment->appendBase('$table->bigIncrements("id")'); |
|
139
|
16 |
|
} elseif ($type instanceof StringType) { |
|
140
|
16 |
|
$codeFragment->appendBase('$table->string("' . $fieldName . '")'); |
|
141
|
4 |
|
} elseif ($type instanceof IntType) { |
|
142
|
1 |
|
$codeFragment->appendBase('$table->integer("' . $fieldName . '")'); |
|
143
|
4 |
|
} elseif ($type instanceof BooleanType) { |
|
144
|
1 |
|
$codeFragment->appendBase('$table->boolean("' . $fieldName . '")'); |
|
145
|
4 |
|
} elseif ($type instanceof FloatType) { |
|
146
|
1 |
|
$codeFragment->appendBase('$table->float("' . $fieldName . '")'); |
|
147
|
3 |
|
} elseif ($type instanceof EnumType) { |
|
148
|
|
|
$this->processEnum($field, $type, $codeFragment); |
|
149
|
3 |
|
} elseif ($type instanceof UnionType) { |
|
150
|
|
|
return; |
|
151
|
3 |
|
} elseif ($type instanceof CustomScalarType) { |
|
152
|
3 |
|
$ourType = $this->parser->getScalarType($type->name); |
|
153
|
3 |
|
if (!$ourType) { |
|
154
|
|
|
throw new Exception("Invalid extended scalar type: " . get_class($type)); |
|
155
|
|
|
} |
|
156
|
3 |
|
$options = []; // TODO: from directives |
|
157
|
3 |
|
$codeFragment->appendBase('$table->' . $ourType->getLaravelSQLType($fieldName, $options)); |
|
158
|
|
|
} elseif ($type instanceof ListOfType) { |
|
159
|
|
|
throw new Exception("Invalid field type: " . get_class($type)); |
|
160
|
|
|
} else { |
|
161
|
|
|
throw new Exception("Invalid field type: " . get_class($type)); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
21 |
|
if (!($field->type instanceof NonNull)) { |
|
165
|
1 |
|
$codeFragment->appendBase('->nullable()'); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
21 |
|
foreach ($directives as $directive) { |
|
169
|
2 |
|
$name = $directive->name->value; |
|
170
|
2 |
|
if ($name === 'migrationSkip') { // special case |
|
171
|
|
|
return; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
2 |
|
$className = $this->getDirectiveClass($name); |
|
175
|
2 |
|
if ($className) { |
|
176
|
1 |
|
$methodName = "$className::processMigrationFieldDirective"; |
|
177
|
|
|
/** @phpstan-ignore-next-line */ |
|
178
|
1 |
|
$methodName( |
|
179
|
1 |
|
$this, |
|
180
|
|
|
$field, |
|
181
|
|
|
$directive, |
|
182
|
|
|
$codeFragment |
|
183
|
|
|
); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
21 |
|
$this->createCode[] = $codeFragment->base . ';'; |
|
188
|
21 |
|
$this->createCode = array_merge($this->createCode, $codeFragment->extraLines); |
|
189
|
21 |
|
} |
|
190
|
|
|
|
|
191
|
|
|
protected function processEnum( |
|
192
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
|
193
|
|
|
EnumType $type, |
|
194
|
|
|
MigrationCodeFragment $codeFragment |
|
195
|
|
|
): void { |
|
196
|
|
|
$fieldName = $field->name; |
|
197
|
|
|
$ourType = $this->parser->getScalarType($type->name); |
|
198
|
|
|
$parsedValues = $type->config['values']; |
|
199
|
|
|
|
|
200
|
|
|
if (!$ourType) { |
|
201
|
|
|
$parsedKeys = array_keys($parsedValues); |
|
202
|
|
|
$enumValues = array_combine($parsedKeys, $parsedKeys); |
|
203
|
|
|
|
|
204
|
|
|
// let's create this for the user |
|
205
|
|
|
$code = DatatypeFactory::generate( |
|
206
|
|
|
$type->name, |
|
207
|
|
|
'enum', |
|
208
|
|
|
'App\\Datatypes', |
|
209
|
|
|
'Tests\\Unit', |
|
210
|
|
|
function (ClassType $enumClass) use ($enumValues) { |
|
211
|
|
|
$enumClass->addConstant('CHOICES', $enumValues); |
|
212
|
|
|
$enumClass->getMethod('__construct')->addBody('$this->choices = self::CHOICES;'); |
|
213
|
|
|
} |
|
214
|
|
|
); |
|
215
|
|
|
|
|
216
|
|
|
$path = base_path('app/Datatypes'); |
|
|
|
|
|
|
217
|
|
|
$lowerTypeName = mb_strtolower($type->name); |
|
218
|
|
|
|
|
219
|
|
|
$retval = DatatypeFactory::generateFile( |
|
220
|
|
|
$code, |
|
221
|
|
|
$path, |
|
222
|
|
|
base_path('tests/Unit/') |
|
223
|
|
|
); |
|
224
|
|
|
|
|
225
|
|
|
$php = \Modelarium\Util::generateLighthouseTypeFile($lowerTypeName, 'App\\Datatypes\\Types'); |
|
226
|
|
|
$filename = $path . "/Types/Datatype_{$lowerTypeName}.php"; |
|
227
|
|
|
if (!is_dir($path . "/Types")) { |
|
228
|
|
|
\Safe\mkdir($path . "/Types", 0777, true); |
|
229
|
|
|
} |
|
230
|
|
|
\Safe\file_put_contents($filename, $php); |
|
231
|
|
|
|
|
232
|
|
|
// recreate scalars |
|
233
|
|
|
\Modelarium\Util::generateScalarFile('App\\Datatypes', base_path('graphql/types.graphql')); |
|
234
|
|
|
|
|
235
|
|
|
// load php files that were just created |
|
236
|
|
|
require_once($retval['filename']); |
|
237
|
|
|
require_once($filename); |
|
238
|
|
|
$this->parser->appendScalar($type->name, 'App\\Datatypes\\Types\\Datatype_' . $lowerTypeName); |
|
239
|
|
|
$ourType = $this->parser->getScalarType($type->name); |
|
240
|
|
|
} |
|
241
|
|
|
if (!($ourType instanceof FormulariumScalarType)) { |
|
242
|
|
|
throw new Exception("Enum {$type->name} {$fieldName} is not a FormulariumScalarType"); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* @var FormulariumScalarType $ourType |
|
247
|
|
|
*/ |
|
248
|
|
|
/** |
|
249
|
|
|
* @var Datatype_enum $ourDatatype |
|
250
|
|
|
*/ |
|
251
|
|
|
$ourDatatype = $ourType->getDatatype(); |
|
252
|
|
|
$currentChoices = $ourDatatype->getChoices(); |
|
253
|
|
|
if (array_diff_key($currentChoices, $parsedValues) || array_diff_key($parsedValues, $currentChoices)) { |
|
254
|
|
|
// TODO??? |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
$options = []; // TODO: from directives |
|
258
|
|
|
$codeFragment->appendBase('$table->' . $ourType->getLaravelSQLType($fieldName, $options)); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
8 |
|
protected function processRelationship( |
|
262
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
|
263
|
|
|
\GraphQL\Language\AST\NodeList $directives |
|
264
|
|
|
): void { |
|
265
|
8 |
|
$lowerName = mb_strtolower($this->getInflector()->singularize($field->name)); |
|
266
|
8 |
|
$lowerNamePlural = $this->getInflector()->pluralize($lowerName); |
|
267
|
8 |
|
$fieldName = $lowerName . '_id'; |
|
268
|
|
|
|
|
269
|
8 |
|
list($type, $isRequired) = Parser::getUnwrappedType($field->type); |
|
270
|
8 |
|
$typeName = $type->name; |
|
271
|
8 |
|
$tableName = self::toTableName($typeName); |
|
|
|
|
|
|
272
|
|
|
|
|
273
|
|
|
// special types that should be skipped. |
|
274
|
8 |
|
if ($typeName === 'Can') { |
|
275
|
|
|
return; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
8 |
|
$codeFragment = new MigrationCodeFragment(); |
|
279
|
|
|
|
|
280
|
8 |
|
$isManyToMany = false; |
|
281
|
8 |
|
foreach ($directives as $directive) { |
|
282
|
8 |
|
$name = $directive->name->value; |
|
283
|
8 |
|
if ($name === 'migrationSkip') { |
|
284
|
|
|
return; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
$className = $this->getDirectiveClass($name); |
|
288
|
|
|
if ($className) { |
|
289
|
|
|
$methodName = "$className::processMigrationRelationshipDirective"; |
|
290
|
|
|
/** @phpstan-igno re-next-line */ |
|
291
|
|
|
$methodName( |
|
292
|
|
|
$this, |
|
293
|
|
|
$field, |
|
294
|
|
|
$directive, |
|
295
|
|
|
$codeFragment |
|
296
|
|
|
); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
// TODO: handle isManyToMany for migrationForeign |
|
300
|
8 |
|
switch ($name) { |
|
301
|
8 |
|
case 'belongToMany': |
|
302
|
|
|
case 'morphedByMany': |
|
303
|
|
|
$isManyToMany = true; |
|
304
|
8 |
|
break; |
|
305
|
|
|
} |
|
306
|
|
|
} |
|
307
|
8 |
|
|
|
308
|
4 |
|
// TODO: move this to the a separate class |
|
309
|
4 |
|
foreach ($directives as $directive) { |
|
310
|
|
|
$name = $directive->name->value; |
|
311
|
4 |
|
switch ($name) { |
|
312
|
|
|
case 'migrationForeign': |
|
313
|
|
|
if (!$isManyToMany) { |
|
314
|
|
|
$arguments = array_merge( |
|
315
|
|
|
[ |
|
316
|
4 |
|
'references' => 'id', |
|
317
|
4 |
|
'on' => $lowerNamePlural |
|
318
|
|
|
], |
|
319
|
4 |
|
Parser::getDirectiveArguments($directive) |
|
320
|
3 |
|
); |
|
321
|
|
|
|
|
322
|
3 |
|
$codeFragment->appendExtraLine( |
|
323
|
|
|
'$table->foreign("' . $fieldName . '")' . |
|
324
|
|
|
"->references(\"{$arguments['references']}\")" . |
|
325
|
|
|
"->on(\"{$arguments['on']}\")" . |
|
326
|
4 |
|
(($arguments['onDelete'] ?? '') ? "->onDelete(\"{$arguments['onDelete']}\")" : '') . |
|
327
|
4 |
|
(($arguments['onUpdate'] ?? '') ? "->onUpdate(\"{$arguments['onUpdate']}\")" : '') . |
|
328
|
4 |
|
';' |
|
329
|
4 |
|
); |
|
330
|
1 |
|
} |
|
331
|
4 |
|
break; |
|
332
|
4 |
|
} |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
4 |
|
if ($codeFragment->base) { |
|
336
|
|
|
if (!($field->type instanceof NonNull)) { |
|
337
|
8 |
|
$codeFragment->appendBase('->nullable()'); |
|
338
|
1 |
|
} |
|
339
|
1 |
|
$this->createCode[] = '$table' . $codeFragment->base . ';'; |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
1 |
|
$this->createCode = array_merge($this->createCode, $codeFragment->extraLines); |
|
343
|
1 |
|
} |
|
344
|
1 |
|
|
|
345
|
|
|
protected function processDirectives( |
|
346
|
1 |
|
\GraphQL\Language\AST\NodeList $directives |
|
347
|
|
|
): void { |
|
348
|
7 |
|
foreach ($directives as $directive) { |
|
349
|
2 |
|
$name = $directive->name->value; |
|
350
|
2 |
|
$className = $this->getDirectiveClass($name); |
|
351
|
2 |
|
if ($className) { |
|
352
|
2 |
|
$methodName = "$className::processMigrationTypeDirective"; |
|
353
|
2 |
|
/** @phpstan-ignore-next-line */ |
|
354
|
|
|
$methodName( |
|
355
|
2 |
|
$this, |
|
356
|
|
|
$directive |
|
357
|
7 |
|
); |
|
358
|
1 |
|
} |
|
359
|
1 |
|
} |
|
360
|
1 |
|
} |
|
361
|
1 |
|
|
|
362
|
|
|
public function generateString(): string |
|
363
|
|
|
{ |
|
364
|
|
|
foreach ($this->type->getFields() as $field) { |
|
365
|
8 |
|
$directives = $field->astNode->directives; |
|
366
|
8 |
|
if ( |
|
367
|
|
|
($field->type instanceof ObjectType) || |
|
368
|
8 |
|
($field->type instanceof ListOfType) || |
|
369
|
4 |
|
($field->type instanceof UnionType) || |
|
370
|
4 |
|
($field->type instanceof NonNull && ( |
|
371
|
|
|
($field->type->getWrappedType() instanceof ObjectType) || |
|
372
|
4 |
|
($field->type->getWrappedType() instanceof ListOfType) || |
|
373
|
4 |
|
($field->type->getWrappedType() instanceof UnionType) |
|
374
|
|
|
)) |
|
375
|
4 |
|
) { |
|
376
|
|
|
// relationship |
|
377
|
|
|
$this->processRelationship($field, $directives); |
|
378
|
4 |
|
} else { |
|
379
|
4 |
|
$this->processBasetype($field, $directives); |
|
380
|
4 |
|
} |
|
381
|
4 |
|
} |
|
382
|
4 |
|
|
|
383
|
4 |
|
assert($this->type->astNode !== null); |
|
384
|
4 |
|
/** |
|
385
|
|
|
* @var \GraphQL\Language\AST\NodeList|null |
|
386
|
|
|
*/ |
|
387
|
4 |
|
$directives = $this->type->astNode->directives; |
|
388
|
|
|
if ($directives) { |
|
389
|
|
|
$this->processDirectives($directives); |
|
|
|
|
|
|
390
|
|
|
} |
|
391
|
8 |
|
|
|
392
|
6 |
|
$context = [ |
|
393
|
|
|
'dummytablename' => $this->tableName, |
|
394
|
|
|
'modelSchemaCode' => "# start graphql\n" . |
|
395
|
6 |
|
$this->currentModel . |
|
396
|
|
|
"\n# end graphql", |
|
397
|
|
|
]; |
|
398
|
8 |
|
|
|
399
|
8 |
|
if ($this->mode === self::MODE_CREATE) { |
|
400
|
|
|
$context['className'] = 'Create' . $this->studlyName; |
|
401
|
21 |
|
$context['dummyCode'] = join("\n ", $this->createCode); |
|
402
|
|
|
$context['dummyPostCreateCode'] = join("\n ", $this->postCreateCode); |
|
403
|
|
|
} else { |
|
404
|
21 |
|
$context['className'] = 'Patch' . $this->studlyName . date('YmdHis'); |
|
405
|
6 |
|
$context['dummyCode'] = '// TODO: write the patch please'; |
|
406
|
6 |
|
$context['dummyPostCreateCode'] = ''; |
|
407
|
6 |
|
} |
|
408
|
6 |
|
|
|
409
|
|
|
return $this->templateStub('migration', $context); |
|
410
|
6 |
|
} |
|
411
|
6 |
|
|
|
412
|
|
|
/** |
|
413
|
|
|
* creates a many-to-many morph relationship table |
|
414
|
|
|
* |
|
415
|
|
|
* @param string $name |
|
416
|
21 |
|
* @param string $relation |
|
417
|
|
|
* @return string The table name. |
|
418
|
21 |
|
*/ |
|
419
|
|
|
public function generateManyToManyMorphTable(string $name, string $relation): string |
|
420
|
21 |
|
{ |
|
421
|
21 |
|
$dummyCode = <<<EOF |
|
422
|
|
|
|
|
423
|
21 |
|
\$table->unsignedBigInteger("{$name}_id"); |
|
424
|
21 |
|
\$table->unsignedBigInteger("{$relation}_id"); |
|
425
|
21 |
|
\$table->string("{$relation}_type"); |
|
426
|
21 |
|
EOF; |
|
427
|
21 |
|
$context = [ |
|
428
|
21 |
|
'dummyCode' => $dummyCode, |
|
429
|
21 |
|
'dummytablename' => $this->getInflector()->pluralize($relation), // TODO: check, toTableName()? |
|
430
|
|
|
'modelSchemaCode' => '' |
|
431
|
|
|
]; |
|
432
|
|
|
$contents = $this->templateStub('migration', $context); |
|
433
|
8 |
|
|
|
434
|
|
|
$item = new GeneratedItem( |
|
435
|
21 |
|
GeneratedItem::TYPE_MIGRATION, |
|
436
|
|
|
$contents, |
|
437
|
|
|
$this->getBasePath( |
|
438
|
|
|
'database/migrations/' . |
|
439
|
|
|
date('Y_m_d_His') . |
|
440
|
|
|
str_pad((string)(static::$counter++), 3, "0", STR_PAD_LEFT) . // so we keep the same order of types in schema |
|
441
|
|
|
'_' . $this->mode . '_' . |
|
442
|
|
|
$relation . |
|
443
|
21 |
|
'_table.php' |
|
444
|
21 |
|
) |
|
445
|
21 |
|
); |
|
446
|
|
|
$this->collection->push($item); |
|
447
|
|
|
|
|
448
|
|
|
return $context['dummytablename']; |
|
449
|
21 |
|
} |
|
450
|
|
|
|
|
451
|
21 |
|
/** |
|
452
|
21 |
|
* creates a many-to-many relationship table |
|
453
|
|
|
* |
|
454
|
|
|
* @param string $type1 |
|
455
|
21 |
|
* @param string $type2 |
|
456
|
21 |
|
* @return string The table name. |
|
457
|
21 |
|
*/ |
|
458
|
21 |
|
public function generateManyToManyTable(string $type1, string $type2): string |
|
459
|
|
|
{ |
|
460
|
|
|
$dummyCode = <<<EOF |
|
461
|
|
|
|
|
462
|
|
|
\$table->increments("id"); |
|
463
|
|
|
\$table->unsignedBigInteger("{$type1}_id")->references('id')->on('{$type1}'); |
|
464
|
|
|
\$table->unsignedBigInteger("{$type2}_id")->references('id')->on('{$type2}'); |
|
465
|
21 |
|
EOF; |
|
466
|
|
|
$context = [ |
|
467
|
|
|
'dummyCode' => $dummyCode, |
|
468
|
|
|
'dummytablename' => "{$type1}_{$type2}", |
|
469
|
|
|
'className' => Str::studly($this->mode) . Str::studly($type1) . Str::studly($type2), |
|
470
|
|
|
'modelSchemaCode' => '' |
|
471
|
|
|
]; |
|
472
|
|
|
$contents = $this->templateStub('migration', $context); |
|
473
|
|
|
|
|
474
|
|
|
$item = new GeneratedItem( |
|
475
|
1 |
|
GeneratedItem::TYPE_MIGRATION, |
|
476
|
|
|
$contents, |
|
477
|
|
|
$this->getBasePath( |
|
478
|
|
|
'database/migrations/' . |
|
479
|
1 |
|
date('Y_m_d_His') . |
|
480
|
1 |
|
str_pad((string)(static::$counter++), 3, "0", STR_PAD_LEFT) . // so we keep the same order of types in schema |
|
481
|
1 |
|
'_' . $this->mode . '_' . |
|
482
|
|
|
$type1 . '_' . $type2 . |
|
483
|
|
|
'_table.php' |
|
484
|
1 |
|
) |
|
485
|
1 |
|
); |
|
486
|
1 |
|
$this->collection->push($item); |
|
487
|
|
|
|
|
488
|
1 |
|
return $context['dummytablename']; |
|
489
|
|
|
} |
|
490
|
1 |
|
|
|
491
|
1 |
|
protected function generateFilename(string $basename): string |
|
492
|
|
|
{ |
|
493
|
1 |
|
$this->mode = self::MODE_CREATE; |
|
494
|
|
|
$match = '_' . $basename . '_table.php'; |
|
495
|
1 |
|
|
|
496
|
1 |
|
$basepath = $this->getBasePath('database/migrations/'); |
|
497
|
1 |
|
if (is_dir($basepath)) { |
|
498
|
1 |
|
$migrationFiles = \Safe\scandir($basepath); |
|
499
|
1 |
|
rsort($migrationFiles); |
|
500
|
|
|
foreach ($migrationFiles as $m) { |
|
501
|
|
|
if (!endsWith($m, $match)) { |
|
502
|
1 |
|
continue; |
|
503
|
|
|
} |
|
504
|
1 |
|
|
|
505
|
|
|
// get source |
|
506
|
|
|
$data = \Safe\file_get_contents($basepath . '/' . $m); |
|
507
|
|
|
|
|
508
|
|
|
// compare with this source |
|
509
|
|
|
$model = trim(getStringBetween($data, '# start graphql', '# end graphql')); |
|
510
|
|
|
|
|
511
|
|
|
// if equal ignore and don't output file |
|
512
|
|
|
if ($model === $this->currentModel) { |
|
513
|
|
|
$this->mode = self::MODE_NO_CHANGE; |
|
514
|
1 |
|
} else { |
|
515
|
|
|
// else we'll generate a diff and patch |
|
516
|
|
|
$this->mode = self::MODE_PATCH; |
|
517
|
|
|
} |
|
518
|
|
|
break; |
|
519
|
1 |
|
} |
|
520
|
1 |
|
} |
|
521
|
|
|
|
|
522
|
|
|
return $this->getBasePath( |
|
523
|
1 |
|
'database/migrations/' . |
|
524
|
1 |
|
date('Y_m_d_His') . |
|
525
|
1 |
|
str_pad((string)(static::$counter++), 3, "0", STR_PAD_LEFT) . // so we keep the same order of types in schema |
|
526
|
1 |
|
'_' . $this->mode . '_' . |
|
527
|
|
|
$basename . '_table.php' |
|
528
|
1 |
|
); |
|
529
|
|
|
} |
|
530
|
|
|
} |
|
531
|
|
|
|