0cc0bb40f2b8649cc06de21d577c7b76728f2a44
[pharext/pharext] / Command.php
1 <?php
2
3 namespace pharext\Cli;
4
5 use pharext\Cli\Args as CliArgs;
6
7 use Phar;
8
9 if (!function_exists("array_column")) {
10 function array_column(array $array, $col, $idx = null) {
11 $result = [];
12 foreach ($array as $el) {
13 if (isset($idx)) {
14 $result[$el[$idx]] = $el[$col];
15 } else {
16 $result[] = $el[$col];
17 }
18 }
19 return $result;
20 }
21 }
22
23 trait Command
24 {
25 /**
26 * Command line arguments
27 * @var pharext\CliArgs
28 */
29 private $args;
30
31 /**
32 * @inheritdoc
33 * @see \pharext\Command::getArgs()
34 */
35 public function getArgs() {
36 return $this->args;
37 }
38
39 /**
40 * Retrieve metadata of the currently running phar
41 * @param string $key
42 * @return mixed
43 */
44 public function metadata($key = null) {
45 $running = new Phar(Phar::running(false));
46
47 if ($key === "signature") {
48 $sig = $running->getSignature();
49 return sprintf("%s signature of %s\n%s",
50 $sig["hash_type"],
51 $this->metadata("name"),
52 chunk_split($sig["hash"], 64, "\n"));
53 }
54
55 $metadata = $running->getMetadata();
56 if (isset($key)) {
57 return $metadata[$key];
58 }
59 return $metadata;
60 }
61
62 /**
63 * Output pharext vX.Y.Z header
64 */
65 public function header() {
66 if (!headers_sent()) {
67 /* only display header, if we didn't generate any output yet */
68 printf("%s\n\n", $this->metadata("header"));
69 }
70 }
71
72 /**
73 * @inheritdoc
74 * @see \pharext\Command::debug()
75 */
76 public function debug($fmt) {
77 if ($this->args->verbose) {
78 vprintf($fmt, array_slice(func_get_args(), 1));
79 }
80 }
81
82 /**
83 * @inheritdoc
84 * @see \pharext\Command::info()
85 */
86 public function info($fmt) {
87 if (!$this->args->quiet) {
88 vprintf($fmt, array_slice(func_get_args(), 1));
89 }
90 }
91
92 /**
93 * @inheritdoc
94 * @see \pharext\Command::warn()
95 */
96 public function warn($fmt) {
97 if (!$this->args->quiet) {
98 if (!isset($fmt)) {
99 $fmt = "%s\n";
100 $arg = error_get_last()["message"];
101 } else {
102 $arg = array_slice(func_get_args(), 1);
103 }
104 vfprintf(STDERR, "Warning: $fmt", $arg);
105 }
106 }
107
108 /**
109 * @inheritdoc
110 * @see \pharext\Command::error()
111 */
112 public function error($fmt) {
113 if (!$this->args->quiet) {
114 if (!isset($fmt)) {
115 $fmt = "%s\n";
116 $arg = error_get_last()["message"];
117 } else {
118 $arg = array_slice(func_get_args(), 1);
119 }
120 vfprintf(STDERR, "ERROR: $fmt", $arg);
121 }
122 }
123
124 /**
125 * Output command line help message
126 * @param string $prog
127 */
128 public function help($prog) {
129 printf("Usage:\n\n \$ %s", $prog);
130
131 $flags = [];
132 $required = [];
133 $optional = [];
134 foreach ($this->args->getSpec() as $spec) {
135 if ($spec[3] & CliArgs::REQARG) {
136 if ($spec[3] & CliArgs::REQUIRED) {
137 $required[] = $spec;
138 } else {
139 $optional[] = $spec;
140 }
141 } else {
142 $flags[] = $spec;
143 }
144 }
145
146 if ($flags) {
147 printf(" [-%s]", implode("", array_column($flags, 0)));
148 }
149 foreach ($required as $req) {
150 printf(" -%s <arg>", $req[0]);
151 }
152 if ($optional) {
153 printf(" [-%s <arg>]", implode("|-", array_column($optional, 0)));
154 }
155 printf("\n\n");
156 $spc = $this->args->getSpec();
157 $max = max(array_map("strlen", array_column($spc, 1)));
158 $max += $max % 8 + 2;
159 foreach ($spc as $spec) {
160 if (isset($spec[0])) {
161 printf(" -%s|", $spec[0]);
162 } else {
163 printf(" ");
164 }
165 printf("--%s ", $spec[1]);
166 if ($spec[3] & CliArgs::REQARG) {
167 printf("<arg> ");
168 } elseif ($spec[3] & CliArgs::OPTARG) {
169 printf("[<arg>]");
170 } else {
171 printf(" ");
172 }
173 printf("%s%s", str_repeat(" ", $max-strlen($spec[1])+3*!isset($spec[0])), $spec[2]);
174 if ($spec[3] & CliArgs::REQUIRED) {
175 printf(" (REQUIRED)");
176 }
177 if (isset($spec[4])) {
178 printf(" [%s]", $spec[4]);
179 }
180 printf("\n");
181 }
182 printf("\n");
183 }
184
185 /**
186 * rm -r
187 * @param string $dir
188 */
189 private function rm($dir) {
190 foreach (scandir($dir) as $entry) {
191 if ($entry === "." || $entry === "..") {
192 continue;
193 } elseif (is_dir("$dir/$entry")) {
194 $this->rm("$dir/$entry");
195 } elseif (!unlink("$dir/$entry")) {
196 $this->warn(null);
197 }
198 }
199 if (!rmdir($dir)) {
200 $this->warn(null);
201 }
202 }
203 }