check_for_updates - kouji6309/SingleMVC GitHub Wiki
檢查 SingleMVC 更新,使用 VERSION 比對線上版本。SingleMVC::check_for_updates 的別名。
開啟 auto_update 將會在有新版時,自動下載並覆蓋框架 __FRAMEWORK__ 的檔案。
check_for_updates(bool $details = false) : mixed
- 指定是否傳回細節。
- 布林:表示是否有新版。
- 陣列:包含結果、線上版本、目前版本與原始碼。
檢查版本
if (check_for_updates()) {
output('text', '有新版框架');
}
檢查版本並顯示細節
$data = check_for_updates(true);
if ($data['result'] == 0) {
output('text', '版本相同');
} else if ($data['result'] == -1) {
output('text', '版本較舊');
} else if ($data['result'] == 1) {
output('text', '版本較新');
}
output('text', ' 線上版本 '.$data['online'].',目前版本 '.$data['current']);
更新框架,搭配 __FRAMEWORK__ 以更新框架檔案
public function update() {
if (($r = check_for_updates(true)) && $r['result'] < 0) {
if (file_put_contents(__FRAMEWORK__, $r['file'])) {
output('text', 'Framework has been updated.');
} else {
output('text', 'Unable to update framework.');
}
} else {
output('text', 'Framework is up-to-date.');
}
}
函數內部使用 file_get_contents 取得最新原始碼,若未開啟 auto_update,將會每次請求時下載原始碼;反之依照 PHP.ini
或預設的設定,原始碼可能會有 180 秒、更多或更少的快取時間,因此不用擔心效能問題。