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\Exception\BreakException;
15
 
16
/**
17
 * An array-based Readline emulation implementation.
18
 */
19
class Transient implements Readline
20
{
21
    private $history;
22
    private $historySize;
23
    private $eraseDups;
24
    private $stdin;
25
 
26
    /**
27
     * Transient Readline is always supported.
28
     *
29
     * {@inheritdoc}
30
     */
31
    public static function isSupported(): bool
32
    {
33
        return true;
34
    }
35
 
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public static function supportsBracketedPaste(): bool
40
    {
41
        return false;
42
    }
43
 
44
    /**
45
     * Transient Readline constructor.
46
     */
47
    public function __construct($historyFile = null, $historySize = 0, $eraseDups = false)
48
    {
49
        // don't do anything with the history file...
50
        $this->history = [];
51
        $this->historySize = $historySize;
52
        $this->eraseDups = $eraseDups;
53
    }
54
 
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function addHistory(string $line): bool
59
    {
60
        if ($this->eraseDups) {
61
            if (($key = \array_search($line, $this->history)) !== false) {
62
                unset($this->history[$key]);
63
            }
64
        }
65
 
66
        $this->history[] = $line;
67
 
68
        if ($this->historySize > 0) {
69
            $histsize = \count($this->history);
70
            if ($histsize > $this->historySize) {
71
                $this->history = \array_slice($this->history, $histsize - $this->historySize);
72
            }
73
        }
74
 
75
        $this->history = \array_values($this->history);
76
 
77
        return true;
78
    }
79
 
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function clearHistory(): bool
84
    {
85
        $this->history = [];
86
 
87
        return true;
88
    }
89
 
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function listHistory(): array
94
    {
95
        return $this->history;
96
    }
97
 
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function readHistory(): bool
102
    {
103
        return true;
104
    }
105
 
106
    /**
107
     * {@inheritdoc}
108
     *
109
     * @throws BreakException if user hits Ctrl+D
110
     *
111
     * @return false|string
112
     */
113
    public function readline(string $prompt = null)
114
    {
115
        echo $prompt;
116
 
117
        return \rtrim(\fgets($this->getStdin()), "\n\r");
118
    }
119
 
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function redisplay()
124
    {
125
        // noop
126
    }
127
 
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function writeHistory(): bool
132
    {
133
        return true;
134
    }
135
 
136
    /**
137
     * Get a STDIN file handle.
138
     *
139
     * @throws BreakException if user hits Ctrl+D
140
     *
141
     * @return resource
142
     */
143
    private function getStdin()
144
    {
145
        if (!isset($this->stdin)) {
146
            $this->stdin = \fopen('php://stdin', 'r');
147
        }
148
 
149
        if (\feof($this->stdin)) {
150
            throw new BreakException('Ctrl+D');
151
        }
152
 
153
        return $this->stdin;
154
    }
155
}