golang
部分主要是用來啟動(dòng)和結(jié)束 webman
進(jìn)程的,其他所有業(yè)務(wù)流程和前端部分都是由 webman
響應(yīng)的。
有興趣可以下載安裝體驗(yàn)一下,只支持 Windows
,安裝包大?。?code>12.7MB。
下載地址 密碼:webman
。
網(wǎng)盤里面有一個(gè) Windows
系統(tǒng)版本的 micro.sfx
文件,只打包了小部分?jǐn)U展,沒有帶 MySQL
,只有sqlite3
。
// app.go
// App struct
type App struct {
ctx context.Context
phpCmd *exec.Cmd
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
a.startPHPProcess()
go a.monitorPHPProcess()
}
func (a *App) startPHPProcess() {
exePath, _ := os.Executable()
installDir := filepath.Dir(exePath)
phpWebmanExePath := filepath.Join(installDir, "myapp.exe")
fmt.Println(phpWebmanExePath)
cmd := exec.Command(phpWebmanExePath)
cmd.SysProcAttr = &syscall.SysProcAttr{
HideWindow: true,
CreationFlags: 0x08000000, // CREATE_NO_WINDOW
}
err := cmd.Start()
if err != nil {
// 使用 Wails 彈窗提示用戶
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Error",
Message: "Failed to start PHP process. Please restart the application.",
})
return
}
time.Sleep(2 * time.Second)
a.phpCmd = cmd
}
func (a *App) shutdown(ctx context.Context) {
fmt.Println(a.phpCmd)
if a.phpCmd != nil && a.phpCmd.Process != nil {
a.phpCmd.Process.Kill()
}
}
func (a *App) monitorPHPProcess() {
if a.phpCmd == nil {
return
}
for {
// 等待進(jìn)程完成
err := a.phpCmd.Wait()
if err != nil {
// 進(jìn)程已退出
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.WarningDialog,
Title: "PHP Process Stopped",
Message: "The PHP process has stopped. Please restart the application.",
})
break
}
// 每隔10秒檢查一次
time.Sleep(10 * time.Second)
}
}
理論上如果用 libphp(embed)+go+wails 可以實(shí)現(xiàn)不依賴 micro.sfx 來編寫桌面程序,就像 frankenPHP 一樣,且可以實(shí)現(xiàn)可替換的代碼。有人有興趣可以研究一下。