Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
Console.php
Go to the documentation of this file.
1<?php
3
12class Console {
13
19 public static function readline() {
20 return \rtrim(\fgets(STDIN));
21 }
22
30 public static function question($prompt, array $propositions = null, array $options = []) {
32 $hiddenProposals = $options['hiddenProposals'] ?? [];
33 $continue = function ($rep, $array) {
34 return \array_search($rep, $array) === false;
35 };
36 if (isset($options['default'])) {
37 $prompt .= ConsoleFormatter::formatHtml(" (default:<b>" . $options['default'] . "</b>)");
38 }
39 if (isset($options['ignoreCase'])) {
40 $continue = function ($rep, $array) {
41 return \array_search(\strtolower($rep), \array_map('strtolower', $array)) === false;
42 };
43 }
44 echo $prompt;
45 if (\is_array($propositions)) {
46 if (\count($propositions) > 2) {
47 $props = "";
48 foreach ($propositions as $index => $prop) {
49 $dec = 2 - \strlen(($index + 1) . '');
50 $props .= "[" . ($index + 1) . "] " . str_repeat(' ', $dec) . $prop . "\n";
51 }
53 do {
54 $answer = self::readline();
55 } while ((int) $answer != $answer || ! isset($propositions[(int) $answer - 1]));
56 $answer = $propositions[(int) $answer - 1];
57 } else {
58 echo " (" . implode("/", $propositions) . ")\n";
59 $propositions = \array_merge($propositions, $hiddenProposals);
60 do {
61 $answer = self::readline();
62 } while ($continue($answer, $propositions));
63 }
64 } else {
65 $answer = self::readline();
66 }
67 if (isset($options['default']) && $answer == '') {
68 $answer = $options['default'];
69 }
70 return $answer;
71 }
72
73 public static function yesNoQuestion($prompt, array $propositions = [
74 'yes',
75 'no'
76 ], array $options = []) {
77 return self::question($prompt, $propositions, [
78 'ignoreCase' => true,
79 'hiddenProposals' => [
80 'y',
81 'n'
82 ]
83 ]);
84 }
85
86 public static function explodeResponse(string $response, $callback = 'trim', string $separator = ',') {
87 return \array_map($callback, \array_filter(\explode($separator, \trim($response)), 'strlen'));
88 }
89
96 public static function isYes($answer) {
97 return \array_search(\trim($answer), [
98 'yes',
99 'y'
100 ]) !== false;
101 }
102
109 public static function isNo($answer) {
110 return \array_search(\trim($answer), [
111 'no',
112 'n'
113 ]) !== false;
114 }
115
122 public static function isCancel($answer) {
123 return \array_search(\trim($answer), [
124 'cancel',
125 'z'
126 ]) !== false;
127 }
128
133 public static function reExecute(): void {
134 global $argv;
135 $argv[0]=\realpath($argv[0]);
136 \system(PHP_BINARY.' '.\implode(' ',$argv));
137 die();
138 }
139}
static formatContent($content, $prefix=" ยท ")
Format a multilines content.
static colorize($string, $color=null, $bgColor=null)
Returns a colored string.
static formatHtml($str)
Format an html message (only for bold values).
Ubiquity\devtools\cmd$Console This class is part of Ubiquity.
Definition Console.php:12
static reExecute()
ReExecutes the loaded script.
Definition Console.php:133
static yesNoQuestion($prompt, array $propositions=['yes', 'no'], array $options=[])
Definition Console.php:73
static isNo($answer)
Returns true if the answer is no or n.
Definition Console.php:109
static isCancel($answer)
Returns true if the answer is cancel or z.
Definition Console.php:122
static question($prompt, array $propositions=null, array $options=[])
Ask the user a question and return the answer.
Definition Console.php:30
static readline()
Read a line from the user input.
Definition Console.php:19
static isYes($answer)
Returns true if the answer is yes or y.
Definition Console.php:96
static explodeResponse(string $response, $callback='trim', string $separator=',')
Definition Console.php:86