|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Modelarium\Laravel\Directives; |
|
4
|
|
|
|
|
5
|
|
|
use Modelarium\Datatype\Datatype_relationship; |
|
6
|
|
|
use Modelarium\Datatype\RelationshipFactory; |
|
7
|
|
|
use Modelarium\Exception\DirectiveException; |
|
8
|
|
|
use Modelarium\Laravel\Targets\ModelGenerator; |
|
9
|
|
|
use Modelarium\Laravel\Targets\Interfaces\ModelDirectiveInterface; |
|
10
|
|
|
use Modelarium\Parser; |
|
11
|
|
|
|
|
12
|
|
|
class EagerLoadDirective implements ModelDirectiveInterface |
|
13
|
|
|
{ |
|
14
|
|
|
public static function processModelTypeDirective( |
|
15
|
|
|
ModelGenerator $generator, |
|
16
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
|
17
|
|
|
): void { |
|
18
|
|
|
$target = Parser::getDirectiveArgumentByName($directive, 'tables', []); |
|
19
|
|
|
|
|
20
|
|
|
foreach ($target as $t) { |
|
21
|
|
|
$generator->with[] = $t; |
|
22
|
|
|
} |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public static function processModelFieldDirective( |
|
26
|
|
|
ModelGenerator $generator, |
|
27
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
|
28
|
|
|
\Formularium\Field $fieldFormularium, |
|
29
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
|
30
|
|
|
): void { |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Undocumented function |
|
35
|
|
|
* |
|
36
|
|
|
* @param ModelGenerator $generator |
|
37
|
|
|
* @param \GraphQL\Type\Definition\FieldDefinition $field |
|
38
|
|
|
* @param \GraphQL\Language\AST\DirectiveNode $directive |
|
39
|
|
|
* @return?\Formularium\Datatype The relationship datatype name. If this directive does not |
|
40
|
|
|
* handle the datatype, just return an empty string. |
|
41
|
|
|
* |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function processModelRelationshipDirective( |
|
44
|
|
|
ModelGenerator $generator, |
|
45
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
|
46
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive, |
|
47
|
|
|
\Formularium\Datatype $datatype = null |
|
48
|
|
|
): ?\Formularium\Datatype { |
|
49
|
|
|
$target = Parser::getDirectiveArgumentByName($directive, 'name'); |
|
50
|
|
|
|
|
51
|
|
|
if (!$target) { |
|
52
|
|
|
if (!$datatype) { |
|
53
|
|
|
throw new DirectiveException("@eagerLoad must be placed after the relationship directive (e.g. @belongsTo, @hasMany etc)"); |
|
54
|
|
|
} |
|
55
|
|
|
if (!($datatype instanceof Datatype_relationship)) { |
|
56
|
|
|
throw new DirectiveException("@eagerLoad got a datatype that is not a relationship"); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$targetSingle = lcfirst($generator->getInflector()->singularize($datatype->getTarget())); |
|
60
|
|
|
$targetPlural = $datatype->getTargetTable(); |
|
61
|
|
|
switch ($datatype->getRelationship()) { |
|
62
|
|
|
case RelationshipFactory::RELATIONSHIP_ONE_TO_ONE: |
|
63
|
|
|
case RelationshipFactory::MORPH_ONE_TO_ONE: |
|
64
|
|
|
$target = $targetSingle; |
|
65
|
|
|
break; |
|
66
|
|
|
case RelationshipFactory::RELATIONSHIP_ONE_TO_MANY: |
|
67
|
|
|
case RelationshipFactory::MORPH_ONE_TO_MANY: |
|
68
|
|
|
$target = $datatype->getIsInverse() ? $targetSingle : $targetPlural; |
|
69
|
|
|
break; |
|
70
|
|
|
case RelationshipFactory::RELATIONSHIP_MANY_TO_MANY: |
|
71
|
|
|
case RelationshipFactory::MORPH_MANY_TO_MANY: |
|
72
|
|
|
$target = $targetPlural; |
|
73
|
|
|
break; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
$generator->with[] = $target; |
|
77
|
|
|
|
|
78
|
|
|
return null; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|