-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWalker.php
147 lines (122 loc) · 3.21 KB
/
Walker.php
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
<?php declare(strict_types=1);
/*
* Copyright (C) Apis Networks, Inc - All Rights Reserved.
*
* Unauthorized copying of this file, via any medium, is
* strictly prohibited without consent. Any dissemination of
* material herein is prohibited.
*
* For licensing inquiries email <licensing@apisnetworks.com>
*
* Written by Matt Saladna <matt@apisnetworks.com>, January 2024
*/
namespace Module\Support\Webapps\App\Type\Vanilla;
use Module\Support\Php\TreeWalker;
use PhpParser\ConstExprEvaluationException;
use PhpParser\ConstExprEvaluator;
use PhpParser\Node;
use PhpParser\Node\Stmt;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
class Walker extends TreeWalker
{
const STORAGE_VAR = 'Configuration';
public function replace(string $var, mixed $new): TreeWalker
{
return $this->walkReplace($var, $new, false);
}
public function set(string $var, mixed $new): TreeWalker
{
return $this->walkReplace($var, $new, true);
}
protected function walkReplace(string $var, mixed $new, bool $append = false): self
{
$replacement = $this->inferType($new);
$node = $this->locateNode($var);
$traverser = new NodeTraverser;
if (!$node) {
if ($append) {
$this->ast[] = $this->buildDimNode($var, $new);
}
return $this;
}
$traverser->addVisitor(new class ($node, $replacement) extends NodeVisitorAbstract {
public function __construct(
private ?Node $node,
private Node $replacement
) { }
public function leaveNode(Node $node) {
if ($node !== $this->node) {
return;
}
$node->expr->expr = $this->replacement;
}
});
$traverser->traverse($this->ast);
return $this;
}
private function buildDimNode(string $var, mixed $val): ?Node
{
$components = explode('.', $var);
$stack = new Node\Expr\ArrayDimFetch(
new Node\Expr\Variable(
self::STORAGE_VAR
),
$this->inferType(current($components))
);
while (false !== ($next = next($components))) {
$stack = new Node\Expr\ArrayDimFetch(
$stack,
$this->inferType($next)
);
}
$node = new Node\Expr\Assign(
$stack,
$this->inferType($val)
);
return new Stmt\Expression(
$node
);
}
private function locateNode(string $var): ?Node
{
$components = explode('.', $var);
return $this->first(function (Node $stmt) use ($components) {
if (!isset($stmt->expr) || !$stmt->expr instanceof Node\Expr\Assign || !$stmt->expr->var instanceof Node\Expr\ArrayDimFetch) {
return false;
}
end($components);
$head = $stmt->expr->var;
do {
if ($head->dim->value !== current($components)) {
return false;
}
$head = $head->var;
} while (false !== prev($components));
if ($head->name !== self::STORAGE_VAR) {
return false;
}
return $stmt;
});
}
/**
* Supports dot-delimited nested values
*
* @param string $var
* @param mixed|null $default
* @return mixed
*/
public function get(string $var, mixed $default = null): mixed
{
if (null === ($node = $this->locateNode($var))) {
return $default;
}
try {
return (new ConstExprEvaluator)->evaluateSilently($node->expr->expr);
} catch (ConstExprEvaluationException $expr) {
return (new \PhpParser\PrettyPrinter\Standard())->prettyPrint(
[$node]
);
}
}
}