859b4228d3b1d9c9d1ae0ff4ec065e7488e74578
[yaffs-website] / vendor / psy / psysh / src / Psy / Readline / GNUReadline.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2017 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 /**
15  * A Readline interface implementation for GNU Readline.
16  *
17  * This is by far the coolest way to do it, but it doesn't work with new PHP.
18  *
19  * Oh well.
20  */
21 class GNUReadline implements Readline
22 {
23     /** @var string|false */
24     protected $historyFile;
25     /** @var int */
26     protected $historySize;
27     /** @var bool */
28     protected $eraseDups;
29
30     /**
31      * GNU Readline is supported iff `readline_list_history` is defined. PHP
32      * decided it would be awesome to swap out GNU Readline for Libedit, but
33      * they ended up shipping an incomplete implementation. So we've got this.
34      *
35      * @return bool
36      */
37     public static function isSupported()
38     {
39         return function_exists('readline_list_history');
40     }
41
42     /**
43      * GNU Readline constructor.
44      *
45      * @param string|false $historyFile
46      * @param int          $historySize
47      * @param bool         $eraseDups
48      */
49     public function __construct($historyFile = null, $historySize = 0, $eraseDups = false)
50     {
51         $this->historyFile = ($historyFile !== null) ? $historyFile : false;
52         $this->historySize = $historySize;
53         $this->eraseDups   = $eraseDups;
54     }
55
56     /**
57      * {@inheritdoc}
58      */
59     public function addHistory($line)
60     {
61         if ($res = readline_add_history($line)) {
62             $this->writeHistory();
63         }
64
65         return $res;
66     }
67
68     /**
69      * {@inheritdoc}
70      */
71     public function clearHistory()
72     {
73         if ($res = readline_clear_history()) {
74             $this->writeHistory();
75         }
76
77         return $res;
78     }
79
80     /**
81      * {@inheritdoc}
82      */
83     public function listHistory()
84     {
85         return readline_list_history();
86     }
87
88     /**
89      * {@inheritdoc}
90      */
91     public function readHistory()
92     {
93         // Workaround PHP bug #69054
94         //
95         // If open_basedir is set, readline_read_history() segfaults. This was fixed in 5.6.7:
96         //
97         //     https://github.com/php/php-src/blob/423a057023ef3c00d2ffc16a6b43ba01d0f71796/NEWS#L19-L21
98         //
99         if (version_compare(PHP_VERSION, '5.6.7', '>=') || !ini_get('open_basedir')) {
100             readline_read_history();
101         }
102         readline_clear_history();
103
104         return readline_read_history($this->historyFile);
105     }
106
107     /**
108      * {@inheritdoc}
109      */
110     public function readline($prompt = null)
111     {
112         return readline($prompt);
113     }
114
115     /**
116      * {@inheritdoc}
117      */
118     public function redisplay()
119     {
120         readline_redisplay();
121     }
122
123     /**
124      * {@inheritdoc}
125      */
126     public function writeHistory()
127     {
128         // We have to write history first, since it is used
129         // by Libedit to list history
130         if ($this->historyFile !== false) {
131             $res = readline_write_history($this->historyFile);
132         } else {
133             $res = true;
134         }
135
136         if (!$res || !$this->eraseDups && !$this->historySize > 0) {
137             return $res;
138         }
139
140         $hist = $this->listHistory();
141         if (!$hist) {
142             return true;
143         }
144
145         if ($this->eraseDups) {
146             // flip-flip technique: removes duplicates, latest entries win.
147             $hist = array_flip(array_flip($hist));
148             // sort on keys to get the order back
149             ksort($hist);
150         }
151
152         if ($this->historySize > 0) {
153             $histsize = count($hist);
154             if ($histsize > $this->historySize) {
155                 $hist = array_slice($hist, $histsize - $this->historySize);
156             }
157         }
158
159         readline_clear_history();
160         foreach ($hist as $line) {
161             readline_add_history($line);
162         }
163
164         if ($this->historyFile !== false) {
165             return readline_write_history($this->historyFile);
166         }
167
168         return true;
169     }
170 }