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
197 lines (170 loc) · 7.08 KB
/
Environment.php
File metadata and controls
197 lines (170 loc) · 7.08 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
<?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\FloatValue;
use Codewithkyrian\Jinja\Runtime\FunctionValue;
use Codewithkyrian\Jinja\Runtime\IntegerValue;
use Codewithkyrian\Jinja\Runtime\KeywordArgumentsValue;
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 (?KeywordArgumentsValue $args = null) {
if (!$args) {
return new ObjectValue([]);
}
return new ObjectValue($args->value);
})
];
$this->tests = [
'boolean' => fn(RuntimeValue $operand) => $operand->type === "BooleanValue",
'callable' => fn(RuntimeValue $operand) => $operand instanceof FunctionValue,
'odd' => function (RuntimeValue $operand) {
if (!($operand instanceof IntegerValue)) {
throw new RuntimeException("Cannot apply test 'odd' to type: $operand->type");
}
return $operand->value % 2 !== 0;
},
'even' => function (RuntimeValue $operand) {
if (!($operand instanceof IntegerValue)) {
throw new RuntimeException("Cannot apply test 'even' to type: $operand->type");
}
return $operand->value % 2 === 0;
},
'false' => fn(RuntimeValue $operand) => $operand instanceof BooleanValue && !$operand->value,
'true' => fn(RuntimeValue $operand) => $operand instanceof BooleanValue && $operand->value,
'null' => fn(RuntimeValue $operand) => $operand instanceof NullValue,
'string' => fn(RuntimeValue $operand) => $operand instanceof StringValue,
'number' => fn(RuntimeValue $operand) => $operand instanceof IntegerValue || $operand instanceof FloatValue,
'integer' => fn(RuntimeValue $operand) => $operand instanceof IntegerValue,
'iterable' => fn(RuntimeValue $operand) => $operand instanceof ArrayValue || $operand instanceof StringValue,
'mapping' => fn(RuntimeValue $operand) => $operand instanceof ObjectValue,
'lower' => fn(RuntimeValue $operand) => $operand instanceof StringValue && $operand->value === strtolower($operand->value),
'upper' => fn(RuntimeValue $operand) => $operand instanceof StringValue && $operand->value === strtoupper($operand->value),
'none' => fn(RuntimeValue $operand) => $operand instanceof NullValue,
'defined' => fn(RuntimeValue $operand) => $operand instanceof UndefinedValue,
'undefined' => fn(RuntimeValue $operand) => $operand instanceof UndefinedValue,
'equalto' => fn(RuntimeValue $a, RuntimeValue $b) => $a->value === $b->value,
'eq' => fn(RuntimeValue $a, RuntimeValue $b) => $a->value === $b->value,
'ne' => fn(RuntimeValue $a, RuntimeValue $b) => $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();
}
}
/**
* Helper function to convert a PHP value to a runtime value.
*/
public static function convertToRuntimeValues(mixed $input): RuntimeValue
{
if (is_int($input)) {
return new IntegerValue($input);
}
if (is_float($input)) {
return new FloatValue($input);
}
if (is_string($input)) {
return new StringValue($input);
}
if (is_bool($input)) {
return new BooleanValue($input);
}
if (is_callable($input)) {
return new FunctionValue(function ($args, $scope) use ($input) {
$plainArgs = array_map(fn($arg) => $arg->value, $args);
$result = call_user_func_array($input, $plainArgs);
return $this->convertToRuntimeValues($result);
});
}
if (is_array($input)) {
if (array_is_list($input)) {
return new ArrayValue(array_map(self::convertToRuntimeValues(...), $input));
}
$convertedItems = [];
foreach ($input as $key => $value) {
$convertedItems[$key] = self::convertToRuntimeValues($value);
}
return new ObjectValue($convertedItems);
}
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");
}
}