-
-
Notifications
You must be signed in to change notification settings - Fork 438
/
ArgPartitioner.php
200 lines (173 loc) · 6.16 KB
/
ArgPartitioner.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
namespace Nuwave\Lighthouse\Execution\Arguments;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Nuwave\Lighthouse\Exceptions\DefinitionException;
use Nuwave\Lighthouse\Support\Contracts\ArgResolver;
use Nuwave\Lighthouse\Support\Utils;
class ArgPartitioner
{
/**
* Partition the arguments into nested and regular.
*
* @return array<\Nuwave\Lighthouse\Execution\Arguments\ArgumentSet>
*/
public static function nestedArgResolvers(ArgumentSet $argumentSet, $root): array
{
$model = $root instanceof Model
? new \ReflectionClass($root)
: null;
foreach ($argumentSet->arguments as $name => $argument) {
static::attachNestedArgResolver($name, $argument, $model);
}
return static::partition(
$argumentSet,
static function (string $name, Argument $argument): bool {
return null !== $argument->resolver;
}
);
}
/**
* Extract all the arguments that correspond to a relation of a certain type on the model.
*
* For example, if the args input looks like this:
*
* [
* 'name' => 'Ralf',
* 'comments' =>
* ['foo' => 'Bar'],
* ]
*
* and the model has a method "comments" that returns a HasMany relationship,
* the result will be:
* [
* [
* 'comments' =>
* ['foo' => 'Bar'],
* ],
* [
* 'name' => 'Ralf',
* ]
* ]
*
* @return array{0: \Nuwave\Lighthouse\Execution\Arguments\ArgumentSet, 1: \Nuwave\Lighthouse\Execution\Arguments\ArgumentSet}
*/
public static function relationMethods(
ArgumentSet $argumentSet,
Model $model,
string $relationClass
): array {
$modelReflection = new \ReflectionClass($model);
[$relations, $remaining] = static::partition(
$argumentSet,
static function (string $name) use ($modelReflection, $relationClass): bool {
return static::methodReturnsRelation($modelReflection, $name, $relationClass);
}
);
$nonNullRelations = new ArgumentSet();
$nonNullRelations->arguments = array_filter(
$relations->arguments,
static function (Argument $argument): bool {
return null !== $argument->value;
}
);
return [$nonNullRelations, $remaining];
}
/**
* Attach a nested argument resolver to an argument.
*/
protected static function attachNestedArgResolver(string $name, Argument &$argument, ?\ReflectionClass $model): void
{
$resolverDirective = $argument->directives->first(
Utils::instanceofMatcher(ArgResolver::class)
);
if ($resolverDirective) {
$argument->resolver = $resolverDirective;
return;
}
if (isset($model)) {
$isRelation = static function (string $relationClass) use ($model, $name): bool {
return static::methodReturnsRelation($model, $name, $relationClass);
};
if (
$isRelation(HasOne::class)
|| $isRelation(MorphOne::class)
) {
$argument->resolver = new ResolveNested(new NestedOneToOne($name));
return;
}
if (
$isRelation(HasMany::class)
|| $isRelation(MorphMany::class)
) {
$argument->resolver = new ResolveNested(new NestedOneToMany($name));
return;
}
if (
$isRelation(BelongsToMany::class)
|| $isRelation(MorphToMany::class)
) {
$argument->resolver = new ResolveNested(new NestedManyToMany($name));
}
}
}
/**
* Partition arguments based on a predicate.
*
* The predicate will be called for each argument within the ArgumentSet
* with the following parameters:
* 1. The name of the argument
* 2. The argument itself
*
* Returns an array of two new ArgumentSet instances:
* - the first one contains all arguments for which the predicate matched
* - the second one contains all arguments for which the predicate did not match
*
* @return array{0: \Nuwave\Lighthouse\Execution\Arguments\ArgumentSet, 1: \Nuwave\Lighthouse\Execution\Arguments\ArgumentSet}
*/
public static function partition(ArgumentSet $argumentSet, \Closure $predicate): array
{
$matched = new ArgumentSet();
$notMatched = new ArgumentSet();
foreach ($argumentSet->arguments as $name => $argument) {
if ($predicate($name, $argument)) {
$matched->arguments[$name] = $argument;
} else {
$notMatched->arguments[$name] = $argument;
}
}
return [
$matched,
$notMatched,
];
}
/**
* Does a method on the model return a relation of the given class?
*/
public static function methodReturnsRelation(
\ReflectionClass $modelReflection,
string $name,
string $relationClass
): bool {
if (! $modelReflection->hasMethod($name)) {
return false;
}
$relationMethodCandidate = $modelReflection->getMethod($name);
$returnType = $relationMethodCandidate->getReturnType();
if (! $returnType instanceof \ReflectionNamedType) {
return false;
}
if ($returnType->isBuiltin()) {
return false;
}
if (! class_exists($returnType->getName())) {
throw new DefinitionException('Class ' . $returnType->getName() . ' does not exist, did you forget to import the Eloquent relation class?');
}
return is_a($returnType->getName(), $relationClass, true);
}
}