CLI::prompt()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 17
rs 9.4285
cc 3
eloc 13
nc 4
nop 2
1
<?php
2
/* zLibrary
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Affero General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Affero General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Affero General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
class CLI
19
{
20
	/**
21
	 * @param string $message
22
	 */
23
	public static function out($message, $die = false)
24
	{
25
		$colors = array(
26
				"|w|" => "1;37", //White
27
				"|b|" => "0;34", //Blue
28
				"|g|" => "0;32", //Green
29
				"|r|" => "0;31", //Red
30
				"|n|" => "0" //Neutral
31
			       );
32
33
		foreach($colors as $color => $value)
34
			$message = str_replace($color, "\033[".$value."m", $message);
35
36
		echo $message.PHP_EOL;
37
		if($die) die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method out() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
38
	}
39
40
	/**
41
	 * @param string $prompt
42
	 */
43
	public static function prompt($prompt, $default = "") {
44
		$colors = array(
45
				"|w|" => "1;37", //White
46
				"|b|" => "0;34", //Blue
47
				"|g|" => "0;32", //Green
48
				"|r|" => "0;31", //Red
49
				"|n|" => "0" //Neutral
50
			       );
51
52
		foreach($colors as $color => $value)
53
			$prompt = str_replace($color, "\033[".$value."m", $prompt);
54
55
		echo "$prompt [$default] ";
56
		$answer = trim(fgets(STDIN));
57
		if (strlen($answer) == 0) return $default;
58
		return $answer;
59
	}
60
61
	// Password prompter kindly borrowed from http://stackoverflow.com/questions/187736/command-line-password-prompt-in-php
62
	public static function prompt_silent($prompt = "Enter Password:") {
63
		$command = "/usr/bin/env bash -c 'echo OK'";
64
		if (rtrim(shell_exec($command)) !== 'OK') {
65
			trigger_error("Can't invoke bash");
66
			return;
67
		}
68
		$command = "/usr/bin/env bash -c 'read -s -p \""
69
			. addslashes($prompt)
70
			. "\" mypassword && echo \$mypassword'";
71
		$password = rtrim(shell_exec($command));
72
		echo "\n";
73
		return $password;
74
	}
75
}
76