|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Railt package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace Railt\SDL\Reflection\Builder\Process; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\Parser\Ast\NodeInterface; |
|
13
|
|
|
use Railt\Parser\Ast\RuleInterface; |
|
14
|
|
|
use Railt\SDL\Contracts\Definitions\Definition; |
|
15
|
|
|
use Railt\SDL\Contracts\Definitions\TypeDefinition; |
|
16
|
|
|
use Railt\SDL\Contracts\Dependent\DependentDefinition; |
|
17
|
|
|
use Railt\SDL\Contracts\Document; |
|
18
|
|
|
use Railt\SDL\Contracts\Invocations\Invocable; |
|
19
|
|
|
use Railt\SDL\Reflection\Builder\DocumentBuilder; |
|
20
|
|
|
use Railt\SDL\Reflection\Validation\Base\ValidatorInterface; |
|
21
|
|
|
use Railt\SDL\Reflection\Validation\Definitions; |
|
22
|
|
|
use Railt\SDL\Reflection\Validation\Uniqueness; |
|
23
|
|
|
use Railt\SDL\Schema\CompilerInterface; |
|
24
|
|
|
use Railt\SDL\Schema\Configuration; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Trait Compiler |
|
28
|
|
|
*/ |
|
29
|
|
|
trait Compiler |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var int |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $offset = 0; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var NodeInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
private $ast; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array|string[] |
|
43
|
|
|
*/ |
|
44
|
|
|
private $siblingActions = []; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var bool |
|
48
|
|
|
*/ |
|
49
|
|
|
private $completed = false; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return void |
|
53
|
|
|
*/ |
|
54
|
6576 |
|
public function compile(): void |
|
55
|
|
|
{ |
|
56
|
6576 |
|
if ($this->completed === false) { |
|
57
|
6576 |
|
$this->completed = true; |
|
58
|
|
|
|
|
59
|
6576 |
|
foreach ($this->getAst()->getChildren() as $child) { |
|
|
|
|
|
|
60
|
6576 |
|
if ($this->compileSiblings($child)) { |
|
61
|
6479 |
|
continue; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
6576 |
|
if ($this->onCompile($child)) { |
|
65
|
6576 |
|
continue; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
6576 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return NodeInterface|RuleInterface |
|
73
|
|
|
*/ |
|
74
|
6576 |
|
public function getAst(): NodeInterface |
|
75
|
|
|
{ |
|
76
|
6576 |
|
return $this->ast; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param NodeInterface $child |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
6576 |
|
protected function compileSiblings(NodeInterface $child): bool |
|
84
|
|
|
{ |
|
85
|
6576 |
|
foreach ($this->siblingActions as $action) { |
|
86
|
6576 |
|
if ($this->$action($child)) { |
|
87
|
6576 |
|
return true; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
6576 |
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return Document|DocumentBuilder |
|
96
|
|
|
*/ |
|
97
|
6582 |
|
public function getDocument(): Document |
|
98
|
|
|
{ |
|
99
|
6582 |
|
return $this->document; |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return CompilerInterface|Configuration |
|
104
|
|
|
*/ |
|
105
|
6569 |
|
public function getCompiler(): CompilerInterface |
|
106
|
|
|
{ |
|
107
|
6569 |
|
return $this->getDocument()->getCompiler(); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param string $group |
|
112
|
|
|
* @return ValidatorInterface |
|
113
|
|
|
* @throws \OutOfBoundsException |
|
114
|
|
|
*/ |
|
115
|
6576 |
|
public function getValidator(string $group = null): ValidatorInterface |
|
116
|
|
|
{ |
|
117
|
6576 |
|
return $this->getCompiler()->getValidator($group); |
|
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return array |
|
122
|
|
|
*/ |
|
123
|
1181 |
|
public function __sleep(): array |
|
124
|
|
|
{ |
|
125
|
1181 |
|
$result = ['offset']; |
|
126
|
|
|
|
|
127
|
1181 |
|
if (\method_exists(parent::class, '__sleep')) { |
|
128
|
1181 |
|
return \array_merge(parent::__sleep(), $result); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $result; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return void |
|
136
|
|
|
*/ |
|
137
|
655 |
|
public function __wakeup(): void |
|
138
|
|
|
{ |
|
139
|
655 |
|
$this->completed = true; |
|
140
|
655 |
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return int |
|
144
|
|
|
*/ |
|
145
|
3291 |
|
public function getDeclarationLine(): int |
|
146
|
|
|
{ |
|
147
|
3291 |
|
return $this->getDocument()->getFile()->getPosition($this->offset)->getLine(); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return int |
|
152
|
|
|
*/ |
|
153
|
3291 |
|
public function getDeclarationColumn(): int |
|
154
|
|
|
{ |
|
155
|
3291 |
|
return $this->getDocument()->getFile()->getPosition($this->offset)->getColumn(); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param NodeInterface $ast |
|
160
|
|
|
* @param Document $document |
|
161
|
|
|
* @return void |
|
162
|
|
|
*/ |
|
163
|
6576 |
|
protected function boot(NodeInterface $ast, Document $document): void |
|
164
|
|
|
{ |
|
165
|
6576 |
|
$this->ast = $ast; |
|
166
|
6576 |
|
$this->document = $document; |
|
167
|
|
|
|
|
168
|
|
|
// Generate identifier if id does not initialized |
|
169
|
6576 |
|
$this->getUniqueId(); |
|
|
|
|
|
|
170
|
|
|
|
|
171
|
|
|
// Collect sibling methods |
|
172
|
6576 |
|
foreach (\class_uses_recursive(static::class) as $sibling) { |
|
173
|
6576 |
|
$method = 'compile' . \class_basename($sibling); |
|
174
|
|
|
|
|
175
|
6576 |
|
if (\method_exists($sibling, $method)) { |
|
176
|
6576 |
|
$this->siblingActions[] = $method; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Initialize the name of the type, if it is an independent |
|
182
|
|
|
* unique definition of the type of GraphQL. |
|
183
|
|
|
*/ |
|
184
|
6576 |
|
if ($this instanceof Definition) { |
|
185
|
6576 |
|
$this->resolveTypeName(); |
|
|
|
|
|
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
6576 |
|
if ($this instanceof Compilable && $this instanceof Invocable) { |
|
189
|
1686 |
|
$this->getDocument()->future($this); |
|
190
|
|
|
|
|
191
|
1686 |
|
return; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* If the type is not initialized by the Document, but |
|
196
|
|
|
* is a child of the root, then the lazy assembly is not needed. |
|
197
|
|
|
* |
|
198
|
|
|
* In this case we run it forcibly, and then we check its state. |
|
199
|
|
|
*/ |
|
200
|
6576 |
|
if ($this instanceof DependentDefinition) { |
|
201
|
5975 |
|
$this->getCompiler()->getCallStack()->push($this); |
|
|
|
|
|
|
202
|
|
|
|
|
203
|
|
|
// Force compile dependent definition |
|
204
|
5975 |
|
$this->compile(); |
|
|
|
|
|
|
205
|
|
|
|
|
206
|
|
|
// Normalize types |
|
207
|
5975 |
|
$this->getCompiler()->getTypeCoercion()->apply($this); |
|
|
|
|
|
|
208
|
|
|
|
|
209
|
|
|
// Verify type |
|
210
|
5975 |
|
$this->getValidator(Definitions::class)->validate($this); |
|
|
|
|
|
|
211
|
|
|
|
|
212
|
5972 |
|
$this->getCompiler()->getCallStack()->pop(); |
|
|
|
|
|
|
213
|
|
|
} |
|
214
|
6576 |
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @param string $name |
|
218
|
|
|
* @param string $desc |
|
219
|
|
|
* @return void |
|
220
|
|
|
*/ |
|
221
|
6576 |
|
private function resolveTypeName(string $name = 'Name', string $desc = 'Description'): void |
|
222
|
|
|
{ |
|
223
|
6576 |
|
foreach ($this->getAst()->getChildren() as $child) { |
|
|
|
|
|
|
224
|
6576 |
|
switch ($child->getName()) { |
|
225
|
6576 |
|
case $name: |
|
226
|
6558 |
|
$node = $child->getChild(0); |
|
227
|
6558 |
|
[$this->name, $this->offset] = [$node->getValue(), $node->getOffset()]; |
|
|
|
|
|
|
228
|
6558 |
|
break; |
|
229
|
|
|
|
|
230
|
6576 |
|
case $desc: |
|
231
|
|
|
$this->description = $this->parseDescription($child); |
|
|
|
|
|
|
232
|
6576 |
|
break; |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
6576 |
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* @param NodeInterface|RuleInterface $ast |
|
239
|
|
|
* @return string |
|
240
|
|
|
*/ |
|
241
|
|
|
private function parseDescription(NodeInterface $ast): string |
|
242
|
|
|
{ |
|
243
|
|
|
$description = \trim($this->parseValue( |
|
244
|
|
|
$ast->getChild(0), |
|
|
|
|
|
|
245
|
|
|
'String' |
|
246
|
|
|
)); |
|
247
|
|
|
|
|
248
|
|
|
return $description |
|
249
|
|
|
? \preg_replace('/^\h*#?\h+(.*?)\h*$/imsu', '$1', $description) |
|
250
|
|
|
: $description; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @param NodeInterface $ast |
|
255
|
|
|
* @param string $type |
|
256
|
|
|
* @param array $path |
|
257
|
|
|
* @return array|float|int|null|string |
|
258
|
|
|
*/ |
|
259
|
5326 |
|
protected function parseValue(NodeInterface $ast, string $type, array $path = []) |
|
260
|
|
|
{ |
|
261
|
5326 |
|
return $this->getDocument()->getValueBuilder()->parse($ast, $type, $path); |
|
|
|
|
|
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @param NodeInterface $ast |
|
266
|
|
|
* @return bool |
|
267
|
|
|
*/ |
|
268
|
6167 |
|
protected function onCompile(NodeInterface $ast): bool |
|
|
|
|
|
|
269
|
|
|
{ |
|
270
|
6167 |
|
return false; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* @param string $type |
|
275
|
|
|
* @return TypeDefinition |
|
276
|
|
|
*/ |
|
277
|
6563 |
|
protected function load(string $type): TypeDefinition |
|
278
|
|
|
{ |
|
279
|
6563 |
|
return $this->getCompiler()->getDictionary()->get($type, $this); |
|
|
|
|
|
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* @param array|TypeDefinition|null $field |
|
284
|
|
|
* @param TypeDefinition $definition |
|
285
|
|
|
* @return TypeDefinition|array |
|
286
|
|
|
*/ |
|
287
|
6576 |
|
protected function unique($field, TypeDefinition $definition) |
|
288
|
|
|
{ |
|
289
|
6576 |
|
$this->getValidator(Uniqueness::class)->validate($field, $definition); |
|
|
|
|
|
|
290
|
|
|
|
|
291
|
6576 |
|
if (\is_array($field)) { |
|
292
|
6576 |
|
$field[$definition->getName()] = $definition; |
|
293
|
|
|
|
|
294
|
6576 |
|
return $field; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
144 |
|
return $definition; |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* @param string $keyword |
|
302
|
|
|
* @return int |
|
303
|
|
|
*/ |
|
304
|
6558 |
|
private function offsetPrefixedBy(string $keyword): int |
|
305
|
|
|
{ |
|
306
|
6558 |
|
return $this->offset - \strlen($keyword) - 1; |
|
307
|
|
|
} |
|
308
|
|
|
} |
|
309
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: