1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Railt package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Railt\SDL\Backend\HashTable; |
13
|
|
|
|
14
|
|
|
use Phplrt\Contracts\Ast\NodeInterface; |
15
|
|
|
use Phplrt\Source\Exception\NotAccessibleException; |
16
|
|
|
use Phplrt\Visitor\Visitor; |
17
|
|
|
use Railt\SDL\Backend\HashTableInterface; |
18
|
|
|
use Railt\SDL\Exception\TypeErrorException; |
19
|
|
|
use Railt\SDL\Frontend\Ast\Definition\ArgumentDefinitionNode; |
20
|
|
|
use Railt\SDL\Frontend\Ast\Definition\InputFieldDefinitionNode; |
21
|
|
|
use Railt\SDL\Frontend\Ast\Executable\ArgumentNode; |
22
|
|
|
use Railt\SDL\Frontend\Ast\Value\VariableValueNode; |
23
|
|
|
use Railt\TypeSystem\Value\ValueInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class VariablesVisitor |
27
|
|
|
*/ |
28
|
|
|
class VariablesVisitor extends Visitor |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var HashTableInterface |
32
|
|
|
*/ |
33
|
|
|
private HashTableInterface $hash; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* VariablesVisitor constructor. |
37
|
|
|
* |
38
|
|
|
* @param HashTableInterface $hash |
39
|
|
|
*/ |
40
|
|
|
public function __construct(HashTableInterface $hash) |
41
|
|
|
{ |
42
|
|
|
$this->hash = $hash; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param NodeInterface $node |
47
|
|
|
* @return mixed|ValueInterface|null |
48
|
|
|
* @throws NotAccessibleException |
49
|
|
|
* @throws TypeErrorException |
50
|
|
|
* @throws \RuntimeException |
51
|
|
|
*/ |
52
|
|
|
public function enter(NodeInterface $node) |
53
|
|
|
{ |
54
|
|
|
switch (true) { |
55
|
|
|
case $node instanceof InputFieldDefinitionNode: |
56
|
|
|
case $node instanceof ArgumentDefinitionNode: |
57
|
|
|
$node->defaultValue = $this->resolve($node->defaultValue); |
|
|
|
|
58
|
|
|
break; |
59
|
|
|
|
60
|
|
|
case $node instanceof ArgumentNode: |
61
|
|
|
$node->value = $this->resolve($node->value); |
62
|
|
|
break; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return null; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param ValueInterface|null $value |
70
|
|
|
* @return ValueInterface|null |
71
|
|
|
* @throws NotAccessibleException |
72
|
|
|
* @throws TypeErrorException |
73
|
|
|
* @throws \RuntimeException |
74
|
|
|
*/ |
75
|
|
|
private function resolve(?ValueInterface $value): ?ValueInterface |
76
|
|
|
{ |
77
|
|
|
if ($value instanceof VariableValueNode) { |
78
|
|
|
return $this->fetch($value); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $value; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param VariableValueNode $var |
86
|
|
|
* @return ValueInterface |
87
|
|
|
* @throws NotAccessibleException |
88
|
|
|
* @throws TypeErrorException |
89
|
|
|
* @throws \RuntimeException |
90
|
|
|
*/ |
91
|
|
|
private function fetch(VariableValueNode $var): ValueInterface |
92
|
|
|
{ |
93
|
|
|
$value = $this->hash->get($var->getName(), $var); |
94
|
|
|
|
95
|
|
|
if ($value instanceof VariableValueNode) { |
96
|
|
|
return $this->fetch($value); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $value; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|