1#!/usr/bin/env php
2<?php
3require __DIR__ . '/bootstrap.php';
4
5const REGX = '\[([^\]]*)\]';
6
7$server_helper_php = LIBRARY_SRC_DIR . '/core/Server/Helper.php';
8if (!file_exists($server_helper_php)) {
9    swoole_error("Unable to find source file [{$server_helper_php}]");
10}
11$php_content = file_get_contents($server_helper_php);
12
13function _replace_options($file, &$php_content, $name) {
14    $source_content = file_get_contents($file);
15    preg_match_all('/php_swoole_array_get_value\(.+?, "(.+?)", .+?\)/', $source_content, $matches);
16    $matches = array_unique($matches[1]);
17    $result = '';
18    foreach ($matches as $option) {
19        $result .= space(8) . sprintf("'%s' => true,\n", strtolower($option), $option);
20    }
21
22    $php_content = preg_replace(
23        '/const '.$name.' = '.REGX.';/',
24        'const '.$name.' = ['.PHP_EOL.$result.space(4).'];',
25        $php_content,
26        1,
27        $replaced
28    );
29    if (!$replaced) {
30        swoole_error("error content [$name]");
31    }
32}
33
34_replace_options(ROOT_DIR.'/ext-src/php_swoole.cc', $php_content, 'GLOBAL_OPTIONS');
35_replace_options(ROOT_DIR.'/ext-src/swoole_server.cc', $php_content, 'SERVER_OPTIONS');
36_replace_options(ROOT_DIR.'/ext-src/swoole_server_port.cc', $php_content, 'PORT_OPTIONS');
37_replace_options(ROOT_DIR.'/ext-src/swoole_async_coro.cc', $php_content, 'AIO_OPTIONS');
38_replace_options(ROOT_DIR.'/ext-src/swoole_coroutine_scheduler.cc', $php_content, 'COROUTINE_OPTIONS');
39
40// save
41if (!file_put_contents($server_helper_php, $php_content)) {
42    swoole_error('Update Server\\Helper failed');
43}
44
45swoole_success('Update Server\\Helper successfully done!');
46
47
48
49
50