X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Fpharext%2FCli%2FCommand.php;h=6ad2c806b4a4ccabe3602f1b5ed8c836e5ca6423;hb=e990b6dabecbdaf98b8d8b2173b0d697f9b2b754;hp=e19ce93a444ffd3d4d87a2be90ed589f897e86a8;hpb=8b991a8b633610f7309d76f15b388bffd073d1f7;p=pharext%2Fpharext diff --git a/src/pharext/Cli/Command.php b/src/pharext/Cli/Command.php index e19ce93..6ad2c80 100644 --- a/src/pharext/Cli/Command.php +++ b/src/pharext/Cli/Command.php @@ -4,7 +4,21 @@ namespace pharext\Cli; use pharext\Cli\Args as CliArgs; -require_once "pharext/Version.php"; +use Phar; + +if (!function_exists("array_column")) { + function array_column(array $array, $col, $idx = null) { + $result = []; + foreach ($array as $el) { + if (isset($idx)) { + $result[$el[$idx]] = $el[$col]; + } else { + $result[] = $el[$col]; + } + } + return $result; + } +} trait Command { @@ -22,12 +36,37 @@ trait Command return $this->args; } + /** + * Retrieve metadata of the currently running phar + * @param string $key + * @return mixed + */ + public function metadata($key = null) { + $running = new Phar(Phar::running(false)); + + if ($key === "signature") { + $sig = $running->getSignature(); + return sprintf("%s signature of %s\n%s", + $sig["hash_type"], + $this->metadata("name"), + chunk_split($sig["hash"], 64, "\n")); + } + + $metadata = $running->getMetadata(); + if (isset($key)) { + return $metadata[$key]; + } + return $metadata; + } + /** * Output pharext vX.Y.Z header */ - function header() { - printf("pharext v%s (c) Michael Wallner \n\n", - \pharext\VERSION); + public function header() { + if (!headers_sent()) { + /* only display header, if we didn't generate any output yet */ + printf("%s\n\n", $this->metadata("header")); + } } /** @@ -52,9 +91,9 @@ trait Command /** * @inheritdoc - * @see \pharext\Command::error() + * @see \pharext\Command::warn() */ - public function error($fmt) { + public function warn($fmt) { if (!$this->args->quiet) { if (!isset($fmt)) { $fmt = "%s\n"; @@ -62,8 +101,22 @@ trait Command } else { $arg = array_slice(func_get_args(), 1); } - vfprintf(STDERR, "ERROR: $fmt", $arg); + vfprintf(STDERR, "Warning: $fmt", $arg); + } + } + + /** + * @inheritdoc + * @see \pharext\Command::error() + */ + public function error($fmt) { + if (!isset($fmt)) { + $fmt = "%s\n"; + $arg = error_get_last()["message"]; + } else { + $arg = array_slice(func_get_args(), 1); } + vfprintf(STDERR, "ERROR: $fmt", $arg); } /** @@ -128,21 +181,16 @@ trait Command } /** - * rm -r - * @param string $dir + * Verbosity + * @return boolean */ - private function rm($dir) { - foreach (scandir($dir) as $entry) { - if ($entry === "." || $entry === "..") { - continue; - } elseif (is_dir("$dir/$entry")) { - $this->rm("$dir/$entry"); - } elseif (!unlink("$dir/$entry")) { - $this->error(null); - } - } - if (!rmdir($dir)) { - $this->error(null); + public function verbosity() { + if ($this->args->verbose) { + return true; + } elseif ($this->args->quiet) { + return false; + } else { + return null; } } }