1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Modelarium\Laravel\Directives; |
4
|
|
|
|
5
|
|
|
use Modelarium\Exception\Exception; |
6
|
|
|
use Modelarium\Laravel\Targets\MigrationGenerator; |
7
|
|
|
use Modelarium\Laravel\Targets\Interfaces\MigrationDirectiveInterface; |
8
|
|
|
use Modelarium\Laravel\Targets\MigrationCodeFragment; |
9
|
|
|
|
10
|
|
|
class MigrationIndexDirective implements MigrationDirectiveInterface |
11
|
|
|
{ |
12
|
1 |
|
public static function processMigrationTypeDirective( |
13
|
|
|
MigrationGenerator $generator, |
14
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
15
|
|
|
): void { |
16
|
|
|
/** @phpstan-ignore-next-line */ |
17
|
1 |
|
$values = $directive->arguments[0]->value->values; |
|
|
|
|
18
|
|
|
|
19
|
1 |
|
$indexFields = []; |
20
|
1 |
|
foreach ($values as $value) { |
21
|
1 |
|
$indexFields[] = $value->value; |
22
|
|
|
} |
23
|
1 |
|
if (!count($indexFields)) { |
24
|
|
|
throw new Exception("You must provide at least one field to an index"); |
25
|
|
|
} |
26
|
1 |
|
$generator->createCode[] ='$table->index("' . implode('", "', $indexFields) .'");'; |
27
|
1 |
|
} |
28
|
|
|
|
29
|
|
|
public static function processMigrationFieldDirective( |
30
|
|
|
MigrationGenerator $generator, |
31
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
32
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive, |
33
|
|
|
MigrationCodeFragment $code |
34
|
|
|
): void { |
35
|
|
|
$code->appendExtraLine('$table->index("' . $field->name . '");'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function processMigrationRelationshipDirective( |
39
|
|
|
MigrationGenerator $generator, |
40
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
41
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive, |
42
|
|
|
MigrationCodeFragment $code |
43
|
|
|
): void { |
44
|
|
|
$code->appendExtraLine('$table->index("' . $field->name . '");'); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|