MCPcopy Create free account
hub / github.com/1Panel-dev/1Panel / ChangePHPVersion

Method ChangePHPVersion

agent/app/service/website.go:1278–1359  ·  view source on GitHub ↗
(req request.WebsitePHPVersionReq)

Source from the content-addressed store, hash-verified

1276}
1277
1278func (w WebsiteService) ChangePHPVersion(req request.WebsitePHPVersionReq) error {
1279 website, err := websiteRepo.GetFirst(repo.WithByID(req.WebsiteID))
1280 if err != nil {
1281 return err
1282 }
1283 if website.Type == constant.Runtime {
1284 oldRuntime, err := runtimeRepo.GetFirst(context.Background(), repo.WithByID(website.RuntimeID))
1285 if err != nil {
1286 return err
1287 }
1288 if oldRuntime.Resource == constant.ResourceLocal {
1289 return buserr.New("ErrPHPResource")
1290 }
1291 client, err := docker.NewDockerClient()
1292 if err != nil {
1293 return err
1294 }
1295 defer client.Close()
1296 if !checkImageLike(client, oldRuntime.Image) {
1297 return buserr.WithName("ErrImageNotExist", oldRuntime.Name)
1298 }
1299 }
1300 configPath := GetSitePath(website, SiteConf)
1301 nginxContent, err := files.NewFileOp().GetContent(configPath)
1302 if err != nil {
1303 return err
1304 }
1305 config, err := parser.NewStringParser(string(nginxContent)).Parse()
1306 if err != nil {
1307 return err
1308 }
1309 servers := config.FindServers()
1310 if len(servers) == 0 {
1311 return errors.New("nginx config is not valid")
1312 }
1313 server := servers[0]
1314 fileOp := files.NewFileOp()
1315 indexPHPPath := path.Join(GetSitePath(website, SiteIndexDir), "index.php")
1316 indexHtmlPath := path.Join(GetSitePath(website, SiteIndexDir), "index.html")
1317 if req.RuntimeID > 0 {
1318 server.UpdateDirective("index", []string{"index.php index.html index.htm default.php default.htm default.html"})
1319 server.RemoveDirective("location", []string{"~", "[^/]\\.php(/|$)"})
1320 runtime, err := runtimeRepo.GetFirst(context.Background(), repo.WithByID(req.RuntimeID))
1321 if err != nil {
1322 return err
1323 }
1324 if runtime.Resource == constant.ResourceLocal {
1325 return buserr.New("ErrPHPResource")
1326 }
1327 website.RuntimeID = req.RuntimeID
1328 website.AppInstallID = 0
1329 phpProxy := fmt.Sprintf("127.0.0.1:%s", runtime.Port)
1330 website.Proxy = phpProxy
1331 server.UpdatePHPProxy([]string{website.Proxy}, "")
1332 website.Type = constant.Runtime
1333 if !fileOp.Stat(indexPHPPath) {
1334 _ = fileOp.WriteFile(indexPHPPath, strings.NewReader(string(nginx_conf.IndexPHP)), constant.FilePerm)
1335 }

Callers

nothing calls this directly

Calls 15

StatMethod · 0.95
WriteFileMethod · 0.95
checkImageLikeFunction · 0.85
GetSitePathFunction · 0.85
getAppInstallByKeyFunction · 0.85
nginxCheckAndReloadFunction · 0.85
ParseMethod · 0.80
FindServersMethod · 0.80
UpdatePHPProxyMethod · 0.80
GetFirstMethod · 0.65
WithByIDMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected