Skip to content

Commit 3f0b204

Browse files
spkiluuu1994
authored andcommitted
cli: allow to change ~/.php_history with PHP_HISTFILE
Closes GH-13313
1 parent 7151855 commit 3f0b204

File tree

3 files changed

+51
-16
lines changed

3 files changed

+51
-16
lines changed

UPGRADING

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ PHP 8.4 UPGRADE NOTES
246246
. Added constant POSIX_SC_CHILD_MAX
247247
. Added constant POSIX_SC_CLK_TCK
248248

249+
- Readfile:
250+
. Added ability to change .php_history path through PHP_HISTFILE env variable.
251+
249252
- Reflection:
250253
. ReflectionAttribute now contains a $name property to improve the debugging
251254
experience.

ext/readline/readline_cli.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,12 @@ static int readline_shell_run(void) /* {{{ */
617617
}
618618

619619
#ifndef PHP_WIN32
620-
history_file = tilde_expand("~/.php_history");
620+
const char *histfile_env_name = "PHP_HISTFILE";
621+
if (getenv(histfile_env_name)) {
622+
spprintf(&history_file, MAXPATHLEN, "%s", getenv(histfile_env_name));
623+
} else {
624+
spprintf(&history_file, MAXPATHLEN, "%s/.php_history", getenv("HOME"));
625+
}
621626
#else
622627
spprintf(&history_file, MAX_PATH, "%s/.php_history", getenv("USERPROFILE"));
623628
#endif
@@ -717,11 +722,7 @@ static int readline_shell_run(void) /* {{{ */
717722

718723
php_last_char = '\0';
719724
}
720-
#ifdef PHP_WIN32
721725
efree(history_file);
722-
#else
723-
free(history_file);
724-
#endif
725726
efree(code);
726727
zend_string_release_ex(prompt, 0);
727728
return EG(exit_status);

sapi/cli/tests/017.phpt

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
CLI -a and libedit
33
--EXTENSIONS--
44
readline
5+
--ENV--
6+
PHP_HISTFILE=
57
--SKIPIF--
68
<?php
79
include "skipif.inc";
@@ -11,9 +13,18 @@ if (readline_info('done') !== NULL) {
1113
?>
1214
--FILE--
1315
<?php
14-
$php = getenv('TEST_PHP_EXECUTABLE_ESCAPED');
15-
$ini = getenv('TEST_PHP_EXTRA_ARGS');
16-
$descriptorspec = [['pipe', 'r'], STDOUT, STDERR];
16+
function runReplCodes($codes) {
17+
$php = getenv('TEST_PHP_EXECUTABLE_ESCAPED');
18+
$ini = getenv('TEST_PHP_EXTRA_ARGS');
19+
$descriptorspec = [['pipe', 'r'], STDOUT, STDERR];
20+
foreach ($codes as $key => $code) {
21+
echo "\n--------------\nSnippet no. $key:\n--------------\n";
22+
$proc = proc_open("$php $ini -a", $descriptorspec, $pipes);
23+
fwrite($pipes[0], $code);
24+
fclose($pipes[0]);
25+
proc_close($proc);
26+
}
27+
}
1728

1829
$codes = array();
1930

@@ -52,17 +63,26 @@ function a_function_with_some_name() {
5263
a_function_w );
5364
EOT;
5465

55-
foreach ($codes as $key => $code) {
56-
echo "\n--------------\nSnippet no. $key:\n--------------\n";
57-
$proc = proc_open("$php $ini -a", $descriptorspec, $pipes);
58-
fwrite($pipes[0], $code);
59-
fclose($pipes[0]);
60-
proc_close($proc);
61-
}
62-
66+
runReplCodes($codes);
6367
echo "\nDone\n";
68+
6469
$dir = PHP_OS_FAMILY == 'Windows' ? getenv("USERPROFILE") : getenv("HOME");
6570
var_dump(file_exists($dir . '/.php_history'));
71+
72+
$php_history_tmp = sprintf('%s%s%s', sys_get_temp_dir(), DIRECTORY_SEPARATOR, 'php_history');
73+
putenv('PHP_HISTFILE=' . $php_history_tmp);
74+
var_dump(file_exists($php_history_tmp));
75+
76+
$last[6] = <<<EOT
77+
echo 'Hello World';
78+
exit
79+
EOT;
80+
runReplCodes($last);
81+
echo "\nDone\n";
82+
83+
$php_history_path = PHP_OS_FAMILY == 'Windows' ? getenv("USERPROFILE") : $php_history_tmp;
84+
var_dump(file_exists($php_history_path));
85+
@unlink($php_history_tmp);
6686
?>
6787
--EXPECT--
6888
--------------
@@ -108,3 +128,14 @@ Parse error: Unmatched ')' in php shell code on line 1
108128

109129
Done
110130
bool(true)
131+
bool(false)
132+
133+
--------------
134+
Snippet no. 6:
135+
--------------
136+
Interactive shell
137+
138+
Hello World
139+
140+
Done
141+
bool(true)

0 commit comments

Comments
 (0)