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