-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.php
96 lines (66 loc) · 1.79 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
<?php
$path = file_get_contents('path.json');
$folders = json_decode($path)->folders;
$exactPaths = json_decode($path)->exact_paths;
foreach ($folders as $folder) {
$files = glob( $folder . '*.php');
if (empty($files)) {
echo "Ошибка пути {$folder} ";
continue;
}
foreach ($files as $file) {
reorderConstants($file);
}
}
foreach ($exactPaths as $exactPath) {
$file = glob($exactPath);
if (empty($file)) {
echo "Ошибка пути {$exactPath} ";
continue;
}
$file = $file[0];
reorderConstants($file);
}
/**
* @param string $file
* @return void
*/
function reorderConstants(string $file): void
{
$content = file_get_contents($file);
preg_match('/class (\w+)/', $content, $classNameMatches);
if (empty($classNameMatches)) {
return;
}
$className = $classNameMatches[1];
preg_match_all('/public const (\w+) = ([0-9]+);/', $content, $matches, PREG_SET_ORDER);
if (empty($matches)) {
return;
}
$constants = [];
foreach ($matches as $match) {
$constants[$match[1]] = $match[2];
}
asort($constants);
$content = preg_replace(
'/class ' . preg_quote($className) . '[\s\S]+}/',
generateNewContent(constants: $constants, className: $className),
$content
);
file_put_contents($file, $content);
}
/**
* @param array $constants
* @param string $className
* @return string
*/
function generateNewContent(array $constants, string $className): string
{
$content = "class $className\n{\n";
foreach ($constants as $constant => $value) {
$content .= " public const $constant = $value;\n";
}
$content .= "}\n";
return $content;
}
echo PHP_EOL . 'Скрипт выполнен.' . PHP_EOL;