1 回答

TA貢獻(xiàn)1872條經(jīng)驗(yàn) 獲得超4個(gè)贊
最終,我處理了 RCON 命令的輸出。缺點(diǎn)是它特別靜態(tài)。我必須圍繞諸如使用 SQL 語(yǔ)句選擇的列并將結(jié)果調(diào)整為適當(dāng)?shù)念愋偷葍?nèi)容進(jìn)行構(gòu)建。
我正在使用https://github.com/gorcon/rcon-cli并圍繞它包裝了一個(gè)查詢類:
class RconDatabase
{
const RCON_EXECUTABLE = __DIR__ . '/../bin/rcon';
public function __construct()
{
}
public function query($sql)
{
if (!is_executable(self::RCON_EXECUTABLE)) throw new FilePermissionException('Unable to execute RCON');
$cmd = self::RCON_EXECUTABLE.' -a '.Config::get('rcon.host').':'.Config::get('rcon.port').' -p '.Config::get('rcon.password').' -c "sql '. $sql .'"';
$output = shell_exec($cmd);
if ($output == null) throw new RconConnectionException('No response from RCON server');
if (strpos($output, 'authentication failed') !== false) throw new RconAuthenticationException();
if (strpos($output, 'dial tcp') !== false) throw new RconNetworkException();
$lines = preg_split("/((\r?\n)|(\r\n?))/", $output);
$results = array();
$last_column = 0;
for ($i = 0; $i < count($lines); $i++) {
if (empty($lines[$i])) continue;
$columns = str_getcsv($lines[$i], '|');
for ($x = 0; $x < count($columns); $x++) {
if ($i == 0 && empty($columns[$x])) {
$last_column = $x;
continue;
}
if ($x == 0 && preg_match('/^#[0-9]+\s+(\S+)/', $columns[0], $match))
$columns[$x] = $match[1];
$columns[$x] = trim($columns[$x]);
}
if ($i == 0) continue;
if ($last_column > 0) unset($columns[$last_column]);
array_push($results, $columns);
}
return $results;
}
}
- 1 回答
- 0 關(guān)注
- 107 瀏覽
添加回答
舉報(bào)