-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPHPFileBuilder.php
More file actions
124 lines (95 loc) · 3.87 KB
/
PHPFileBuilder.php
File metadata and controls
124 lines (95 loc) · 3.87 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
<?php
namespace RonasIT\Larabuilder\Builders;
use PhpParser\Error;
use PhpParser\NodeVisitor\CloningVisitor;
use PhpParser\ParserFactory;
use RonasIT\Larabuilder\Enums\AccessModifierEnum;
use RonasIT\Larabuilder\Enums\DefaultValue;
use RonasIT\Larabuilder\Enums\InsertPositionEnum;
use RonasIT\Larabuilder\Exceptions\InvalidPHPFileException;
use RonasIT\Larabuilder\NodeTraverser;
use RonasIT\Larabuilder\Printer;
use RonasIT\Larabuilder\ValueOptions\MethodParams;
use RonasIT\Larabuilder\Visitors\AddImports;
use RonasIT\Larabuilder\Visitors\AddTraits;
use RonasIT\Larabuilder\Visitors\MethodVisitors\AddItemToReturnArray;
use RonasIT\Larabuilder\Visitors\MethodVisitors\AddMethod;
use RonasIT\Larabuilder\Visitors\MethodVisitors\InsertCodeToMethod;
use RonasIT\Larabuilder\Visitors\PropertyVisitors\AddArrayPropertyItem;
use RonasIT\Larabuilder\Visitors\PropertyVisitors\RemoveArrayPropertyItem;
use RonasIT\Larabuilder\Visitors\PropertyVisitors\SetProperty;
class PHPFileBuilder
{
protected array $syntaxTree;
protected array $oldTokens;
protected NodeTraverser $traverser;
public function __construct(
protected string $filePath,
) {
$parser = new ParserFactory()->createForHostVersion();
$code = file_get_contents($this->filePath);
try {
$this->syntaxTree = $parser->parse($code);
} catch (Error) {
throw new InvalidPHPFileException($this->filePath);
}
$this->oldTokens = $parser->getTokens();
$this->traverser = new NodeTraverser();
}
public function setProperty(string $name, mixed $value, AccessModifierEnum $accessModifier = AccessModifierEnum::Public): self
{
$this->traverser->addVisitor(new SetProperty($name, $value, $accessModifier));
return $this;
}
public function addArrayPropertyItem(string $propertyName, mixed $value): self
{
$this->traverser->addVisitor(new AddArrayPropertyItem($propertyName, $value));
return $this;
}
public function removeArrayPropertyItem(string $propertyName, array $values): self
{
$this->traverser->addVisitor(new RemoveArrayPropertyItem($propertyName, $values));
return $this;
}
public function addImports(array $imports): self
{
$this->traverser->addVisitor(new AddImports($imports));
return $this;
}
public function addTraits(array $traits): self
{
$this->traverser->addVisitor(new AddTraits($traits));
$this->addImports($traits);
return $this;
}
public function addMethod(
string $name,
string $code,
MethodParams $params = new MethodParams(),
?string $returnType = null,
AccessModifierEnum $accessModifier = AccessModifierEnum::Public,
bool $static = false,
bool $returnsByRef = false,
): self {
$this->traverser->addVisitor(new AddMethod($name, $code, $params, $returnType, $accessModifier, $static, $returnsByRef));
return $this;
}
public function addItemToReturnArray(string $methodName, string $value, string|DefaultValue $key = DefaultValue::None): self
{
$this->traverser->addVisitor(new AddItemToReturnArray($methodName, $value, $key));
return $this;
}
public function insertCodeToMethod(string $methodName, string $code, InsertPositionEnum $position = InsertPositionEnum::End): self
{
$this->traverser->addVisitor(new InsertCodeToMethod($methodName, $code, $position));
return $this;
}
public function save(): void
{
$this->traverser->addVisitor(new CloningVisitor());
$oldSyntaxTree = $this->syntaxTree;
$newSyntaxTree = $this->traverser->traverse($this->syntaxTree);
$fileContent = new Printer()->printFormatPreserving($newSyntaxTree, $oldSyntaxTree, $this->oldTokens);
file_put_contents($this->filePath, $fileContent);
}
}