-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathadviser.php
113 lines (87 loc) · 3.12 KB
/
adviser.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
<?php
class Adviser
{
private $path;
private $phalconClasses;
private $logFile;
public function __construct(array $phalconClasses, string $path, string $logFile) {
$this->phalconClasses = $phalconClasses;
$this->path = $path;
$this->logFile = (empty($logFile)) ? "upgradeLog.txt" : $logFile;
}
public function createLogAction()
{
if (empty($this->path)) {
die("Please provide the path to the Application");
}
if (is_file($this->path)) {
die($this->logPhalconClassesState($this->path));
}
$phpFiles = [];
$this->getPhpFiles($this->path, $phpFiles);
if (empty($phpFiles)) {
die("No PHP files found in $this->path");
}
$log = $this->processPhpFiles($phpFiles);
file_put_contents($this->logFile, $log);
echo "Check '$this->logFile' to review the necessary changes to upgrade\n";
}
private function processPhpFiles(array $files): string
{
$log = "";
foreach ($files as $file) {
$log .= $this->logPhalconClassesState($file);
}
return $log;
}
private function getPhpFiles(string $dir, array &$phpFiles = [])
{
$files = scandir($dir);
foreach ($files as $key => $value) {
$path = realpath($dir . DIRECTORY_SEPARATOR . $value);
if (is_file($path)) {
if (pathinfo($path, PATHINFO_EXTENSION) === "php") {
$phpFiles[] = $path;
}
} else if ($value != "." && $value != ".." && $value != "vendor" && $value != ".git") {
$this->getPhpFiles($path, $phpFiles);
if (pathinfo($path, PATHINFO_EXTENSION) === "php") {
$phpFiles[] = $path;
}
}
}
}
private function logPhalconClassesState(string $file): string
{
try {
$fn = fopen($file, "r");
} catch (exception $e) {
return "Error opening $file => " . $e->getMessage() . ";\n";
}
$classes = [];
while(! feof($fn)) {
if (preg_match("/Phalcon\\\([^\s;(]+)/", fgets($fn), $match)) {
$classes[] = $match[0];
}
}
fclose($fn);
if (count($classes) == 0) {
return $file . ":\nNo Phalcon classes found\n\n";
}
return $file . ":\n" . $this->checkClassState($classes) . "\n\n";
}
private function checkClassState(array $classes):string
{
$log = "";
foreach ($classes as $class) {
if (isset($this->phalconClasses[$class])) {
$log .= $class . " => " . $this->phalconClasses[$class] . "\n";
} elseif (strpos($class, "::") > 0) {
$log .= $class . " => Check possible changes in constant\n";
} else {
$log .= $class . " => No changes\n";
}
}
return $log;
}
}