-1

当你尝试在 git 中进行提交时没有任何消息,它会在 shell 中为你打开编辑器,当你关闭编辑器时,git 会读取内容。

我想做完全一样的,但是在 php.ini 中。

正如我在 git 源代码(https://github.com/git/git/blob/master/editor.cuse_shell )中看到的那样,它只是在标志设置为 true 的情况下启动进程。我怎样才能在 PHP 中实现这一点?另外,我看到 git 将 SIGINT 和 SIGQUIT 信号设置为忽略。如何在 PHP 中完成?

4

1 回答 1

1

shell_exec()将在 shell 中生成一个进程:http: //php.net/shell_exec

要处理您需要使用pcntl_signal()的信号,这需要 pcntl 扩展:http ://php.net/manual/en/function.pcntl-signal.php

一个完整的例子,打开一个文件进行编辑,然后显示编辑的内容:

$fh = fopen('tmp_file', 'w');
fwrite($fh, "Hello!");
fclose($fh);

shell_exec("nano tmp_file > /dev/tty < /dev/tty");

$data = file_get_contents('tmp_file');
unlink('tmp_file');
echo "You entered: $data\n";

您需要将输入和输出重定向到/dev/tty以使流程具有交互性(否则它将挂起,具体取决于编辑器)。

于 2013-03-22T18:06:20.780 回答