forked from CodeWithKyrian/jinja-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnvironment.php
More file actions
217 lines (190 loc) · 7.51 KB
/
Environment.php
File metadata and controls
217 lines (190 loc) · 7.51 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
<?php
declare(strict_types=1);
namespace Codewithkyrian\Jinja\Core;
use Codewithkyrian\Jinja\Exceptions\RuntimeException;
use Codewithkyrian\Jinja\Exceptions\SyntaxError;
use Codewithkyrian\Jinja\Runtime\ArrayValue;
use Codewithkyrian\Jinja\Runtime\BooleanValue;
use Codewithkyrian\Jinja\Runtime\FunctionValue;
use Codewithkyrian\Jinja\Runtime\NullValue;
use Codewithkyrian\Jinja\Runtime\NumericValue;
use Codewithkyrian\Jinja\Runtime\ObjectValue;
use Codewithkyrian\Jinja\Runtime\RuntimeValue;
use Codewithkyrian\Jinja\Runtime\StringValue;
use Codewithkyrian\Jinja\Runtime\UndefinedValue;
/**
* Represents the current environment (scope) at runtime.
*/
class Environment
{
/**
* @var array The variables declared in this environment.
*/
protected array $variables = [];
/**
* @var array The tests available in this environment.
*/
public array $tests = [];
/**
* @var ?Environment The parent environment, if any.
*/
protected ?Environment $parent = null;
public function __construct(?Environment $parent = null)
{
$this->parent = $parent;
$this->variables = [
'namespace' => new FunctionValue(function ($args) {
if (count($args) === 0) {
return new ObjectValue([]);
}
if (count($args) !== 1 || !($args[0] instanceof ObjectValue)) {
throw new RuntimeException("`namespace` expects either zero arguments or a single object argument");
}
return $args[0];
})
];
$this->tests = [
'boolean' => fn(RuntimeValue $operand) => $operand->type === "BooleanValue",
'callable' => fn(RuntimeValue $operand) => $operand instanceof FunctionValue,
'odd' => function (RuntimeValue $operand) {
if ($operand->type !== "NumericValue") {
throw new RuntimeException("Cannot apply test 'odd' to type: $operand->type");
}
return $operand->value % 2 !== 0;
},
'even' => function (RuntimeValue $operand) {
if ($operand->type !== "NumericValue") {
throw new RuntimeException("Cannot apply test 'even' to type: $operand->type");
}
return $operand->value % 2 === 0;
},
'false' => fn(RuntimeValue $operand) => $operand->type === "BooleanValue" && !$operand->value,
'true' => fn(RuntimeValue $operand) => $operand->type === "BooleanValue" && $operand->value,
'number' => fn(RuntimeValue $operand) => $operand->type === "NumericValue",
'integer' => function (RuntimeValue $operand) {
if ($operand->type !== "NumericValue") {
return false;
}
return is_int($operand->value);
},
'iterable' => fn(RuntimeValue $operand) => $operand instanceof ArrayValue || $operand instanceof StringValue,
'lower' => function (RuntimeValue $operand) {
if ($operand->type !== "StringValue") {
return false;
}
return $operand->value === strtolower($operand->value);
},
'upper' => function (RuntimeValue $operand) {
if ($operand->type !== "StringValue") {
return false;
}
return $operand->value === strtoupper($operand->value);
},
'none' => function (RuntimeValue $operand) {
return $operand->type === "NullValue";
},
'defined' => function (RuntimeValue $operand) {
return $operand->type !== "UndefinedValue";
},
'undefined' => function (RuntimeValue $operand) {
return $operand->type === "UndefinedValue";
},
'equalto' => function (RuntimeValue $a, RuntimeValue $b) {
return $a->value === $b->value;
},
];
}
/**
* Set the value of a variable in the current environment.
*/
public function set(string $name, $value): RuntimeValue
{
return $this->declareVariable($name, self::convertToRuntimeValues($value));
}
private function declareVariable(string $name, RuntimeValue $value): RuntimeValue
{
if (array_key_exists($name, $this->variables)) {
throw new SyntaxError("Variable already declared: $name");
}
$this->variables[$name] = $value;
return $value;
}
public function setVariable($name, $value)
{
$this->variables[$name] = $value;
return $value;
}
/**
* Resolve the environment in which the variable is declared.
*/
private function resolve(string $name): Environment
{
if (array_key_exists($name, $this->variables)) {
return $this;
}
if ($this->parent !== null) {
return $this->parent->resolve($name);
}
throw new RuntimeException("Unknown variable: $name");
}
public function lookupVariable(string $name): RuntimeValue
{
try {
$environment = $this->resolve($name);
return $environment->variables[$name] ?? new UndefinedValue();
} catch (\Exception $e) {
return new UndefinedValue();
}
}
public static function convertToRuntimeValues(mixed $input): RuntimeValue
{
if (is_numeric($input)) {
return new NumericValue(floatval($input));
}
if (is_string($input)) {
return new StringValue($input);
}
if (is_bool($input)) {
return new BooleanValue($input);
}
if (is_callable($input)) {
// Wrap the PHP callable in a runtime function value
return new FunctionValue(function ($args, $scope) use ($input) {
// Assuming args are also runtime values; extract their inner values for the callable
$plainArgs = array_map(function ($arg) {
return $arg->value;
}, $args);
$result = call_user_func_array($input, $plainArgs);
// Convert the result back to a runtime value
return $this->convertToRuntimeValues($result);
});
}
if (is_array($input)) {
if (array_keys($input) !== range(0, count($input) - 1)) {
// Associative array, treat as ObjectValue
$convertedItems = [];
foreach ($input as $key => $value) {
$convertedItems[$key] = self::convertToRuntimeValues($value);
}
return new ObjectValue($convertedItems);
} else {
// Sequential array, treat as ArrayValue
return new ArrayValue(array_map(self::convertToRuntimeValues(...), $input));
}
}
if (is_callable($input)) {
return new FunctionValue(function ($args) use ($input) {
$plainArgs = array_map(function ($arg) {
return $arg->value;
}, $args);
$result = call_user_func_array($input, $plainArgs);
// Convert the result back to a runtime value
return self::convertToRuntimeValues($result);
});
}
if (is_null($input)) {
return new NullValue();
}
throw new RuntimeException("Cannot convert to runtime value: Unsupported type");
}
}