forked from travist/cliphp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.php
67 lines (57 loc) · 1.49 KB
/
cli.php
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
<?php
class CLIPHP {
var $args = array();
public function __construct() {
global $argv;
set_time_limit(0);
// Try the config file first.
$handle = fopen(dirname(__FILE__) . '/config', 'a+');
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
$values = explode('=', $buffer);
$this->args[trim($values[0])] = trim($values[1]);
}
fclose($handle);
}
// Now try the arguments.
$name = '';
foreach( $argv as $arg ) {
if (substr($arg, 0, 1) === '-') {
$name = substr($arg, 1);
}
else if( $name ) {
$this->args[$name] = $arg;
$name = '';
}
}
}
public function read() {
$fp = fopen("php://stdin", "r");
$in = fgets($fp, 4094);
fclose($fp);
return trim($in);
}
public function cache( $name, $value ) {
$handle = fopen(dirname(__FILE__) . '/config', 'a+');
if ($handle) {
fwrite($handle, $name . '=' . $value . "\n");
fclose($handle);
}
}
public function set( $name, $value, $cache = FALSE ) {
if( !isset($this->args[$name]) ) {
if( $cache ) {
$this->cache( $name, $value );
}
}
$this->args[$name] = $value;
}
public function get( $name, $output, $cache = FALSE ) {
if (!isset($this->args[$name])) {
echo $output . "\n";
$this->set( $name, $this->read(), $cache );
}
return $this->args[$name];
}
}
?>