1 | <?php |
||
38 | class ConsoleIO |
||
39 | { |
||
40 | |||
41 | /** |
||
42 | * Least output level. |
||
43 | * At this level clearice is expected to be mute. Nothing would be outputted |
||
44 | * to any of the streams. |
||
45 | * @var int |
||
46 | */ |
||
47 | const OUTPUT_LEVEL_0 = 0; |
||
48 | |||
49 | /** |
||
50 | * Output level 1 |
||
51 | * @var int |
||
52 | */ |
||
53 | const OUTPUT_LEVEL_1 = 1; |
||
54 | |||
55 | /** |
||
56 | * Output level 2 |
||
57 | * @var int |
||
58 | */ |
||
59 | const OUTPUT_LEVEL_2 = 2; |
||
60 | |||
61 | /** |
||
62 | * Output level 3. |
||
63 | * At this level clearice is expected not to filter any output. Everything |
||
64 | * that is sent to ClearIce would be outputted to the streams. |
||
65 | * @var int |
||
66 | */ |
||
67 | const OUTPUT_LEVEL_3 = 3; |
||
68 | |||
69 | /** |
||
70 | * The default output level of the ClearIce library. |
||
71 | * @var int |
||
72 | */ |
||
73 | private $defaultOutputLevel = self::OUTPUT_LEVEL_1; |
||
74 | |||
75 | /** |
||
76 | * An array to hold the output level stack. |
||
77 | * @var array |
||
78 | */ |
||
79 | private $outputLevelStack = array(); |
||
80 | |||
81 | /** |
||
82 | * An array of the three streams used primarily for I/O. These are the |
||
83 | * standard output stream, the standard input stream and the error stream. |
||
84 | * Being an associative array, this property presents the three streams |
||
85 | * through its output, input and error keys. |
||
86 | * |
||
87 | * @var array |
||
88 | */ |
||
89 | private $streams = array(); |
||
90 | |||
91 | /** |
||
92 | * The URLs of the various streams used for I/O. This variable stores these |
||
93 | * URLs under the input, output and error streams respectively. |
||
94 | * |
||
95 | * @see ClearIce::$streams |
||
96 | * @var array |
||
97 | */ |
||
98 | private $streamUrls = array( |
||
99 | 'input' => 'php://stdin', |
||
100 | 'output' => 'php://stdout', |
||
101 | 'error' => 'php://stderr' |
||
102 | ); |
||
103 | |||
104 | /** |
||
105 | * A function for getting answers to questions from users interractively. |
||
106 | * This function takes the question and an optional array of parameters. |
||
107 | * The question is a regular string and the array provides extra information |
||
108 | * about the question being asked. |
||
109 | * |
||
110 | * The array takes the following parameters |
||
111 | * |
||
112 | * **answers** |
||
113 | * An array of posible answers to the question. Once this array is available |
||
114 | * the user would be expected to provide an answer which is specifically in |
||
115 | * the list. Any other answer would be rejected. The library would print |
||
116 | * out all the possible answers so the user is aware of which answers |
||
117 | * are valid. |
||
118 | * |
||
119 | * **default** |
||
120 | * A default answer which should be used in case the user does not supply an |
||
121 | * answer. The library would make the user aware of this default by placing |
||
122 | * it in square brackets after the question. |
||
123 | * |
||
124 | * **required** |
||
125 | * If this flag is set, the user would be required to provide an answer. A |
||
126 | * blank answer would be rejected. |
||
127 | * |
||
128 | * @param string $question The question you want to ask |
||
129 | * @param array $params An array of options that this function takes. |
||
130 | * @return string The response provided by the user to the prompt. |
||
131 | */ |
||
132 | 9 | public function getResponse($question, $params = array()) |
|
167 | |||
168 | /** |
||
169 | * Set the URL of any of the streams used by ClearIce. |
||
170 | * ClearIce maintains three different streams for its I/O operations. The |
||
171 | * `output` stream is used for output, the `error` stream is used for errors |
||
172 | * and the `input` stream is used for input. The `output` and `error` streams |
||
173 | * are represented by the standard output and standard error streams |
||
174 | * respectively. The `input` stream on the other hand is represented by the |
||
175 | * standard input stream by default. |
||
176 | * |
||
177 | * Streams could be any valid PHP stream URL. |
||
178 | * Example to write all output to a file you could set. |
||
179 | * |
||
180 | * ````php |
||
181 | * <?php |
||
182 | * ClearIce::setStreamUrl('output', '/path/to/file'); |
||
183 | * ClearIce::setStreamUrl('error', '/path/to/file'); |
||
184 | * ```` |
||
185 | * |
||
186 | * Once a new URL is set, any old streams are closed and the new one is |
||
187 | * opened in its place immediately the stream is accessed. |
||
188 | * |
||
189 | * @param string $type The type of stream to set a URL for. The value of this |
||
190 | * could either be 'error', 'input' or 'output'. |
||
191 | * |
||
192 | * @param string $url The URL of the stream. Based on the type of stream |
||
193 | * being requested, the right kind of permissions must |
||
194 | * be set. For instance |
||
195 | */ |
||
196 | 19 | public function setStreamUrl($type, $url) |
|
201 | |||
202 | /** |
||
203 | * Write a string to the output stream. |
||
204 | * If an output stream is not defined this method writes to the standard |
||
205 | * output (the console) by default. |
||
206 | * |
||
207 | * @param string $string |
||
208 | */ |
||
209 | 17 | public function output($string, $outputLevel = self::OUTPUT_LEVEL_1, $stream = 'output') |
|
215 | |||
216 | /** |
||
217 | * Write a string to the error stream. |
||
218 | * If an error stream is not defined this method writes to the standard |
||
219 | * error (the console) by default. |
||
220 | * |
||
221 | * @param string $string |
||
222 | */ |
||
223 | 5 | public function error($string, $outputLevel = self::OUTPUT_LEVEL_1) |
|
227 | |||
228 | /** |
||
229 | * Set the output level of the ClearIce output streams (including the error) |
||
230 | * stream. |
||
231 | * |
||
232 | * @param int $outputLevel |
||
233 | */ |
||
234 | 3 | public function setOutputLevel($outputLevel) |
|
238 | |||
239 | /** |
||
240 | * Returns the current output level of the CliearIce library. |
||
241 | * @return int |
||
242 | */ |
||
243 | 2 | public function getOutputLevel() |
|
247 | |||
248 | /** |
||
249 | * Push an output level unto the output level stack. |
||
250 | * The output level pushed becomes the new output level with which ClearIce |
||
251 | * would work. The previous level pushed would be automatically restored |
||
252 | * when the ClearIce::popOutputLevel() method is called. Using the output |
||
253 | * level stack gives you a convenient way to change the output level |
||
254 | * temporarily without having to keep a record of the previous output level. |
||
255 | * |
||
256 | * @param int $outputLevel |
||
257 | */ |
||
258 | 1 | public function pushOutputLevel($outputLevel) |
|
263 | |||
264 | /** |
||
265 | * Pop the last output level which was pushed unto the output level stack. |
||
266 | * This restores the previous output level which was active before the last |
||
267 | * call to the ClearIce::pushOutputLevel() method. Using the output |
||
268 | * level stack gives you a convenient way to change the output level |
||
269 | * temporarily without having to keep a record of the previous output level. |
||
270 | * |
||
271 | */ |
||
272 | 1 | public function popOutputLevel() |
|
276 | |||
277 | /** |
||
278 | * Resets the output level stack. |
||
279 | * This method clears all items off the output level stack leaving only the |
||
280 | * current output level. |
||
281 | */ |
||
282 | 1 | public function resetOutputLevel() |
|
289 | |||
290 | /** |
||
291 | * Reads a line of string from the input stream. |
||
292 | * If an input stream is not defined this method reads an input from the |
||
293 | * standard input (usually a keyboard) by default. |
||
294 | * |
||
295 | * @todo look into using readline for this in cases where it's available |
||
296 | * @return string |
||
297 | */ |
||
298 | 9 | public function input() |
|
302 | |||
303 | /** |
||
304 | * Returns a stream resource for a given stream type. |
||
305 | * If the stream has not been opened this method opens the stream before |
||
306 | * returning the asociated resource. This ensures that there is only one |
||
307 | * resource handle to any stream at any given time. |
||
308 | * |
||
309 | * @param string $type |
||
310 | * @return resource |
||
311 | */ |
||
312 | 17 | private function getStream($type) |
|
319 | |||
320 | } |
||
321 |