0ec52fffea07b06b829ee4e3ccffd68a651c8a98
[pharext/pharext] / src / pharext / Cli / Command.php
1 <?php
2
3 namespace pharext\Cli;
4
5 use pharext\Cli\Args as CliArgs;
6
7 require_once "pharext/Version.php";
8
9 trait Command
10 {
11 /**
12 * Command line arguments
13 * @var pharext\CliArgs
14 */
15 private $args;
16
17 /**
18 * @inheritdoc
19 * @see \pharext\Command::getArgs()
20 */
21 public function getArgs() {
22 return $this->args;
23 }
24
25 /**
26 * Output pharext vX.Y.Z header
27 */
28 function header() {
29 printf("pharext v%s (c) Michael Wallner <mike@php.net>\n\n",
30 \pharext\VERSION);
31 }
32
33 /**
34 * @inheritdoc
35 * @see \pharext\Command::info()
36 */
37 public function info($fmt) {
38 if (!$this->args->quiet) {
39 vprintf($fmt, array_slice(func_get_args(), 1));
40 }
41 }
42
43 /**
44 * @inheritdoc
45 * @see \pharext\Command::error()
46 */
47 public function error($fmt) {
48 if (!$this->args->quiet) {
49 if (!isset($fmt)) {
50 $fmt = "%s\n";
51 $arg = error_get_last()["message"];
52 } else {
53 $arg = array_slice(func_get_args(), 1);
54 }
55 vfprintf(STDERR, "ERROR: $fmt", $arg);
56 }
57 }
58
59 /**
60 * Output command line help message
61 * @param string $prog
62 */
63 public function help($prog) {
64 printf("Usage:\n\n \$ %s", $prog);
65
66 $flags = [];
67 $required = [];
68 $optional = [];
69 foreach ($this->args->getSpec() as $spec) {
70 if ($spec[3] & CliArgs::REQARG) {
71 if ($spec[3] & CliArgs::REQUIRED) {
72 $required[] = $spec;
73 } else {
74 $optional[] = $spec;
75 }
76 } else {
77 $flags[] = $spec;
78 }
79 }
80
81 if ($flags) {
82 printf(" [-%s]", implode("", array_column($flags, 0)));
83 }
84 foreach ($required as $req) {
85 printf(" -%s <arg>", $req[0]);
86 }
87 if ($optional) {
88 printf(" [-%s <arg>]", implode("|-", array_column($optional, 0)));
89 }
90 printf("\n\n");
91 $spc = $this->args->getSpec();
92 $max = max(array_map("strlen", array_column($spc, 1)));
93 $max += $max % 8 + 2;
94 foreach ($spc as $spec) {
95 if (isset($spec[0])) {
96 printf(" -%s|", $spec[0]);
97 } else {
98 printf(" ");
99 }
100 printf("--%s ", $spec[1]);
101 if ($spec[3] & CliArgs::REQARG) {
102 printf("<arg> ");
103 } elseif ($spec[3] & CliArgs::OPTARG) {
104 printf("[<arg>]");
105 } else {
106 printf(" ");
107 }
108 printf("%s%s", str_repeat(" ", $max-strlen($spec[1])+3*!isset($spec[0])), $spec[2]);
109 if ($spec[3] & CliArgs::REQUIRED) {
110 printf(" (REQUIRED)");
111 }
112 if (isset($spec[4])) {
113 printf(" [%s]", $spec[4]);
114 }
115 printf("\n");
116 }
117 printf("\n");
118 }
119
120 /**
121 * Create temporary file/directory name
122 * @param string $prefix
123 * @param string $suffix
124 */
125 private function tempname($prefix, $suffix = null) {
126 if (!isset($suffix)) {
127 $suffix = uniqid();
128 }
129 return sprintf("%s/%s.%s", sys_get_temp_dir(), $prefix, $suffix);
130 }
131
132 /**
133 * Create a new temp directory
134 * @param string $prefix
135 * @return string
136 */
137 private function newtemp($prefix) {
138 $temp = $this->tempname($prefix);
139 if (!is_dir($temp)) {
140 if (!mkdir($temp, 0700, true)) {
141 $this->error(null);
142 exit(3);
143 }
144 }
145 return $temp;
146 }
147
148 /**
149 * rm -r
150 * @param string $dir
151 */
152 private function rm($dir) {
153 foreach (scandir($dir) as $entry) {
154 if ($entry === "." || $entry === "..") {
155 continue;
156 } elseif (is_dir("$dir/$entry")) {
157 $this->rm("$dir/$entry");
158 } elseif (!unlink("$dir/$entry")) {
159 $this->error(null);
160 }
161 }
162 if (!rmdir($dir)) {
163 $this->error(null);
164 }
165 }
166 }