refactor some commonly used code into a trait
authorMichael Wallner <mike@php.net>
Thu, 5 Mar 2015 14:27:09 +0000 (15:27 +0100)
committerMichael Wallner <mike@php.net>
Thu, 5 Mar 2015 14:27:09 +0000 (15:27 +0100)
Makefile
bin/pharext
src/pharext/CliArgs.php
src/pharext/CliCommand.php [new file with mode: 0644]
src/pharext/Installer.php
src/pharext/Packager.php
src/pharext/Version.php [new file with mode: 0644]
tests/src/pharext/CliArgsTest.php
tests/src/pharext/CliCommandTest.php [new file with mode: 0644]

index 3f00b50b50f7a3513aee4004f8dc5fe32225ba2e..cf19520418200fc554b77b8694d01a6fbd36859e 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -5,11 +5,16 @@
 all: bin/pharext
 
 bin/pharext: src/* src/pharext/*
+       @for file in $?; do php -l $$file | sed -ne '/^No syntax errors/!p' && exit $${PIPESTATUS[0]}; done
+       phpunit tests
        php -d phar.readonly=0 build/create-phar.php
        chmod +x $@
 
+test:
+       phpunit tests
+       
 clean:
        rm bin/pharext*
 
-.PHONY: all clean
-       
+.PHONY: all clean test
+.SUFFIXES: .php
\ No newline at end of file
index 1d0ed19cfaa22712c9e73d3f8071c7730db041ed..222569dcaec5e47649e0d94fb6c282067ef8d059 100755 (executable)
Binary files a/bin/pharext and b/bin/pharext differ
index f720b3c1d840964642246fac1e07b31f7c9fc911..1f07e4463ead32319d6dd641feae46badc9104ac 100644 (file)
@@ -89,10 +89,18 @@ class CliArgs implements \ArrayAccess
        }
        
        /**
-        * Get compiled spec
+        * Get original spec
         * @return array
         */
        public function getSpec() {
+               return $this->orig;
+       }
+       
+       /**
+        * Get compiled spec
+        * @return array
+        */
+       public function getCompiledSpec() {
                return $this->spec;
        }
        
@@ -157,48 +165,6 @@ class CliArgs implements \ArrayAccess
                }
        }
        
