|
| 1 | +/* |
| 2 | + * Copyright (c) 2021-present Fabien Potencier <fabien@symfony.com> |
| 3 | + * |
| 4 | + * This file is part of Symfony CLI project |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License as |
| 8 | + * published by the Free Software Foundation, either version 3 of the |
| 9 | + * License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Affero General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +package php |
| 21 | + |
| 22 | +import ( |
| 23 | + "fmt" |
| 24 | + "github.com/pkg/errors" |
| 25 | + "github.com/rs/zerolog" |
| 26 | + "github.com/symfony-cli/symfony-cli/util" |
| 27 | + "io" |
| 28 | + "net/http" |
| 29 | + "os" |
| 30 | + "path/filepath" |
| 31 | + "strings" |
| 32 | +) |
| 33 | + |
| 34 | +type PieResult struct { |
| 35 | + code int |
| 36 | + error error |
| 37 | +} |
| 38 | + |
| 39 | +func (p PieResult) Error() string { |
| 40 | + if p.error != nil { |
| 41 | + return p.error.Error() |
| 42 | + } |
| 43 | + |
| 44 | + return "" |
| 45 | +} |
| 46 | + |
| 47 | +func (p PieResult) ExitCode() int { |
| 48 | + return p.code |
| 49 | +} |
| 50 | + |
| 51 | +func Pie(dir string, args, env []string, stdout, stderr, logger io.Writer, debugLogger zerolog.Logger) PieResult { |
| 52 | + e := &Executor{ |
| 53 | + Dir: dir, |
| 54 | + BinName: "php", |
| 55 | + Stdout: stdout, |
| 56 | + Stderr: stderr, |
| 57 | + SkipNbArgs: -1, |
| 58 | + ExtraEnv: env, |
| 59 | + Logger: debugLogger, |
| 60 | + } |
| 61 | + |
| 62 | + if piePath := os.Getenv("SYMFONY_PIE_PATH"); piePath != "" { |
| 63 | + debugLogger.Debug().Str("SYMFONY_PIE_PATH", piePath).Msg("SYMFONY_PIE_PATH has been defined. User is taking control over PIE detection and execution.") |
| 64 | + e.Args = append([]string{piePath}, args...) |
| 65 | + } else if path, err := e.findPie(); err == nil && assertIsPHPScript(path) { |
| 66 | + e.Args = append([]string{"php", path}, args...) |
| 67 | + } else { |
| 68 | + reason := "No PIE installation found." |
| 69 | + if path != "" { |
| 70 | + reason = fmt.Sprintf("Detected PIE file (%s) is not a valid PHAR or PHP script.", path) |
| 71 | + } |
| 72 | + fmt.Fprintln(logger, " WARNING:", reason) |
| 73 | + fmt.Fprintln(logger, " Downloading PIE for you, but it is recommended to install PIE yourself, instructions available at https://github.com/php/pie") |
| 74 | + // we don't store it under bin/ to avoid it being found by findPie as we want to only use it as a fallback |
| 75 | + binDir := filepath.Join(util.GetHomeDir(), "pie") |
| 76 | + if path, err = downloadPie(binDir); err != nil { |
| 77 | + return PieResult{ |
| 78 | + code: 1, |
| 79 | + error: errors.Wrap(err, "unable to find pie, get it at https://github.com/php/pie"), |
| 80 | + } |
| 81 | + } |
| 82 | + e.Args = append([]string{"php", path}, args...) |
| 83 | + fmt.Fprintf(logger, " (running %s)\n\n", e.CommandLine()) |
| 84 | + } |
| 85 | + |
| 86 | + ret := e.Execute(false) |
| 87 | + if ret != 0 { |
| 88 | + return PieResult{ |
| 89 | + code: ret, |
| 90 | + error: errors.Errorf("unable to run %s", e.CommandLine()), |
| 91 | + } |
| 92 | + } |
| 93 | + return PieResult{} |
| 94 | +} |
| 95 | + |
| 96 | +func findPie(logger zerolog.Logger) (string, error) { |
| 97 | + for _, file := range []string{"pie", "pie.phar"} { |
| 98 | + logger.Debug().Str("source", "PIE").Msgf(`Looking for PIE in the PATH as "%s"`, file) |
| 99 | + if pharPath, _ := LookPath(file); pharPath != "" { |
| 100 | + // On Windows, we don't want the .bat, but the real pie phar/PHP file |
| 101 | + if strings.HasSuffix(pharPath, ".bat") { |
| 102 | + pharPath = pharPath[:len(pharPath)-4] + ".phar" |
| 103 | + } |
| 104 | + logger.Debug().Str("source", "PIE").Msgf(`Found potential PIE as "%s"`, pharPath) |
| 105 | + return pharPath, nil |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return "", os.ErrNotExist |
| 110 | +} |
| 111 | + |
| 112 | +func downloadPie(dir string) (string, error) { |
| 113 | + if err := os.MkdirAll(dir, 0755); err != nil { |
| 114 | + return "", err |
| 115 | + } |
| 116 | + path := filepath.Join(dir, "pie.phar") |
| 117 | + if _, err := os.Stat(path); err == nil { |
| 118 | + return path, nil |
| 119 | + } |
| 120 | + |
| 121 | + piePhar, err := downloadPiePhar() |
| 122 | + if err != nil { |
| 123 | + return "", err |
| 124 | + } |
| 125 | + |
| 126 | + err = os.WriteFile(path, piePhar, 0755) |
| 127 | + if err != nil { |
| 128 | + return "", err |
| 129 | + } |
| 130 | + |
| 131 | + return path, nil |
| 132 | +} |
| 133 | + |
| 134 | +func downloadPiePhar() ([]byte, error) { |
| 135 | + resp, err := http.Get("https://github.com/php/pie/releases/latest/download/pie.phar") |
| 136 | + if err != nil { |
| 137 | + return nil, err |
| 138 | + } |
| 139 | + defer resp.Body.Close() |
| 140 | + return io.ReadAll(resp.Body) |
| 141 | +} |
0 commit comments