From 35f1191ab8ca06052eb08e05322c5d7ff87187dd Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Fri, 6 Mar 2015 13:29:33 +0100 Subject: [PATCH 1/1] simplify --- src/pharext/CliCommand.php | 46 +++++++++++++++++++++++++++++++++++++- src/pharext/Packager.php | 37 +++++------------------------- 2 files changed, 50 insertions(+), 33 deletions(-) diff --git a/src/pharext/CliCommand.php b/src/pharext/CliCommand.php index f4f29b6..46ead1c 100644 --- a/src/pharext/CliCommand.php +++ b/src/pharext/CliCommand.php @@ -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 \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 diff --git a/src/pharext/Packager.php b/src/pharext/Packager.php index 5401b4c..0b133e3 100644 --- a/src/pharext/Packager.php +++ b/src/pharext/Packager.php @@ -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"); -- 2.30.2