-       /**
-        * Output command line help message
-        * @param string $prog
-        */
-       public function help($prog) {
-               printf("\nUsage:\n\n  $ %s", $prog);
-               $flags = [];
-               $required = [];
-               $optional = [];
-               foreach ($this->orig as $spec) {
-                       if ($spec[3] & self::REQARG) {
-                               if ($spec[3] & self::REQUIRED) {
-                                       $required[] = $spec;
-                               } else {
-                                       $optional[] = $spec;
-                               }
-                       } else {
-                               $flags[] = $spec;
-                       }
-               }
-               
-               if ($flags) {
-                       printf(" [-%s]", implode("|-", array_column($flags, 0)));
-               }
-               foreach ($required as $req) {
-                       printf(" -%s <arg>", $req[0]);
-               }
-               if ($optional) {
-                       printf(" [-%s <arg>]", implode("|-", array_column($optional, 0)));
-               } 
-               printf("\n\n");
-               foreach ($this->orig as $spec) {
-                       printf("    -%s|--%s %s", $spec[0], $spec[1], ($spec[3] & self::REQARG) ? "<arg>  " : (($spec[3] & self::OPTARG) ? "[<arg>]" : "       "));
-                       printf("%s%s %s", str_repeat(" ", 16-strlen($spec[1])), $spec[2], ($spec[3] & self::REQUIRED) ? "(REQUIRED)" : "");
-                       if (isset($spec[4])) {
-                               printf(" [%s]", $spec[4]);
-                       }
-                       printf("\n");
-               }
-               printf("\n");
-       }
-       
        /**
         * Retreive the default argument of an option
         * @param string $o
diff --git a/src/pharext/CliCommand.php b/src/pharext/CliCommand.php
new file mode 100644 (file)
index 0000000..1299a0e
--- /dev/null
@@ -0,0 +1,66 @@
+<?php
+
+namespace pharext;
+
+require_once __DIR__."/Version.php";
+
+trait CliCommand
+{
+       /**
+        * Command line arguments
+        * @var pharext\CliArgs
+        */
+       private $args;
+       
+       /**
+        * Output pharext vX.Y.Z header
+        */
+       function header() {
+               printf("pharext v%s (c) Michael Wallner <mike@php.net>\n", VERSION);
+       }
+       
+       /**
+        * Output command line help message
+        * @param string $prog
+        */
+       public function help($prog) {
+               printf("\nUsage:\n\n  \$ %s", $prog);
+               
+               $flags = [];
+               $required = [];
+               $optional = [];
+               foreach ($this->args->getSpec() as $spec) {
+                       if ($spec[3] & CliArgs::REQARG) {
+                               if ($spec[3] & CliArgs::REQUIRED) {
+                                       $required[] = $spec;
+                               } else {
+                                       $optional[] = $spec;
+                               }
+                       } else {
+                               $flags[] = $spec;
+                       }
+               }
+       
+               if ($flags) {
+                       printf(" [-%s]", implode("", array_column($flags, 0)));
+               }
+               foreach ($required as $req) {
+                       printf(" -%s <arg>", $req[0]);
+               }
+               if ($optional) {
+                       printf(" [-%s <arg>]", implode("|-", array_column($optional, 0)));
+               }
+               printf("\n\n");
+               foreach ($this->args->getSpec() as $spec) {
+                       printf("    -%s|--%s %s", $spec[0], $spec[1], ($spec[3] & CliArgs::REQARG) ? "<arg>  " : (($spec[3] & CliArgs::OPTARG) ? "[<arg>]" : "       "));
+                       printf("%s%s%s", str_repeat(" ", 16-strlen($spec[1])), $spec[2], ($spec[3] & CliArgs::REQUIRED) ? " (REQUIRED)" : "");
+                       if (isset($spec[4])) {
+                               printf(" [%s]", $spec[4]);
+                       }
+                       printf("\n");
+               }
+               printf("\n");
+       }
+       
+       
+}
\ No newline at end of file
index cd15ba04486c02a5a918da5ecd9fcdb2261ef533..f6c7807063be73616c162e390a7c95fd8b68aacf 100644 (file)
@@ -9,11 +9,7 @@ use Phar;
  */
 class Installer implements Command
 {
-       /**
-        * Command line arguments
-        * @var pharext\CliArgs
-        */
-       private $args;
+       use CliCommand;
        
        /**
         * Create the command
@@ -44,23 +40,31 @@ class Installer implements Command
         * @see \pharext\Command::run()
         */
        public function run($argc, array $argv) {
+               $errs = [];
                $prog = array_shift($argv);
                foreach ($this->args->parse(--$argc, $argv) as $error) {
-                       $this->error("%s\n", $error);
+                       $errs[] = $error;
                }
                
                if ($this->args["help"]) {
-                       $this->args->help($prog);
+                       $this->header();
+                       $this->help($prog);
                        exit;
                }
                
                foreach ($this->args->validate() as $error) {
-                       $this->error("%s\n", $error);
+                       $errs[] = $error;
                }
                
-               if (isset($error)) {
+               if ($errs) {
+                       if (!$this->args["quiet"]) {
+                               $this->header();
+                       }
+                       foreach ($errs as $err) {
+                               $this->error("%s\n", $err);
+                       } 
                        if (!$this->args["quiet"]) {
-                               $this->args->help($prog);
+                               $this->help($prog);
                        }
                        exit(1);
                }
index a848e2d39ce672fc5a0ff67dfb652376d526c2e8..b6b05e1ac6b467f1b6f19acc57bc3fffc0ba448e 100644 (file)
@@ -9,11 +9,7 @@ use Phar;
  */
 class Packager implements Command
 {
-       /**
-        * Command line arguments
-        * @var pharext\CliArgs
-        */
-       private $args;
+       use CliCommand;
        
        /**
         * Extension source directory
@@ -32,6 +28,10 @@ class Packager implements Command
                                CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
                        ["q", "quiet", "Less output",
                                CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
+                       ["n", "name", "Extension name",
+                               CliArgs::REQUIRED|CliArgs::SINGLE|CliArgs::REQARG],
+                       ["r", "release", "Extension release version",
+                               CliArgs::REQUIRED|CliArgs::SINGLE|CliArgs::REQARG],
                        ["s", "source", "Extension source directory",
                                CliArgs::REQUIRED|CliArgs::SINGLE|CliArgs::REQARG],
                        ["g", "git", "Use `git ls-files` instead of the standard ignore filter",
@@ -41,15 +41,11 @@ class Packager implements Command
                        ["d", "dest", "Destination directory",
                                CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG,
                                "."],
-                       ["n", "name", "Extension name",
-                               CliArgs::REQUIRED|CliArgs::SINGLE|CliArgs::REQARG],
-                       ["r", "release", "Extension release version",
-                               CliArgs::REQUIRED|CliArgs::SINGLE|CliArgs::REQARG],
                        ["z", "gzip", "Create additional PHAR compressed with gzip",
                                CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
                        ["Z", "bzip", "Create additional PHAR compressed with bzip",
                                CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
-]);
+               ]);
        }
        
        /**
@@ -57,13 +53,15 @@ class Packager implements Command
         * @see \pharext\Command::run()
         */
        public function run($argc, array $argv) {
+               $errs = [];
                $prog = array_shift($argv);
                foreach ($this->args->parse(--$argc, $argv) as $error) {
-                       $this->error("%s\n", $error);
+                       $errs[] = $error;
                }
                
                if ($this->args["help"]) {
-                       $this->args->help($prog);
+                       $this->header();
+                       $this->help($prog);
                        exit;
                }
                
@@ -78,12 +76,18 @@ class Packager implements Command
                }
                
                foreach ($this->args->validate() as $error) {
-                       $this->error("%s\n", $error);
+                       $errs[] = $error;
                }
                
-               if (isset($error)) {
+               if ($errs) {
+                       if (!$this->args["quiet"]) {
+                               $this->header();
+                       }
+                       foreach ($errs as $err) {
+                               $this->error("%s\n", $err);
+                       }
                        if (!$this->args["quiet"]) {
-                               $this->args->help($prog);
+                               $this->help($prog);
                        }
                        exit(1);
                }
diff --git a/src/pharext/Version.php b/src/pharext/Version.php
new file mode 100644 (file)
index 0000000..50a2875
--- /dev/null
@@ -0,0 +1,5 @@
+<?php
+
+namespace pharext;
+
+const VERSION = "@PHAREXT_VERSION@";
index bdfdb1563de87de4ae3b850f6f0bd3e06ac5295d..ff05c8ff8cc6c05e408e1ed70418ef7e0f3f88ba 100644 (file)
@@ -45,7 +45,7 @@ class CliArgsTest extends \PHPUnit_Framework_TestCase
                        $spec["-".$arg[0]] = $arg;
                        $spec["--".$arg[1]] = $arg;
                }
-               $this->assertSame($args->getSpec(), $spec);
+               $this->assertSame($args->getCompiledSpec(), $spec);
        }
 
        public function testParseNothing() {
@@ -127,25 +127,4 @@ class CliArgsTest extends \PHPUnit_Framework_TestCase
                $this->assertTrue(isset($error));
        }
 
-       public function testHelp() {
-               $this->expectOutputString(<<<EOF
-
-Usage:
-
-  $ testprog [-h|-v|-q|-s] [-p|-n|-c <arg>]
-
-    -h|--help                    Display help 
-    -v|--verbose                 More output 
-    -q|--quiet                   Less output 
-    -p|--prefix <arg>            PHP installation prefix if phpize is not in \$PATH, e.g. /opt/php7 
-    -n|--common-name <arg>       PHP common program name, e.g. php5 or zts-php  [php]
-    -c|--configure <arg>         Additional extension configure flags, e.g. -c --with-flag 
-    -s|--sudo [<arg>]            Installation might need increased privileges  [sudo -S %s]
-
-
-EOF
-               );
-               $this->args->help("testprog");
-       }
-
 }
diff --git a/tests/src/pharext/CliCommandTest.php b/tests/src/pharext/CliCommandTest.php
new file mode 100644 (file)
index 0000000..a2bf3d9
--- /dev/null
@@ -0,0 +1,56 @@
+<?php
+
+namespace pharext;
+
+class CliCommandTest extends \PHPUnit_Framework_TestCase
+{
+       use CliCommand;
+       
+       /**
+        * @var array
+        */
+       protected $spec;
+
+       protected function setUp() {
+               $this->spec = [
+               ["h", "help", "Display help",
+                       CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT],
+               ["v", "verbose", "More output",
+                       CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
+               ["q", "quiet", "Less output",
+                       CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG],
+               ["p", "prefix", "PHP installation prefix if phpize is not in \$PATH, e.g. /opt/php7",
+                       CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG],
+               ["n", "common-name", "PHP common program name, e.g. php5 or zts-php",
+                       CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG,
+                       "php"],
+               ["c", "configure", "Additional extension configure flags, e.g. -c --with-flag",
+                       CliArgs::OPTIONAL|CliArgs::MULTI|CliArgs::REQARG],
+               ["s", "sudo", "Installation might need increased privileges",
+                       CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::OPTARG,
+                       "sudo -S %s"]
+               ];
+               $this->args = new CliArgs($this->spec);
+       }
+       
+       public function testHelp() {
+               $this->expectOutputString(<<<EOF
+
+Usage:
+
+  $ testprog [-hvqs] [-p|-n|-c <arg>]
+
+    -h|--help                    Display help
+    -v|--verbose                 More output
+    -q|--quiet                   Less output
+    -p|--prefix <arg>            PHP installation prefix if phpize is not in \$PATH, e.g. /opt/php7
+    -n|--common-name <arg>       PHP common program name, e.g. php5 or zts-php [php]
+    -c|--configure <arg>         Additional extension configure flags, e.g. -c --with-flag
+    -s|--sudo [<arg>]            Installation might need increased privileges [sudo -S %s]
+
+
+EOF
+               );
+               $this->help("testprog");
+       }
+}