-
-
Notifications
You must be signed in to change notification settings - Fork 438
/
ArgumentSet.php
275 lines (234 loc) · 9.38 KB
/
ArgumentSet.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
namespace Nuwave\Lighthouse\Execution\Arguments;
use Closure;
use Nuwave\Lighthouse\Schema\Directives\RenameDirective;
use Nuwave\Lighthouse\Schema\Directives\SpreadDirective;
use Nuwave\Lighthouse\Scout\ScoutEnhancer;
use Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective;
use Nuwave\Lighthouse\Support\Contracts\FieldBuilderDirective;
use Nuwave\Lighthouse\Support\Utils;
class ArgumentSet
{
/**
* An associative array from argument names to arguments.
*
* @var array<string, \Nuwave\Lighthouse\Execution\Arguments\Argument>
*/
public $arguments = [];
/**
* An associative array of arguments that were not given.
*
* @var array<string, \Nuwave\Lighthouse\Execution\Arguments\Argument>
*/
public $undefined = [];
/**
* A list of directives.
*
* This may be coming from
* - the field the arguments are a part of
* - the parent argument when in a tree of nested inputs.
*
* @var \Illuminate\Support\Collection<\Nuwave\Lighthouse\Support\Contracts\Directive>
*/
public $directives;
/**
* Get a plain array representation of this ArgumentSet.
*
* @return array<string, mixed>
*/
public function toArray(): array
{
$plainArguments = [];
foreach ($this->arguments as $name => $argument) {
$plainArguments[$name] = $argument->toPlain();
}
return $plainArguments;
}
/**
* Check if the ArgumentSet has a non-null value with the given key.
*/
public function has(string $key): bool
{
$argument = $this->arguments[$key] ?? null;
if ($argument === null) {
return false;
}
return $argument->value !== null;
}
/**
* Apply the @spread directive and return a new, modified instance.
*
* @noRector \Rector\DeadCode\Rector\ClassMethod\RemoveDeadRecursiveClassMethodRector
*/
public function spread(): self
{
$argumentSet = new self();
$argumentSet->directives = $this->directives;
foreach ($this->arguments as $name => $argument) {
$value = $argument->value;
// In this case, we do not care about argument sets nested within
// lists, spreading only makes sense for single nested inputs.
if ($value instanceof self) {
// Recurse down first, as that resolves the more deeply nested spreads first
$value = $value->spread();
if ($argument->directives->contains(
Utils::instanceofMatcher(SpreadDirective::class)
)) {
$argumentSet->arguments += $value->arguments;
continue;
}
}
$argumentSet->arguments[$name] = $argument;
}
return $argumentSet;
}
/**
* Apply the @rename directive and return a new, modified instance.
*
* @noRector \Rector\DeadCode\Rector\ClassMethod\RemoveDeadRecursiveClassMethodRector
*/
public function rename(): self
{
$argumentSet = new self();
$argumentSet->directives = $this->directives;
foreach ($this->arguments as $name => $argument) {
// Recursively apply the renaming to nested inputs.
// We look for further ArgumentSet instances, they
// might be contained within an array.
$argument->value = Utils::applyEach(
function ($value) {
if ($value instanceof self) {
return $value->rename();
}
return $value;
},
$argument->value
);
/** @var \Nuwave\Lighthouse\Schema\Directives\RenameDirective|null $renameDirective */
$renameDirective = $argument->directives->first(function ($directive) {
return $directive instanceof RenameDirective;
});
if ($renameDirective !== null) {
$argumentSet->arguments[$renameDirective->attributeArgValue()] = $argument;
} else {
$argumentSet->arguments[$name] = $argument;
}
}
return $argumentSet;
}
/**
* Apply ArgBuilderDirectives and scopes to the builder.
*
* @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $builder
* @param array<string> $scopes
* @param \Closure $directiveFilter
*
* @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|\Laravel\Scout\Builder
*/
public function enhanceBuilder(object $builder, array $scopes, Closure $directiveFilter = null): object
{
$scoutEnhancer = new ScoutEnhancer($this, $builder);
if ($scoutEnhancer->hasSearchArguments()) {
return $scoutEnhancer->enhanceBuilder();
}
self::applyArgBuilderDirectives($this, $builder, $directiveFilter);
self::applyFieldBuilderDirectives($this, $builder);
foreach ($scopes as $scope) {
$builder->{$scope}($this->toArray());
}
return $builder;
}
/**
* Recursively apply the ArgBuilderDirectives onto the builder.
*
* TODO get rid of the reference passing in here. The issue is that @search makes a new builder instance,
* but we must special case that in some way anyhow, as only eq filters can be added on top of search.
*
* @param \Nuwave\Lighthouse\Execution\Arguments\ArgumentSet $argumentSet
* @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $builder
* @param (\Closure(\Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective): bool)|null $directiveFilter
*/
protected static function applyArgBuilderDirectives(self $argumentSet, object &$builder, Closure $directiveFilter = null): void
{
foreach ($argumentSet->arguments as $argument) {
$value = $argument->toPlain();
// TODO switch to instanceof when we require bensampo/laravel-enum
// Unbox Enum values to ensure their underlying value is used for queries
if (is_a($value, '\BenSampo\Enum\Enum')) {
$value = $value->value;
}
$filteredDirectives = $argument
->directives
->filter(Utils::instanceofMatcher(ArgBuilderDirective::class));
if (null !== $directiveFilter) {
$filteredDirectives = $filteredDirectives->filter($directiveFilter);
}
$filteredDirectives->each(static function (ArgBuilderDirective $argBuilderDirective) use (&$builder, $value): void {
$builder = $argBuilderDirective->handleBuilder($builder, $value);
});
Utils::applyEach(
static function ($value) use (&$builder, $directiveFilter) {
if ($value instanceof self) {
self::applyArgBuilderDirectives($value, $builder, $directiveFilter);
}
},
$argument->value
);
}
}
/**
* Apply the FieldBuilderDirectives onto the builder.
*
* TODO get rid of the reference passing in here. The issue is that @search makes a new builder instance,
* but we must special case that in some way anyhow, as only eq filters can be added on top of search.
*
* @param \Nuwave\Lighthouse\Execution\Arguments\ArgumentSet $argumentSet
* @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $builder
*/
protected static function applyFieldBuilderDirectives(self $argumentSet, object &$builder): void
{
$argumentSet->directives
->filter(Utils::instanceofMatcher(FieldBuilderDirective::class))
->each(static function (FieldBuilderDirective $fieldBuilderDirective) use (&$builder): void {
$builder = $fieldBuilderDirective->handleFieldBuilder($builder);
});
}
/**
* Add a value at the dot-separated path.
*
* Works just like @see \Illuminate\Support\Arr::add().
*
* @param mixed $value Any value to inject.
* @return $this
*/
public function addValue(string $path, $value): self
{
$argumentSet = $this;
$keys = explode('.', $path);
while (count($keys) > 1) {
$key = array_shift($keys);
// If the key doesn't exist at this depth, we will just create an empty ArgumentSet
// to hold the next value, allowing us to create the ArgumentSet to hold a final
// value at the correct depth. Then we'll keep digging into the ArgumentSet.
if (! isset($argumentSet->arguments[$key])) {
$argument = new Argument();
$argument->value = new self();
$argumentSet->arguments[$key] = $argument;
}
$argumentSet = $argumentSet->arguments[$key]->value;
}
$argument = new Argument();
$argument->value = $value;
$argumentSet->arguments[array_shift($keys)] = $argument;
return $this;
}
/**
* The contained arguments, including all that were not passed.
*
* @return array<string, \Nuwave\Lighthouse\Execution\Arguments\Argument>
*/
public function argumentsWithUndefined(): array
{
return array_merge($this->arguments, $this->undefined);
}
}