Skip to content

Commit

Permalink
cli: allow to change ~/.php_history with PHP_HISTFILE
Browse files Browse the repository at this point in the history
Closes GH-13313
  • Loading branch information
spk authored and iluuu1994 committed Apr 22, 2024
1 parent 7151855 commit 3f0b204
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
3 changes: 3 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ PHP 8.4 UPGRADE NOTES
. Added constant POSIX_SC_CHILD_MAX
. Added constant POSIX_SC_CLK_TCK

- Readfile:
. Added ability to change .php_history path through PHP_HISTFILE env variable.

- Reflection:
. ReflectionAttribute now contains a $name property to improve the debugging
experience.
Expand Down
11 changes: 6 additions & 5 deletions ext/readline/readline_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,12 @@ static int readline_shell_run(void) /* {{{ */
}

#ifndef PHP_WIN32
history_file = tilde_expand("~/.php_history");
const char *histfile_env_name = "PHP_HISTFILE";
if (getenv(histfile_env_name)) {
spprintf(&history_file, MAXPATHLEN, "%s", getenv(histfile_env_name));
} else {
spprintf(&history_file, MAXPATHLEN, "%s/.php_history", getenv("HOME"));
}
#else
spprintf(&history_file, MAX_PATH, "%s/.php_history", getenv("USERPROFILE"));
#endif
Expand Down Expand Up @@ -717,11 +722,7 @@ static int readline_shell_run(void) /* {{{ */

php_last_char = '\0';
}
#ifdef PHP_WIN32
efree(history_file);
#else
free(history_file);
#endif
efree(code);
zend_string_release_ex(prompt, 0);
return EG(exit_status);
Expand Down
53 changes: 42 additions & 11 deletions sapi/cli/tests/017.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
CLI -a and libedit
--EXTENSIONS--
readline
--ENV--
PHP_HISTFILE=
--SKIPIF--
<?php
include "skipif.inc";
Expand All @@ -11,9 +13,18 @@ if (readline_info('done') !== NULL) {
?>
--FILE--
<?php
$php = getenv('TEST_PHP_EXECUTABLE_ESCAPED');
$ini = getenv('TEST_PHP_EXTRA_ARGS');
$descriptorspec = [['pipe', 'r'], STDOUT, STDERR];
function runReplCodes($codes) {
$php = getenv('TEST_PHP_EXECUTABLE_ESCAPED');
$ini = getenv('TEST_PHP_EXTRA_ARGS');
$descriptorspec = [['pipe', 'r'], STDOUT, STDERR];
foreach ($codes as $key => $code) {
echo "\n--------------\nSnippet no. $key:\n--------------\n";
$proc = proc_open("$php $ini -a", $descriptorspec, $pipes);
fwrite($pipes[0], $code);
fclose($pipes[0]);
proc_close($proc);
}
}

$codes = array();

Expand Down Expand Up @@ -52,17 +63,26 @@ function a_function_with_some_name() {
a_function_w );
EOT;

foreach ($codes as $key => $code) {
echo "\n--------------\nSnippet no. $key:\n--------------\n";
$proc = proc_open("$php $ini -a", $descriptorspec, $pipes);
fwrite($pipes[0], $code);
fclose($pipes[0]);
proc_close($proc);
}

runReplCodes($codes);
echo "\nDone\n";

$dir = PHP_OS_FAMILY == 'Windows' ? getenv("USERPROFILE") : getenv("HOME");
var_dump(file_exists($dir . '/.php_history'));

$php_history_tmp = sprintf('%s%s%s', sys_get_temp_dir(), DIRECTORY_SEPARATOR, 'php_history');
putenv('PHP_HISTFILE=' . $php_history_tmp);
var_dump(file_exists($php_history_tmp));

$last[6] = <<<EOT
echo 'Hello World';
exit
EOT;
runReplCodes($last);
echo "\nDone\n";

$php_history_path = PHP_OS_FAMILY == 'Windows' ? getenv("USERPROFILE") : $php_history_tmp;
var_dump(file_exists($php_history_path));
@unlink($php_history_tmp);
?>
--EXPECT--
--------------
Expand Down Expand Up @@ -108,3 +128,14 @@ Parse error: Unmatched ')' in php shell code on line 1

Done
bool(true)
bool(false)

--------------
Snippet no. 6:
--------------
Interactive shell

Hello World

Done
bool(true)

0 comments on commit 3f0b204

Please sign in to comment.