Running a shell command
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- C
- Star
- 40.4k
- Fork
- 8.1k
- Merge trung bình
- 2 ngày 13 giờ
- Pull request đã merge (30 ngày)
- 96
Mô tả
Description
There's a handful of ways to run a command in PHP - I have to look this up every single time, and every time, I end up reaching the conclusion that none of them are exactly good.
The most common, most basic requirement is you just want to run a command, capture the output, and get the exit status code - it appears there is still no simple way to achieve this in PHP.
-
shell_execcan't reliably detect failures, doesn't return the status code, fails for binary output on Windows 👎 -
systemonly returns the last line of the command output. 👎 -
pcntl_execis not available on Windows 👎 -
popenrequires some hand holding and leads to bugs 👎 -
proc_openrequires even more hand holding and is difficult just to get it working at all 👎 -
passthrurequires an output buffer, which may be slightly less error prone than other options, but still 👎 -
execopens a command window (on Windows only?) 👎
Finally, there's the backtick operator, which, having dedicated syntax, seems like the most obvious option - but turns out to be an alias for shell_exec, being one of the worst options overall.
Of all these options, exec probably comes closest to fulfilling the most basic requirements - yet still falls short by opening a command window on Windows; behavior that is (I'm sure, for some historical reason) inconsistent with that of Linux.
Rather than suggesting we add yet another function to run a command, how about fixing this issue with exec?
Yes, that is technically a breaking change, but do we imagine anyone is writing Windows-specific PHP programs that rely on opening command windows, expecting interaction from the user? This seems highly unlikely.
I think it would be really great if this situation could finally be remedied, so that just running a simple command is simple and "feature-complete" with no cross-platform quirks - this should not be something that requires any mental gymnastics, and it should be extremely obvious and easy to find in the manual, e.g. with an opening note on every other manual page for all these other functions.
I'm not going to suggest deprecating any of these functions, as they are most likely all being used for something.
But wouldn't it be great if there was one clear choice going forward that "just works" for the 98% use case? 🙂
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Xem xét hành vi và tài liệu của exec cùng với shell_exec, system, pcntl_exec, popen, proc_open, passthru và backticks, đặc biệt chú ý đến hành vi của cửa sổ lệnh Windows được mô tả ở đây. So sánh các yêu cầu hiện có về đầu ra và trạng thái thoát trên Windows và Linux; công việc được coi là hoàn tất khi hành vi exec được đề xuất và tác động của nó đối với khả năng tương thích đã được giải quyết rõ ràng.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- php
- Lĩnh vực
- cli
- Loại issue
- Tính năng
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 25/100