Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | 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
 *
6
 * (c) 2012-2022 Justin Hileman
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
     * @return bool
34
     */
35
    public static function isSupported(): bool
36
    {
37
        return \function_exists('readline') && !\function_exists('readline_list_history');
38
    }
39
 
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public static function supportsBracketedPaste(): bool
44
    {
45
        return false;
46
    }
47
 
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function listHistory(): array
52
    {
53
        $history = \file_get_contents($this->historyFile);
54
        if (!$history) {
55
            return [];
56
        }
57
 
58
        // libedit doesn't seem to support non-unix line separators.
59
        $history = \explode("\n", $history);
60
 
61
        // remove history signature if it exists
62
        if ($history[0] === '_HiStOrY_V2_') {
63
            \array_shift($history);
64
        }
65
 
66
        // decode the line
67
        $history = \array_map([$this, 'parseHistoryLine'], $history);
68
        // filter empty lines & comments
69
        return \array_values(\array_filter($history));
70
    }
71
 
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function writeHistory(): bool
76
    {
77
        $res = parent::writeHistory();
78
 
79
        // Libedit apparently refuses to save history if the history file is not
80
        // owned by the user, even if it is writable. Warn when this happens.
81
        //
82
        // See https://github.com/bobthecow/psysh/issues/552
83
        if ($res === false && !$this->hasWarnedOwnership) {
84
            if (\is_file($this->historyFile) && \is_writable($this->historyFile)) {
85
                $this->hasWarnedOwnership = true;
86
                $msg = \sprintf('Error writing history file, check file ownership: %s', $this->historyFile);
87
                \trigger_error($msg, \E_USER_NOTICE);
88
            }
89
        }
90
 
91
        return $res;
92
    }
93
 
94
    /**
95
     * From GNUReadline (readline/histfile.c & readline/histexpand.c):
96
     * lines starting with "\0" are comments or timestamps;
97
     * if "\0" is found in an entry,
98
     * everything from it until the next line is a comment.
99
     *
100
     * @param string $line The history line to parse
101
     *
102
     * @return string|null
103
     */
104
    protected function parseHistoryLine(string $line)
105
    {
106
        // empty line, comment or timestamp
107
        if (!$line || $line[0] === "\0") {
108
            return;
109
        }
110
        // if "\0" is found in an entry, then
111
        // everything from it until the end of line is a comment.
112
        if (($pos = \strpos($line, "\0")) !== false) {
113
            $line = \substr($line, 0, $pos);
114
        }
115
 
116
        return ($line !== '') ? Str::unvis($line) : null;
117
    }
118
}