1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Modelarium\Laravel\Directives; |
4
|
|
|
|
5
|
|
|
use Modelarium\Exception\DirectiveException; |
6
|
|
|
use Modelarium\Exception\Exception; |
7
|
|
|
use Modelarium\Laravel\Targets\MigrationGenerator; |
8
|
|
|
use Modelarium\Laravel\Targets\ModelGenerator; |
9
|
|
|
use Modelarium\Laravel\Targets\Interfaces\MigrationDirectiveInterface; |
10
|
|
|
use Modelarium\Laravel\Targets\Interfaces\ModelDirectiveInterface; |
11
|
|
|
use Modelarium\Laravel\Targets\MigrationCodeFragment; |
12
|
|
|
use Modelarium\Parser; |
13
|
|
|
|
14
|
|
|
class MigrationFulltextIndexDirective implements MigrationDirectiveInterface, ModelDirectiveInterface |
15
|
|
|
{ |
16
|
1 |
|
public static function processMigrationTypeDirective( |
17
|
|
|
MigrationGenerator $generator, |
18
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
19
|
|
|
): void { |
20
|
1 |
|
$indexFields = Parser::getDirectiveArgumentByName($directive, 'fields'); |
21
|
|
|
|
22
|
1 |
|
if (!count($indexFields)) { |
|
|
|
|
23
|
|
|
throw new Exception("You must provide at least one field to a full text index"); |
24
|
|
|
} |
25
|
1 |
|
$generator->postCreateCode[] = |
26
|
|
|
"if (!App::environment('testing')) { |
27
|
1 |
|
DB::statement('ALTER TABLE " . $generator->getTableName() . |
28
|
1 |
|
" ADD FULLTEXT fulltext_index (`" . implode('`, `', $indexFields) . "`)'); |
|
|
|
|
29
|
|
|
}"; |
30
|
1 |
|
} |
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
|
|
|
public static function processMigrationRelationshipDirective( |
42
|
|
|
MigrationGenerator $generator, |
43
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
44
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive, |
45
|
|
|
MigrationCodeFragment $code |
46
|
|
|
): void { |
47
|
|
|
throw new DirectiveException("Directive not supported here"); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public static function processModelTypeDirective( |
51
|
|
|
ModelGenerator $generator, |
52
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
53
|
|
|
): void { |
54
|
|
|
$indexFields = Parser::getDirectiveArgumentByName($directive, 'fields'); |
55
|
|
|
$indexFieldsString = join(', ', $indexFields); |
56
|
|
|
|
57
|
|
|
$method = $generator->class->addMethod('scopeFulltext') |
58
|
|
|
->setPublic() |
59
|
|
|
->setComment(' |
60
|
|
|
Scope a query to use the fulltext index |
61
|
|
|
|
62
|
|
|
@param \Illuminate\Database\Eloquent\Builder $query |
63
|
|
|
@param string $needle |
64
|
|
|
@return \Illuminate\Database\Eloquent\Builder |
65
|
|
|
') |
66
|
|
|
->setReturnType('\\Illuminate\\Database\\Eloquent\\Builder') |
67
|
|
|
->setBody("\$parts = array_filter(explode(' ', str_replace(',', '', \$needle)));\n" . |
68
|
|
|
"\$search = implode(',', \$parts);\n" . |
69
|
|
|
"return \$query->whereRaw(\"MATCH ($indexFieldsString) AGAINST (? IN NATURAL LANGUAGE MODE)\", [\$search]);" |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
$method->addParameter('query') |
73
|
|
|
->setType('\\Illuminate\\Database\\Eloquent\\Builder'); |
74
|
|
|
$method->addParameter('needle') |
75
|
|
|
->setType('string'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public static function processModelFieldDirective( |
79
|
|
|
ModelGenerator $generator, |
80
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
81
|
|
|
\Formularium\Field $fieldFormularium, |
82
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
83
|
|
|
): void { |
84
|
|
|
// nothing |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public static function processModelRelationshipDirective( |
88
|
|
|
ModelGenerator $generator, |
89
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
90
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive, |
91
|
|
|
\Formularium\Datatype $datatype = null |
92
|
|
|
): ?\Formularium\Datatype { |
93
|
|
|
return null; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|