1 | <?php |
||
9 | class CLI |
||
10 | { |
||
11 | |||
12 | /** |
||
13 | * The command line prompt |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | private $prompt; |
||
18 | |||
19 | /** |
||
20 | * Handle for stdin channel |
||
21 | * |
||
22 | * @var resource |
||
23 | */ |
||
24 | private $stdin; |
||
25 | |||
26 | /** |
||
27 | * Handle for stdout channel |
||
28 | * |
||
29 | * @var resource |
||
30 | */ |
||
31 | private $stdout; |
||
32 | |||
33 | /** |
||
34 | * Handle for stderr channel |
||
35 | * |
||
36 | * @var resource |
||
37 | */ |
||
38 | private $stderr; |
||
39 | |||
40 | /** |
||
41 | * Command line parser |
||
42 | * |
||
43 | * @var Parser |
||
44 | */ |
||
45 | private $parser; |
||
46 | |||
47 | /** |
||
48 | * Create a new CLI instance |
||
49 | * |
||
50 | * @param string $prompt |
||
51 | * Optional command line prompt (default 'cli') |
||
52 | * @param \Nkey\Caribu\Console\Parser $parser |
||
53 | * Optional command line parser (default instance of \Nkey\Caribu\Console\DefaultParser) |
||
54 | */ |
||
55 | 2 | public function __construct(string $prompt = "cli", Parser $parser = null) |
|
56 | { |
||
57 | 2 | $this->setPrompt($prompt); |
|
58 | |||
59 | 2 | $this->parser = $parser; |
|
60 | 2 | if (null === $this->parser) { |
|
61 | 2 | $this->parser = new DefaultParser(); |
|
62 | } |
||
63 | |||
64 | 2 | $this->stdin = fopen("php://stdin", "r"); |
|
65 | 2 | $this->stdout = fopen("php://stdout", "w"); |
|
66 | 2 | $this->stderr = fopen("php://stderr", "w"); |
|
67 | 2 | } |
|
68 | |||
69 | /** |
||
70 | * Set a custom stdin channel |
||
71 | * |
||
72 | * @param resource $stdin |
||
73 | * The channel |
||
74 | * @return \Nkey\Caribu\Console\CLI |
||
75 | */ |
||
76 | 2 | public function setStdIn($stdin): CLI |
|
81 | |||
82 | /** |
||
83 | * Set a custom stdout channel |
||
84 | * |
||
85 | * @param resource $stdout |
||
86 | * The channel |
||
87 | * @return \Nkey\Caribu\Console\CLI |
||
88 | */ |
||
89 | 2 | public function setStdOut($stdout): CLI |
|
94 | |||
95 | /** |
||
96 | * Set a custom stderr channel |
||
97 | * |
||
98 | * @param resource $stderr |
||
99 | * The channel |
||
100 | * @return \Nkey\Caribu\Console\CLI |
||
101 | */ |
||
102 | 2 | public function setStdErr($stderr): CLI |
|
107 | |||
108 | /** |
||
109 | * Read full line from stdin |
||
110 | * |
||
111 | * @return ParsedCommand The parsed command |
||
112 | */ |
||
113 | 2 | public function readLine(): ParsedCommand |
|
119 | |||
120 | /** |
||
121 | * Write output to stdout channel |
||
122 | * |
||
123 | * @param string $output |
||
124 | * The output to write |
||
125 | * |
||
126 | * @return \Nkey\Caribu\Console\CLI Fluent interface instance |
||
127 | */ |
||
128 | 2 | public function writeStdout(string $output): CLI |
|
134 | |||
135 | /** |
||
136 | * Write a line to stdout channel |
||
137 | * |
||
138 | * @param string $line |
||
139 | * The line to write, line break will be added |
||
140 | * |
||
141 | * @return \Nkey\Caribu\Console\CLI |
||
142 | */ |
||
143 | 1 | public function writeStdoutLine(string $line): CLI |
|
148 | |||
149 | /** |
||
150 | * Write a line to stderr channel |
||
151 | * |
||
152 | * @param string $line |
||
153 | * The line to write, line break will be added |
||
154 | * |
||
155 | * @return \Nkey\Caribu\Console\CLI |
||
156 | */ |
||
157 | 1 | public function writeStderrLine(string $line): CLI |
|
162 | |||
163 | /** |
||
164 | * Write output to stderr channel |
||
165 | * |
||
166 | * @param string $output |
||
167 | * The output to write |
||
168 | * |
||
169 | * @return \Nkey\Caribu\Console\CLI Fluent interface instance |
||
170 | */ |
||
171 | 1 | public function writeStderr(string $output): CLI |
|
177 | |||
178 | /** |
||
179 | * Set the prompt |
||
180 | * |
||
181 | * @param string $prompt |
||
182 | * The prompt to set |
||
183 | * |
||
184 | * @return \Nkey\Caribu\Console\CLI Fluent interface instance |
||
185 | */ |
||
186 | 2 | public function setPrompt(string $prompt): CLI |
|
191 | } |
||
192 |