-
Notifications
You must be signed in to change notification settings - Fork 1
/
Operators.php
295 lines (248 loc) · 8.36 KB
/
Operators.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php declare(strict_types = 1);
namespace LastDragon_ru\LaraASP\GraphQL\Builder;
use LastDragon_ru\LaraASP\Core\Application\ContainerResolver;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Context;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Ignored;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Operator;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Scope;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeSource;
use LastDragon_ru\LaraASP\GraphQL\Builder\Directives\ExtendOperatorsDirective;
use LastDragon_ru\LaraASP\GraphQL\Utils\AstManipulator;
use WeakMap;
use function array_filter;
use function array_map;
use function array_merge;
use function array_values;
use function is_a;
use function is_object;
use function is_string;
abstract class Operators {
/**
* Determines default operators available for each type.
*
* @var array<string, list<class-string<Operator>|string>>
*/
protected array $default = [];
/**
* Determines actual operators available for each type.
*
* @var array<string, list<class-string<Operator>|string>>
*/
private array $operators = [];
/**
* @var WeakMap<AstManipulator, array<string, list<class-string<Operator>|Operator>>>
*/
private WeakMap $types;
/**
* @var WeakMap<AstManipulator, array<class-string<Operator>, bool>>
*/
private WeakMap $enabled;
/**
* @param array<string, list<class-string<Operator>|string>> $operators
*/
public function __construct(
protected readonly ContainerResolver $container,
array $operators = [],
) {
$this->types = new WeakMap();
$this->enabled = new WeakMap();
foreach ($operators as $type => $value) {
$this->operators[$type] = $value;
}
}
/**
* @return class-string<Scope>
*/
abstract protected function getScope(): string;
/**
* @template T of Operator
*
* @param T|class-string<T> $operator
*
* @return T|null
*/
public function getOperator(
Manipulator $manipulator,
Operator|string $operator,
TypeSource $source,
Context $context,
): ?Operator {
if (!is_a($operator, $this->getScope(), true)) {
return null;
}
if (is_string($operator)) {
$operator = $this->container->getInstance()->make($operator);
}
if (!$this->isEnabled($manipulator, $operator)) {
return null;
}
if (!$operator->isAvailable($manipulator, $source, $context)) {
return null;
}
return $operator;
}
/**
* @return list<Operator>
*/
public function getOperators(Manipulator $manipulator, string $type, TypeSource $source, Context $context): array {
return array_values(
array_filter(
array_map(
function (mixed $operator) use ($manipulator, $source, $context): ?Operator {
return $this->getOperator($manipulator, $operator, $source, $context);
},
$this->getTypeOperators($manipulator, $type),
),
),
);
}
/**
* @return list<class-string<Operator>|Operator>
*/
protected function getTypeOperators(AstManipulator $manipulator, string $type): array {
// Cached?
if (isset($this->types[$manipulator][$type])) {
return $this->types[$manipulator][$type];
}
// Prepare
$this->types[$manipulator] ??= [];
$this->types[$manipulator][$type] ??= [];
// Operators
$unique = [];
$operators = $this->findOperators($manipulator, $type);
foreach ($operators as $operator) {
$class = is_object($operator) ? $operator::class : $operator;
$unique[$class] ??= $operator;
}
$unique = array_values($unique);
// Cache
$this->types[$manipulator][$type] = $unique;
// Return
return $unique;
}
/**
* @param array<string, true> $processed
*
* @return array<array-key, class-string<Operator>|Operator>
*/
private function findOperators(
AstManipulator $manipulator,
string $type,
int $level = 0,
array &$processed = [],
): array {
// We have several levels where operators can be defined - AST, config,
// and built-in defaults. We are always starting at the highest level
// and go deeper if there are no operators or if the type with the same
// name is found.
// Processed?
if (isset($processed[$type])) {
return [];
}
// Search for operators
$list = match ($level) {
0 => $this->findAstOperators($manipulator, $type),
1 => $this->findConfigOperators($type),
2 => $this->findDefaultOperators($type),
default => null,
};
if ($list === null) {
return [];
}
// Merge
$operators = [];
foreach ($list as $operator) {
if (is_a($operator, Operator::class, true)) {
$operators[] = $operator;
} elseif ($type !== $operator) {
$processed[$type] = true;
$operators = array_merge(
$operators,
$this->findOperators($manipulator, $operator, 0, $processed),
);
} else {
$operators = array_merge(
$operators,
$this->findOperators($manipulator, $operator, $level + 1, $processed),
);
}
}
// Empty?
if ($operators === []) {
$operators = $this->findOperators($manipulator, $type, $level + 1, $processed);
}
// Mark
$processed[$type] = true;
// Return
return $operators;
}
/**
* @return array<array-key, class-string<Operator>|Operator|string>|null
*/
private function findAstOperators(AstManipulator $manipulator, string $type): ?array {
if (!$manipulator->isTypeDefinitionExists($type)) {
return [];
}
$scope = $this->getScope();
$operators = [];
$directives = $manipulator->getDirectives($manipulator->getTypeDefinition($type));
foreach ($directives as $directive) {
if (!($directive instanceof $scope)) {
continue;
}
if ($directive instanceof ExtendOperatorsDirective) {
$operators[] = $directive->getType() ?? $type;
} elseif ($directive instanceof Operator) {
$operators[] = $directive;
} elseif ($directive instanceof Ignored) {
$operators = null;
break;
} else {
// empty
}
}
return $operators;
}
/**
* @return array<array-key, class-string<Operator>|string>
*/
private function findConfigOperators(string $type): array {
return $this->operators[$type] ?? [];
}
/**
* @return array<array-key, class-string<Operator>|string>
*/
private function findDefaultOperators(string $type): array {
return $this->default[$type] ?? [];
}
/**
* @param Operator|class-string<Operator> $operator
*/
private function isEnabled(AstManipulator $manipulator, Operator|string $operator): bool {
// Cached?
$class = is_object($operator) ? $operator::class : $operator;
if (isset($this->enabled[$manipulator][$class])) {
return $this->enabled[$manipulator][$class];
}
// Prepare
$this->enabled[$manipulator] ??= [];
// Check
$enabled = true;
foreach ($this->getDisabledOperators($manipulator) as $disabled) {
if (is_a($operator, is_object($disabled) ? $disabled::class : $disabled, true)) {
$enabled = false;
break;
}
}
// Cache
$this->enabled[$manipulator][$class] = $enabled;
// Return
return $enabled;
}
/**
* @return list<class-string<Operator>|Operator>
*/
protected function getDisabledOperators(AstManipulator $manipulator): array {
return [];
}
}