-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractAppBootstrapVisitor.php
More file actions
159 lines (123 loc) · 4.42 KB
/
AbstractAppBootstrapVisitor.php
File metadata and controls
159 lines (123 loc) · 4.42 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
<?php
namespace RonasIT\Larabuilder\Visitors\AppBootstrapVisitors;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Enum_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Nop;
use PhpParser\Node\Stmt\Trait_;
use PhpParser\NodeVisitorAbstract;
use RonasIT\Larabuilder\Exceptions\InvalidBootstrapAppFileException;
abstract class AbstractAppBootstrapVisitor extends NodeVisitorAbstract
{
protected const array FORBIDDEN_NODES = [
Class_::class,
Trait_::class,
Interface_::class,
Enum_::class,
];
protected static array $existingParentNodes = [];
abstract protected function getInsertableNode(): Expression;
public function __construct(
protected string $parentMethod,
protected string $targetMethod,
protected array $closureParams = [],
) {
}
public function afterTraverse(array $nodes): ?array
{
static::$existingParentNodes = [];
return null;
}
public function enterNode(Node $node): void
{
$isNotBootstrapAppFile = array_any(self::FORBIDDEN_NODES, fn ($type) => $node instanceof $type);
if ($isNotBootstrapAppFile) {
throw new InvalidBootstrapAppFileException(class_basename($node));
}
if ($node instanceof MethodCall && $node->name->toString() === $this->parentMethod) {
static::$existingParentNodes[] = $this->parentMethod;
}
}
public function leaveNode(Node $node): Node
{
if ($node instanceof MethodCall && $node->name->toString() === 'create') {
if (!in_array($this->parentMethod, static::$existingParentNodes)) {
$node = $this->insertParentNode($node);
}
if ($node->var->getAttribute('wasCreated')) {
$node->var = $this->handleParentNode($node->var);
}
}
if (!$node instanceof MethodCall) {
return $node;
}
if ($this->isParentNode($node) && $this->shouldInsertNode($node)) {
return $this->insertNode($node);
}
return $node;
}
protected function insertParentNode(Node $node): Node
{
static::$existingParentNodes[] = $this->parentMethod;
$closure = new Closure([
'params' => $this->closureParams,
'returnType' => new Identifier('void'),
]);
$parentCall = new MethodCall($node->var, new Identifier($this->parentMethod), [new Arg($closure)]);
$parentCall->setAttribute('wasCreated', true);
return new MethodCall($parentCall, new Identifier('create'));
}
protected function handleParentNode(MethodCall $node): Node
{
if ($this->isParentNode($node) && $this->shouldInsertNode($node)) {
return $this->insertNode($node);
}
return $node;
}
protected function isParentNode(Node $node): bool
{
return $node instanceof MethodCall && $node->name->toString() === $this->parentMethod;
}
protected function shouldInsertNode(MethodCall $node): bool
{
foreach ($node->args[0]->value->stmts as $stmt) {
if (!$stmt instanceof Expression) {
continue;
}
if (!$this->isCallbackCall($stmt)) {
continue;
}
if ($this->matchesCustomCriteria($stmt)) {
return false;
}
}
return true;
}
protected function insertNode(MethodCall $node): MethodCall
{
$currentStatements = $node->args[0]->value->stmts;
$statement = $this->getInsertableNode();
if (count($currentStatements) === 1 && $currentStatements[0] instanceof Nop) {
$node->args[0]->value->stmts = [$statement];
return $node;
}
$lastExistingStatement = end($currentStatements);
$statement->setAttribute('previous', $lastExistingStatement);
$node->args[0]->value->stmts[] = $statement;
return $node;
}
protected function matchesCustomCriteria(Expression $statement): bool
{
return false;
}
protected function isCallbackCall(Expression $stmt): bool
{
return $stmt->expr instanceof MethodCall && $stmt->expr->name->toString() === $this->targetMethod;
}
}