This repository was archived by the owner on Aug 20, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCLI.php
More file actions
110 lines (88 loc) · 2.38 KB
/
CLI.php
File metadata and controls
110 lines (88 loc) · 2.38 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
<?php
/**
* The MIT License
* http://creativecommons.org/licenses/MIT/
*
* Copyright (c) Alix Axel <alix.axel@gmail.com>
**/
class phunction_CLI extends phunction
{
public function __construct()
{
}
public static function Argument($key = null, $default = false)
{
if (array_key_exists('argv', $GLOBALS) === true)
{
return parent::Value($GLOBALS['argv'], $key, $default);
}
return $default;
}
public static function Call($command, $arguments = null, $delimiter = ' ')
{
foreach (($arguments = (array) $arguments) as $key => $value)
{
if (isset($value) === true)
{
$value = $delimiter . escapeshellarg($value);
}
$arguments[$key] = (is_int($key) === true) ? trim($value) : $key . $value;
}
return escapeshellcmd(trim(sprintf('%s %s', $command, implode(' ', $arguments))));
}
public static function Error($string, $eol = true)
{
if (defined('STDERR') === true)
{
if ($eol === true)
{
$string .= PHP_EOL;
}
return fwrite(STDERR, $string);
}
return false;
}
public static function Format($string, $foreground = null, $background = null, $extra = null)
{
if (strncasecmp('WIN', PHP_OS, 3) !== 0)
{
$format = array(0 => 'reset', 1 => 'bold', 4 => 'underline', 7 => 'negative');
$colors = array_merge(array_flip(explode('|', 'black|red|green|yellow|blue|magenta|cyan|white')), array('default' => 9));
foreach (array('foreground', 'background') as $key)
{
$$key = parent::Value($colors, strtolower($$key), 9);
}
if (strlen($extra = implode(';', array_keys(array_intersect($format, explode('|', $extra))))) == 0)
{
$extra = 0;
}
return sprintf('%c[%s;%u;%um%s%c[0m', 27, $extra, $foreground + 30, $background + 40, $string, 27);
}
return $string;
}
public static function Read($bytes = null, $format = null)
{
if (defined('STDIN') === true)
{
$result = (isset($bytes) === true) ? fread(STDIN, $bytes) : fgets(STDIN);
if (($result !== false) && (isset($format) === true))
{
return sscanf($result, $format);
}
return rtrim($result, PHP_EOL);
}
return false;
}
public static function Write($string, $eol = true)
{
if (defined('STDOUT') === true)
{
if ($eol === true)
{
$string .= PHP_EOL;
}
return fwrite(STDOUT, $string);
}
return false;
}
}