major refactoring under the hood
[pharext/pharext] / src / pharext / Cli / Command.php
index e19ce93a444ffd3d4d87a2be90ed589f897e86a8..0cc0bb40f2b8649cc06de21d577c7b76728f2a44 100644 (file)
@@ -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 <mike@php.net>\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"));
+               }
        }
        
        /**
@@ -50,6 +89,22 @@ trait Command
                }
        }
 
+       /**
+        * @inheritdoc
+        * @see \pharext\Command::warn()
+        */
+       public function warn($fmt) {
+               if (!$this->args->quiet) {
+                       if (!isset($fmt)) {
+                               $fmt = "%s\n";
+                               $arg = error_get_last()["message"];
+                       } else {
+                               $arg = array_slice(func_get_args(), 1);
+                       }
+                       vfprintf(STDERR, "Warning: $fmt", $arg);
+               }
+       }
+
        /**
         * @inheritdoc
         * @see \pharext\Command::error()
@@ -138,11 +193,11 @@ trait Command
                        } elseif (is_dir("$dir/$entry")) {
                                $this->rm("$dir/$entry");
                        } elseif (!unlink("$dir/$entry")) {
-                               $this->error(null);
+                               $this->warn(null);
                        }
                }
                if (!rmdir($dir)) {
-                       $this->error(null);
+                       $this->warn(null);
                }
        }
 }