-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathDirectiveLocator.php
More file actions
321 lines (272 loc) · 10 KB
/
DirectiveLocator.php
File metadata and controls
321 lines (272 loc) · 10 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
namespace Nuwave\Lighthouse\Schema;
use Composer\ClassMapGenerator\ClassMapGenerator;
use GraphQL\Language\AST\DirectiveNode;
use GraphQL\Language\AST\Node;
use Illuminate\Container\Container;
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcher;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Nuwave\Lighthouse\Events\RegisterDirectiveNamespaces;
use Nuwave\Lighthouse\Exceptions\DirectiveException;
use Nuwave\Lighthouse\Schema\AST\ASTHelper;
use Nuwave\Lighthouse\Schema\Directives\BaseDirective;
use Nuwave\Lighthouse\Support\Contracts\Directive;
use Nuwave\Lighthouse\Support\Utils;
class DirectiveLocator
{
/**
* The paths used for locating directive classes.
*
* Should be tried in the order they are contained in this array,
* going from the most significant to least significant.
*
* Lazily initialized.
*
* @var array<int, string>
*/
protected $directiveNamespaces;
/**
* A map from short directive names to full class names.
*
* E.g.
* [
* 'create' => 'Nuwave\Lighthouse\Schema\Directives\CreateDirective',
* 'custom' => 'App\GraphQL\Directives\CustomDirective',
* ]
*
* @var array<string, class-string<\Nuwave\Lighthouse\Support\Contracts\Directive>>
*/
protected $resolvedClassnames = [];
/**
* @var \Illuminate\Contracts\Events\Dispatcher
*/
protected $eventsDispatcher;
public function __construct(EventsDispatcher $eventsDispatcher)
{
$this->eventsDispatcher = $eventsDispatcher;
}
/**
* A list of namespaces with directives in descending priority.
*
* @return array<int, string>
*/
public function namespaces(): array
{
if (null === $this->directiveNamespaces) {
$this->directiveNamespaces
// When looking for a directive by name, the namespaces are tried in order
= (new Collection([
// User defined directives (top priority)
config('lighthouse.namespaces.directives'),
// Plugin developers defined directives
$this->eventsDispatcher->dispatch(new RegisterDirectiveNamespaces()),
// Lighthouse defined directives
'Nuwave\\Lighthouse\\Schema\\Directives',
]))
->flatten()
->filter()
->all();
}
return $this->directiveNamespaces;
}
/**
* Scan the namespaces for directive classes.
*
* @return array<string, class-string<\Nuwave\Lighthouse\Support\Contracts\Directive>>
*/
public function classes(): array
{
$directives = [];
$basePath = base_path();
dump($basePath);
foreach ($this->namespaces() as $directiveNamespace) {
dump($directiveNamespace);
$generator = new ClassMapGenerator();
dump('generator');
// TODO any of those hang indefinitely
// $generator->scanPaths($basePath, null, 'psr-4', '');
$generator->scanPaths($basePath);
dump('scanPaths');
$classesInNamespace = $generator->getClassMap()->getMap();
dump($classesInNamespace);
foreach ($classesInNamespace as $class => $_) {
$reflection = new \ReflectionClass($class);
if (! $reflection->isInstantiable()) {
continue;
}
if (! is_a($class, Directive::class, true)) {
continue;
}
$name = self::directiveName($class);
// The directive was already found, so we do not add it twice
if (isset($directives[$name])) {
continue;
}
$directives[$name] = $class;
}
}
return $directives;
}
/**
* Return the parsed definitions for all directive classes.
*
* @return list<\GraphQL\Language\AST\DirectiveDefinitionNode>
*/
public function definitions(): array
{
$definitions = [];
foreach ($this->classes() as $directiveClass) {
$definitions[] = ASTHelper::extractDirectiveDefinition($directiveClass::definition());
}
return $definitions;
}
/**
* Create a directive by the given directive name.
*/
public function create(string $directiveName): Directive
{
$directiveClass = $this->resolve($directiveName);
return Container::getInstance()->make($directiveClass);
}
/**
* Resolve the class for a given directive name.
*
* @throws \Nuwave\Lighthouse\Exceptions\DirectiveException
*
* @return class-string<\Nuwave\Lighthouse\Support\Contracts\Directive>
*/
public function resolve(string $directiveName): string
{
// Bail to respect the priority of namespaces, the first resolved directive is kept
if (array_key_exists($directiveName, $this->resolvedClassnames)) {
return $this->resolvedClassnames[$directiveName];
}
foreach ($this->namespaces() as $baseNamespace) {
$directiveClass = $baseNamespace . '\\' . static::className($directiveName);
if (class_exists($directiveClass)) {
if (! is_a($directiveClass, Directive::class, true)) {
throw new DirectiveException("Class {$directiveClass} must implement the interface " . Directive::class);
}
$this->resolvedClassnames[$directiveName] = $directiveClass;
return $directiveClass;
}
}
throw new DirectiveException("No directive found for `{$directiveName}`");
}
/**
* Returns the expected class name for a directive name.
*/
protected static function className(string $directiveName): string
{
return Str::studly($directiveName) . 'Directive';
}
/**
* Returns the expected directive name for a class name.
*/
public static function directiveName(string $className): string
{
$baseName = basename(str_replace('\\', '/', $className));
return lcfirst(
self::beforeLast($baseName, 'Directive')
);
}
/**
* TODO use Str::beforeLast with Laravel 6+.
*/
protected static function beforeLast(string $subject, string $search): string
{
if ('' === $search) {
return $subject;
}
$pos = mb_strrpos($subject, $search);
if (false === $pos) {
return $subject;
}
return Str::substr($subject, 0, $pos);
}
/**
* @param class-string<\Nuwave\Lighthouse\Support\Contracts\Directive> $directiveClass
*/
public function setResolved(string $directiveName, string $directiveClass): self
{
$this->resolvedClassnames[$directiveName] = $directiveClass;
return $this;
}
/**
* Get all directives that are associated with an AST node.
*
* @return \Illuminate\Support\Collection<\Nuwave\Lighthouse\Support\Contracts\Directive>
*/
public function associated(Node $node): Collection
{
if (! property_exists($node, 'directives')) {
throw new \Exception('Expected Node class with property `directives`, got: ' . get_class($node));
}
return (new Collection($node->directives))
->map(function (DirectiveNode $directiveNode) use ($node): Directive {
$directive = $this->create($directiveNode->name->value);
if ($directive instanceof BaseDirective) {
// @phpstan-ignore-next-line If there were directives on the given Node, it must be of an allowed type
$directive->hydrate($directiveNode, $node);
}
return $directive;
});
}
/**
* Get all directives of a certain type that are associated with an AST node.
*
* @template TDirective of \Nuwave\Lighthouse\Support\Contracts\Directive
*
* @param class-string<TDirective> $directiveClass
*
* @return \Illuminate\Support\Collection<TDirective>
*/
public function associatedOfType(Node $node, string $directiveClass): Collection
{
/**
* Ensured by instanceofMatcher.
*
* @var \Illuminate\Support\Collection<TDirective> $associatedOfType
*/
$associatedOfType = $this
->associated($node)
->filter(Utils::instanceofMatcher($directiveClass));
return $associatedOfType;
}
/**
* Get a single directive of a type that belongs to an AST node.
*
* Use this for directives types that can only occur once, such as field resolvers.
* This throws if more than one such directive is found.
*
* @template TDirective of \Nuwave\Lighthouse\Support\Contracts\Directive
*
* @param class-string<TDirective> $directiveClass
*
* @throws \Nuwave\Lighthouse\Exceptions\DirectiveException
*
* @return TDirective|null
*/
public function exclusiveOfType(Node $node, string $directiveClass): ?Directive
{
$directives = $this->associatedOfType($node, $directiveClass);
if ($directives->count() > 1) {
$directiveNames = $directives
->map(function (Directive $directive): string {
$definition = ASTHelper::extractDirectiveDefinition(
$directive::definition()
);
return '@' . $definition->name->value;
})
->implode(', ');
if (! property_exists($node, 'name')) {
throw new \Exception('Expected Node class with property `name`, got: ' . get_class($node));
}
throw new DirectiveException(
"Node {$node->name->value} can only have one directive of type {$directiveClass} but found [{$directiveNames}]."
);
}
return $directives->first();
}
}