-
Notifications
You must be signed in to change notification settings - Fork 0
/
generation.php
186 lines (154 loc) · 4.62 KB
/
generation.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
use Revobot\Services\Providers\Tg;
require_once __DIR__ . '/vendor/autoload.php';
// require_once __DIR__ . '/config.php';
define('COMMANDS_PATH', __DIR__ . '/src/Commands');
define('BUILD_PATH', __DIR__ . '/build.txt');
incBuild(getBuild());
echo "Update build number in build.txt\n";
processFiles();
function processFiles()
{
$help_str = "";
$switch = '';
$commands = [];
$tg_commands = [];
$f = get_dir_files(COMMANDS_PATH);
foreach ($f as $file) {
if (substr(basename($file), -7) !== 'Cmd.php') {
continue;
}
$class = str_replace('.php', '', basename($file));
$namespace = str_replace(__DIR__, '', $file);
$namespace = str_replace(basename($file), '', $namespace);
$namespace = str_replace('/', '\\', $namespace);
$namespace = str_replace('\src', 'Revobot', $namespace);
//echo $namespace;
$reflector = new ReflectionClass($namespace . $class);
// echo $namespace . $class . "\n";
$constants = $reflector->getConstants();
if (!isset($constants['IS_ENABLED'])) {
continue;
}
if (!$constants['IS_ENABLED']) {
continue;
}
if (!isset($constants['IS_ADMIN_ONLY']) && !isset($constants['IS_HIDDEN'])) {
$help_str .= '/' . $constants['KEYS'][0] . ' - ' . $constants['HELP_DESCRIPTION'] . "\n";
$tg_commands[] = ['command' => $constants['KEYS'][0], 'description' => $constants['HELP_DESCRIPTION']];
}
$commands = array_merge($commands, $constants['KEYS']);
try {
$start_params = array_column($reflector->getConstructor()->getParameters(), 'name');
$switch .= generateSwitch($namespace . $class, $constants['KEYS'], $start_params);
} catch (Exception $e) {
continue;
}
}
$helpCmd = generateHelpCmd($help_str);
$commands_arr = '\'' . implode('\',\'', $commands) . "'";
$commandsManager = generateCommandsManager($commands_arr, $switch);
file_put_contents(COMMANDS_PATH . '/HelpCmd.php', $helpCmd);
file_put_contents(COMMANDS_PATH . '/../CommandsManager.php', $commandsManager);
echo "generated: HelpCmd.php, CommandsManager.php\n";
$tg_json_cmd = json_encode($tg_commands);
updateTgCommands($tg_json_cmd);
echo "Updated tg commands\n";
}
function get_dir_files($dir, $recursive = true, $include_folders = false)
{
if (!is_dir($dir))
return [];
$files = [];
$dir = rtrim($dir, '/\\'); // удалим слэш на конце
foreach (glob("$dir/{,.}[!.,!..]*", GLOB_BRACE) as $file) {
if (is_dir($file)) {
if ($include_folders)
$files[] = $file;
if ($recursive)
$files = array_merge($files, call_user_func(__FUNCTION__, $file, $recursive, $include_folders));
} else
$files[] = $file;
}
return $files;
}
function generateHelpCmd($commands)
{
return <<<PHP
<?php
/*
Autogenerated code
*/
namespace Revobot\Commands;
class HelpCmd extends BaseCmd
{
const KEYS = ['help','хэлп','хлеп', 'помощь','start'];
const IS_ENABLED = true;
const HELP_DESCRIPTION = 'Помощь';
/**
* @return string
*/
public function exec(): string
{
return "Список команд бота:\n{$commands}";
}
}
PHP;
}
function getBuild()
{
$v = file_get_contents(BUILD_PATH);
return (int)$v;
}
function incBuild($build)
{
$build++;
file_put_contents(BUILD_PATH, $build);
return $build;
}
function generateCommandsManager($commands, $switch)
{
return <<<TEXT
<?php
/*
Autogenerated code
*/
namespace Revobot;
class CommandsManager extends CommandsManagerBase
{
public const COMMANDS = [
{$commands}
];
/**
* @param Revobot \$bot
* @param string \$command
* @param string \$input
* @return string
*/
public static function run(Revobot \$bot, string \$command, string \$input): string
{
switch (\$command) {
{$switch}
default:
\$response = '';
}
dbg_echo('cmd:' . \$command . ',inp:' . \$input . ',response: ' . \$response . "\\n");
return \$response;
}
}
TEXT;
}
function generateSwitch($class, $commands, $start_params)
{
$result = '';
foreach ($commands as $cmd) {
$result .= "case '{$cmd}':\n";
}
$params = '$' . implode(', $', $start_params);
$result .= "\t\$response = (new \\{$class}($params))->exec();\n\tbreak;\n";
return $result;
}
function updateTgCommands(string $commands)
{
return Tg::setMyCommands($commands);
}