-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleApp.php
More file actions
161 lines (132 loc) · 4.06 KB
/
ConsoleApp.php
File metadata and controls
161 lines (132 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
namespace SoftBricks\CLI;
/**
* Class ConsoleApp
* Helps creating CLI scripts
* @package SoftBricks\CLI
*/
abstract class ConsoleApp {
// define read pointer
protected $inputHandle;
// stack of all arguments that application got on call
protected $analyzedARGS = array();
protected $rawARGS = array();
/**
* Opens handle for the input stream & renders console arguments into array stack
*/
function __construct(){
// open input stream
$this->inputHandle = fopen("php://stdin","r");
// set the raw arguments
$this->rawARGS = $GLOBALS['argv'];
unset($this->rawARGS[0]);
$this->rawARGS = array_values($this->rawARGS);
// parse all arguments we've got
parse_str(implode('&', array_slice($GLOBALS['argv'], 1)), $this->analyzedARGS);
// run this console application
$this->run();
}
/**
* We always close the input stream... (safety first!)
*/
function __destruct(){
// close input stream
fclose($this->inputHandle);
}
/**
* @param string $bufferSize optional
* @return string the users console input
*/
protected function readString ($bufferSize='255') {
// read from input stream
return trim(
fgets($this->inputHandle, $bufferSize)
);
}
/**
* @return int the users console input as string
*/
protected function readInt () {
// initialize integer that should be returned
$number = null;
// read number from input stream
fscanf($this->inputHandle, "%d\n", $number);
// return the parsed number
return $number;
}
/**
* Creates a colored text output string for unix shells
* @param $string
* @param null $foreground_color
* @param null $background_color
* @return string
*/
private function colorizeString($string, $foreground_color = null, $background_color = null) {
$colored_string = "";
// Check if given foreground color found
if ($foreground_color !== null) {
$colored_string .= "\033[" .$foreground_color . "m";
}
// Check if given background color found
if ($background_color !== null) {
$colored_string .= "\033[" . $background_color . "m";
}
// Add string and end coloring
$colored_string .= $string . "\033[0m";
return $colored_string;
}
/**
* writes some text into the console and ends the line with a linebreak
* @param $txt
*/
protected function println($txt, $colorForeground = null, $colorBackground = null) {
echo $this->colorizeString($txt, $colorForeground, $colorBackground)."\n";
}
/**
* writes some text into the console (no linebreak is attached)
* @param $txt
* @param null $colorForeground
* @param null $colorBackground
*/
protected function _print($txt, $colorForeground = null, $colorBackground = null){
echo $this->colorizeString($txt, $colorForeground, $colorBackground);
}
/**
* printes amount $n (standard 1) line breaks into the console
* @param int $n
*/
protected function lineBreak($n = 1) {
for($i = 1; $i<=$n; $i++) echo "\n";
}
/**
* checks if $arg was set at the callup of this script or not
* @param $arg
* @return bool
*/
protected function isArgAvailable($arg) {
return isset($this->analyzedARGS[$arg]);
}
/**
* @param $arg
* @return mixed|null the requested argument or NULL if not available
*/
protected function getArg($arg) {
if ($this->isArgAvailable($arg)) {
return $this->analyzedARGS[$arg];
} else {
return null;
}
}
/**
* @return void
*/
abstract protected function run();
/**
* Creates an instance of the child class, which will execute the console app
*/
public static function execute()
{
$className = get_called_class();
new $className();
}
}