simplify
authorMichael Wallner <mike@php.net>
Fri, 6 Mar 2015 12:29:33 +0000 (13:29 +0100)
committerMichael Wallner <mike@php.net>
Fri, 6 Mar 2015 12:29:33 +0000 (13:29 +0100)
src/pharext/CliCommand.php
src/pharext/Packager.php

index f4f29b6ab10d6d58aace00977c238486f574e98d..46ead1ccd977db7b03893ee9e00e4d128278dec3 100644 (file)
@@ -12,6 +12,14 @@ trait CliCommand
         */
        private $args;
        
+       /**
+        * @inheritdoc
+        * @see \pharext\Command::getArgs()
+        */
+       public function getArgs() {
+               return $this->args;
+       }
+
        /**
         * Output pharext vX.Y.Z header
         */
@@ -19,6 +27,32 @@ trait CliCommand
                printf("pharext v%s (c) Michael Wallner <mike@php.net>\n\n", VERSION);
        }
        
+       /**
+        * @inheritdoc
+        * @see \pharext\Command::info()
+        */
+       public function info($fmt) {
+               if (!$this->args->quiet) {
+                       vprintf($fmt, array_slice(func_get_args(), 1));
+               }
+       }
+
+       /**
+        * @inheritdoc
+        * @see \pharext\Command::error()
+        */
+       public function error($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, "ERROR: $fmt", $arg);
+               }
+       }
+
        /**
         * Output command line help message
         * @param string $prog
@@ -80,5 +114,15 @@ trait CliCommand
                printf("\n");
        }
        
-       
+       /**
+        * Create temporary file/directory name
+        * @param string $prefix
+        * @param string $suffix
+        */
+       private function tempname($prefix, $suffix = null) {
+               if (!isset($suffix)) {
+                       $suffix = uniqid();
+               }
+               return sprintf("%s/%s.%s", sys_get_temp_dir(), $prefix, $suffix);
+       }
 }
\ No newline at end of file
index 5401b4c269f84f7f2190c60b372b1caeb84e82b8..0b133e357909f937b75d8244f8d3899ac4df7d45 100644 (file)
@@ -97,34 +97,6 @@ class Packager implements Command
                $this->createPackage();
        }
        
-       /**
-        * @inheritdoc
-        * @see \pharext\Command::getArgs()
-        */
-       public function getArgs() {
-               return $this->args;
-       }
-       
-       /**
-        * @inheritdoc
-        * @see \pharext\Command::info()
-        */
-       public function info($fmt) {
-               if (!$this->args->quiet) {
-                       vprintf($fmt, array_slice(func_get_args(), 1));
-               }
-       }
-       
-       /**
-        * @inheritdoc
-        * @see \pharext\Command::error()
-        */
-       public function error($fmt) {
-               if (!$this->args->quiet) {
-                       vfprintf(STDERR, "ERROR: $fmt", array_slice(func_get_args(), 1));
-               }
-       }
-       
        /**
         * Traverses all pharext source files to bundle
         * @return Generator
@@ -142,7 +114,7 @@ class Packager implements Command
         */
        private function createPackage() {
                $pkguniq = uniqid();
-               $pkgtemp = sys_get_temp_dir() ."/{$pkguniq}.phar";
+               $pkgtemp = $this->tempname($pkguniq, "phar");
                $pkgdesc = "{$this->args->name}-{$this->args->release}";
        
                $this->info("Creating phar %s ...%s", $pkgtemp, $this->args->verbose ? "\n" : " ");
@@ -156,8 +128,9 @@ class Packager implements Command
                        $package->setStub("#!/usr/bin/php -dphar.readonly=1\n".$package->getStub());
                        $package->stopBuffering();
                        
-                       chmod($pkgtemp, 0770);
-                       if ($this->args->verbose) {
+                       if (!chmod($pkgtemp, 0777)) {
+                               $this->error(null);
+                       } elseif ($this->args->verbose) {
                                $this->info("Created executable phar %s\n", $pkgtemp);
                        } else {
                                $this->info("OK\n");
@@ -192,7 +165,7 @@ class Packager implements Command
                        $pkgname = $this->args->dest ."/". basename($pkgfile);
                        $this->info("Finalizing %s ... ", $pkgname);
                        if (!rename($pkgtemp, $pkgname)) {
-                               $this->error("%s\n", error_get_last()["message"]);
+                               $this->error(null);
                                exit(5);
                        }
                        $this->info("OK\n");