Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of Psy Shell.
5
 *
200 lars 6
 * (c) 2012-2023 Justin Hileman
148 lars 7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Psy\Readline;
13
 
14
use Psy\Util\Str;
15
 
16
/**
17
 * A Libedit-based Readline implementation.
18
 *
19
 * This is largely the same as the Readline implementation, but it emulates
20
 * support for `readline_list_history` since PHP decided it was a good idea to
21
 * ship a fake Readline implementation that is missing history support.
22
 *
23
 * NOTE: As of PHP 7.4, PHP sometimes has history support in the Libedit
24
 * wrapper, so it will use the GNUReadline implementation rather than this one.
25
 */
26
class Libedit extends GNUReadline
27
{
28
    private $hasWarnedOwnership = false;
29
 
30
    /**
31
     * Let's emulate GNU Readline by manually reading and parsing the history file!
32
     */
33
    public static function isSupported(): bool
34
    {
35
        return \function_exists('readline') && !\function_exists('readline_list_history');
36
    }
37
 
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public static function supportsBracketedPaste(): bool
42
    {
43
        return false;
44
    }
45
 
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function listHistory(): array
50
    {
51
        $history = \file_get_contents($this->historyFile);
52
        if (!$history) {
53
            return [];
54
        }
55
 
56
        // libedit doesn't seem to support non-unix line separators.
57
        $history = \explode("\n", $history);
58
 
59
        // remove history signature if it exists
60
        if ($history[0] === '_HiStOrY_V2_') {
61
            \array_shift($history);
62
        }
63
 
64
        // decode the line
65
        $history = \array_map([$this, 'parseHistoryLine'], $history);
66
        // filter empty lines & comments
67
        return \array_values(\array_filter($history));
68
    }
69
 
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function writeHistory(): bool
74
    {
75
        $res = parent::writeHistory();
76
 
77
        // Libedit apparently refuses to save history if the history file is not
78
        // owned by the user, even if it is writable. Warn when this happens.
79
        //
80
        // See https://github.com/bobthecow/psysh/issues/552
81
        if ($res === false && !$this->hasWarnedOwnership) {
82
            if (\is_file($this->historyFile) && \is_writable($this->historyFile)) {
83
                $this->hasWarnedOwnership = true;
84
                $msg = \sprintf('Error writing history file, check file ownership: %s', $this->historyFile);
85
                \trigger_error($msg, \E_USER_NOTICE);
86
            }
87
        }
88
 
89
        return $res;
90
    }
91
 
92
    /**
93
     * From GNUReadline (readline/histfile.c & readline/histexpand.c):
94
     * lines starting with "\0" are comments or timestamps;
95
     * if "\0" is found in an entry,
96
     * everything from it until the next line is a comment.
97
     *
98
     * @param string $line The history line to parse
99
     *
100
     * @return string|null
101
     */
102
    protected function parseHistoryLine(string $line)
103
    {
104
        // empty line, comment or timestamp
105
        if (!$line || $line[0] === "\0") {
106
            return;
107
        }
108
        // if "\0" is found in an entry, then
109
        // everything from it until the end of line is a comment.
110
        if (($pos = \strpos($line, "\0")) !== false) {
111
            $line = \substr($line, 0, $pos);
112
        }
113
 
114
        return ($line !== '') ? Str::unvis($line) : null;
115
    }
116
}