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