bc0afbbf374ac4bfca064ff1215f5fd8ffcdcbab
[pharext/pharext] / src / pharext / Cli / Command.php
1 <?php
2
3 namespace pharext\Cli;
4
5 use pharext\Archive;
6 use pharext\Cli\Args as CliArgs;
7
8 use Phar;
9
10 if (!function_exists("array_column")) {
11 function array_column(array $array, $col, $idx = null) {
12 $result = [];
13 foreach ($array as $el) {
14 if (isset($idx)) {
15 $result[$el[$idx]] = $el[$col];
16 } else {
17 $result[] = $el[$col];
18 }
19 }
20 return $result;
21 }
22 }
23
24 trait Command
25 {
26 /**
27 * Command line arguments
28 * @var pharext\CliArgs
29 */
30 private $args;
31
32 /**
33 * @inheritdoc
34 * @see \pharext\Command::getArgs()
35 */
36 public function getArgs() {
37 return $this->args;
38 }
39
40 /**
41 * Retrieve metadata of the currently running phar
42 * @param string $key
43 * @return mixed
44 */
45 public function metadata($key = null) {
46 if (extension_loaded("Phar")) {
47 $running = new Phar(Phar::running(false));
48 } else {
49 $running = new Archive(PHAREXT_PHAR);
50 }
51
52 if ($key === "signature") {
53 $sig = $running->getSignature();
54 return sprintf("%s signature of %s\n%s",
55 $sig["hash_type"],
56 $this->metadata("name"),
57 chunk_split($sig["hash"], 64, "\n"));
58 }
59
60 $metadata = $running->getMetadata();
61 if (isset($key)) {
62 return $metadata[$key];
63 }
64 return $metadata;
65 }
66
67 /**
68 * Output pharext vX.Y.Z header
69 */
70 public function header() {
71 if (!headers_sent()) {
72 /* only display header, if we didn't generate any output yet */
73 printf("%s\n\n", $this->metadata("header"));
74 }
75 }
76
77 /**
78 * @inheritdoc
79 * @see \pharext\Command::debug()
80 */
81 public function debug($fmt) {
82 if ($this->args->verbose) {
83 vprintf($fmt, array_slice(func_get_args(), 1));
84 }
85 }
86
87 /**
88 * @inheritdoc
89 * @see \pharext\Command::info()
90 */
91 public function info($fmt) {
92 if (!$this->args->quiet) {
93 vprintf($fmt, array_slice(func_get_args(), 1));
94 }
95 }
96
97 /**
98 * @inheritdoc
99 * @see \pharext\Command::warn()
100 */
101 public function warn($fmt) {
102 if (!$this->args->quiet) {
103 if (!isset($fmt)) {
104 $fmt = "%s\n";
105 $arg = error_get_last()["message"];
106 } else {
107 $arg = array_slice(func_get_args(), 1);
108 }
109 vfprintf(STDERR, "Warning: $fmt", $arg);
110 }
111 }
112
113 /**
114 * @inheritdoc
115 * @see \pharext\Command::error()
116 */
117 public function error($fmt) {
118 if (!isset($fmt)) {
119 $fmt = "%s\n";
120 $arg = error_get_last()["message"];
121 } else {
122 $arg = array_slice(func_get_args(), 1);
123 }
124 vfprintf(STDERR, "ERROR: $fmt", $arg);
125 }
126
127 /**
128 * Output command line help message
129 * @param string $prog
130 */
131 public function help($prog) {
132 print new Args\Help($prog, $this->args);
133 }
134
135 /**
136 * Verbosity
137 * @return boolean
138 */
139 public function verbosity() {
140 if ($this->args->verbose) {
141 return true;
142 } elseif ($this->args->quiet) {
143 return false;
144 } else {
145 return null;
146 }
147 }
148 }