-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.php
142 lines (118 loc) · 4.84 KB
/
script.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
<?php
declare(strict_types=1);
require 'vendor/autoload.php';
require 'code_mapping.php';
use PhpParser\Error;
use PhpParser\Modifiers;
use PhpParser\Node;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter;
use PhpParser\NodeVisitor\ParentConnectingVisitor;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\Node\Stmt\Class_;
// Diretório base a ser vasculhado
$baseDir = __DIR__ . '/src'; // Altere para o caminho correto
// Função para percorrer o diretório recursivamente
function scanDirectory(string $dir, array $codeMapping)
{
$files = scandir($dir);
foreach ($files as $file) {
if ($file === '.' || $file === '..') {
continue;
}
$filePath = $dir . DIRECTORY_SEPARATOR . $file;
if (is_dir($filePath)) {
scanDirectory($filePath, $codeMapping);
} elseif (is_file($filePath) && pathinfo($filePath, PATHINFO_EXTENSION) === 'php') {
processFile($filePath, $codeMapping);
}
}
}
// Função para processar cada arquivo
function processFile(string $filePath, array $codeMapping)
{
$code = file_get_contents($filePath);
$parserFactory = new ParserFactory();
$parser = $parserFactory->createForNewestSupportedVersion();
try {
$ast = $parser->parse($code);
} catch (Error $error) {
echo "Erro ao analisar o arquivo {$filePath}: {$error->getMessage()}\n";
return;
}
$traverser = new NodeTraverser();
// Adicionar NameResolver
$traverser->addVisitor(new NameResolver());
// Adicionar ParentConnectingVisitor
$traverser->addVisitor(new ParentConnectingVisitor());
$visitor = new class ($codeMapping) extends NodeVisitorAbstract {
private $codeMapping;
private $currentClass;
private $classNode;
public function __construct(array $codeMapping)
{
$this->codeMapping = $codeMapping;
}
public function enterNode(Node $node)
{
if ($node instanceof Node\Stmt\Class_) {
if (isset($node->namespacedName)) {
$this->currentClass = $node->namespacedName->toString();
} else {
$this->currentClass = $node->name->toString();
}
$this->classNode = $node;
}
if ($node instanceof Node\Stmt\ClassMethod && $node->isStatic()) {
if ($this->currentClass && isset($this->codeMapping[$this->currentClass])) {
$methodName = $node->name->toString();
if (isset($this->codeMapping[$this->currentClass][$methodName])) {
$codeInt = $this->codeMapping[$this->currentClass][$methodName];
// Adicionar a constante de código inteiro na classe, se ainda não existir
$constName = 'CODE_' . strtoupper($methodName);
// Verificar se a constante já existe
$constExists = false;
foreach ($this->classNode->stmts as $stmt) {
if ($stmt instanceof Node\Stmt\ClassConst) {
foreach ($stmt->consts as $const) {
if ($const->name->toString() === $constName) {
$constExists = true;
break 2;
}
}
}
}
if (!$constExists) {
$const = new Node\Stmt\ClassConst(
[
new Node\Const_(
new Node\Identifier($constName),
new Int_($codeInt)
)
],
Modifiers::PRIVATE
);
// Inserir a constante no início dos statements da classe
array_unshift($this->classNode->stmts, $const);
}
}
}
}
}
};
$traverser->addVisitor($visitor);
$modifiedAst = $traverser->traverse($ast);
// Converter o AST de volta para código PHP
$prettyPrinter = new PrettyPrinter\Standard();
$modifiedCode = $prettyPrinter->prettyPrintFile($modifiedAst);
// Salvar o código modificado de volta no arquivo
file_put_contents($filePath, $modifiedCode);
echo "Arquivo atualizado: {$filePath}\n";
}
// Iniciar o processo de varredura
scanDirectory($baseDir, $codeMapping);
echo "Atualização concluída.\n";