|
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\Invocations; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\Parser\Ast\NodeInterface; |
|
13
|
|
|
use Railt\Parser\Ast\RuleInterface; |
|
14
|
|
|
use Railt\SDL\Base\Invocations\BaseDirectiveInvocation; |
|
15
|
|
|
use Railt\SDL\Contracts\Definitions\DirectiveDefinition; |
|
16
|
|
|
use Railt\SDL\Contracts\Definitions\TypeDefinition; |
|
17
|
|
|
use Railt\SDL\Contracts\Dependent\ArgumentDefinition; |
|
18
|
|
|
use Railt\SDL\Contracts\Document; |
|
19
|
|
|
use Railt\SDL\Exceptions\TypeConflictException; |
|
20
|
|
|
use Railt\SDL\Reflection\Builder\DocumentBuilder; |
|
21
|
|
|
use Railt\SDL\Reflection\Builder\Process\Compilable; |
|
22
|
|
|
use Railt\SDL\Reflection\Builder\Process\Compiler; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class DirectiveInvocationBuilder |
|
26
|
|
|
*/ |
|
27
|
|
|
class DirectiveInvocationBuilder extends BaseDirectiveInvocation implements Compilable |
|
28
|
|
|
{ |
|
29
|
|
|
use Compiler; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* DirectiveInvocationBuilder constructor. |
|
33
|
|
|
* @param NodeInterface $ast |
|
34
|
|
|
* @param DocumentBuilder|Document $document |
|
35
|
|
|
* @param TypeDefinition $parent |
|
36
|
|
|
* @throws \Railt\SDL\Exceptions\TypeConflictException |
|
37
|
|
|
*/ |
|
38
|
894 |
|
public function __construct(NodeInterface $ast, DocumentBuilder $document, TypeDefinition $parent) |
|
39
|
|
|
{ |
|
40
|
894 |
|
$this->parent = $parent; |
|
41
|
894 |
|
$this->boot($ast, $document); |
|
42
|
894 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param NodeInterface $ast |
|
46
|
|
|
* @return bool |
|
47
|
|
|
* @throws \Railt\SDL\Exceptions\TypeConflictException |
|
48
|
|
|
*/ |
|
49
|
894 |
|
protected function onCompile(NodeInterface $ast): bool |
|
50
|
|
|
{ |
|
51
|
894 |
|
if ($ast->is('Argument')) { |
|
52
|
12 |
|
[$name, $value] = $this->parseArgumentValue($ast); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** @var DirectiveDefinition $definition */ |
|
55
|
12 |
|
$definition = $this->getTypeDefinition(); |
|
56
|
|
|
/** @var ArgumentDefinition|null $argument */ |
|
57
|
12 |
|
$argument = $definition->getArgument($name); |
|
58
|
|
|
|
|
59
|
12 |
|
if (! $argument) { |
|
60
|
6 |
|
$this->getCompiler()->getCallStack()->push($definition); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
6 |
|
$error = \sprintf('Argument %s not defined in %s', $name, $definition); |
|
63
|
6 |
|
throw new TypeConflictException($error, $this->getCompiler()->getCallStack()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
6 |
|
$typeName = $argument->getTypeDefinition()->getName(); |
|
67
|
|
|
|
|
68
|
6 |
|
$this->arguments[$name] = $this->parseValue($value, $typeName); |
|
69
|
|
|
|
|
70
|
6 |
|
return true; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
894 |
|
return false; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param NodeInterface|RuleInterface $ast |
|
78
|
|
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
12 |
|
private function parseArgumentValue(NodeInterface $ast): array |
|
81
|
|
|
{ |
|
82
|
12 |
|
[$key, $value] = [null, null]; |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
12 |
|
foreach ($ast->getChildren() as $child) { |
|
|
|
|
|
|
85
|
12 |
|
if ($child->is('Name')) { |
|
86
|
12 |
|
$key = $child->getChild(0)->getValue(); |
|
87
|
12 |
|
continue; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
12 |
|
if ($child->is('Value')) { |
|
91
|
12 |
|
$value = $child->getChild(0); |
|
92
|
12 |
|
continue; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
12 |
|
return [$key, $value]; |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return null|TypeDefinition |
|
101
|
|
|
*/ |
|
102
|
900 |
|
public function getTypeDefinition(): ?TypeDefinition |
|
103
|
|
|
{ |
|
104
|
900 |
|
return $this->load($this->getName()); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.