HEX
Server: Apache/2.4.59 (Debian)
System: Linux keymana 4.19.0-21-cloud-amd64 #1 SMP Debian 4.19.249-2 (2022-06-30) x86_64
User: lijunjie (1003)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //home/lijunjie/swoole-cli/swoole-src-4.8.13/tools/constant-generator.php
#!/usr/bin/env php
<?php
require __DIR__ . '/bootstrap.php';

$constant_php = LIBRARY_SRC_DIR . '/core/Constant.php';
if (!file_exists($constant_php)) {
    swoole_error("Unable to find source file [{$constant_php}]");
}

$root_dir = ROOT_DIR;
$file_list = explode("\n", `cd {$root_dir} && git ls-files`);
$file_list = array_filter($file_list, function (string $filename) {
    $ext = pathinfo($filename, PATHINFO_EXTENSION);
    return $ext === 'h' || $ext === 'c' || $ext === 'cc';
});

$source_content = '';
foreach ($file_list as $file) {
    $source_content .= file_get_contents("{$root_dir}/{$file}");
}

preg_match_all('/php_swoole_array_get_value\(.+?, "(.+?)", .+?\)/', $source_content, $matches);
$matches = array_unique($matches[1]);

$coroutineOptions = [
    'exit_condition',
    'deadlock_check_disable_trace',
    'deadlock_check_limit',
    'deadlock_check_depth'
];
$helperOptions = [
    'stats_file',
    'stats_timer_interval',
    'admin_server',
];
$options = array_merge($matches, $coroutineOptions, $helperOptions);
$result = '';
foreach ($options as $option) {
    $result .= space(4) . sprintf("public const OPTION_%s = '%s';\n\n", strtoupper($option), $option);
}

$event_file = [
    "{$root_dir}/ext-src/swoole_server.cc",
    "{$root_dir}/ext-src/swoole_server_port.cc"
];

$server_event_content = '';
foreach ($event_file as $file) {
    $server_event_content .= file_get_contents($file);
}
preg_match_all('/vent\(SW_SERVER_CB_on(.+?),/', $server_event_content, $server_event);
$server_events = array_unique($server_event[1]);

$event_result = '';
foreach ($server_events as $event) {
    if ($event === 'HandShake') {
        $event = 'handshake';
    }
    $event_result .= space(4) . sprintf("public const EVENT_%s = '%s';\n\n", strtoupper(unCamelize($event)), lcfirst($event));
}

$constant_php_content = file_get_contents($constant_php);

$event_pattern = '/(\/\* \{\{\{ EVENT \*\/\n)([\s\S]*)(\/\* \}\}\} EVENT \*\/)/';
$option_pattern = '/(\/\* \{\{\{ OPTION \*\/\n)([\s\S]*)(\/\* \}\}\} OPTION \*\/)/';

function replaceConstantContent($pattern, $result, &$content) {
    $content = preg_replace(
        $pattern,
        '${1}' . $result . space(4) . '${3}',
        $content,
        1,
        $replaced
    );

    return $replaced;
}

$event_replaced = replaceConstantContent($event_pattern, $event_result, $constant_php_content);
$option_replaced = replaceConstantContent($option_pattern, $result, $constant_php_content);

if (!$event_replaced || !$option_replaced || !file_put_contents($constant_php, $constant_php_content)) {
    swoole_error('Update constant failed');
}

swoole_success('Constant generator successfully done!');