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
}
/**
- * 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;
}
}
}
- /**
- * 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
--- /dev/null
+<?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
*/
class Installer implements Command
{
- /**
- * Command line arguments
- * @var pharext\CliArgs
- */
- private $args;
+ use CliCommand;
/**
* Create the 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);
}
*/
class Packager implements Command
{
- /**
- * Command line arguments
- * @var pharext\CliArgs
- */
- private $args;
+ use CliCommand;
/**
* Extension source directory
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",
["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],
-]);
+ ]);
}
/**
* @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);
}
--- /dev/null
+<?php
+
+namespace pharext;
+
+const VERSION = "@PHAREXT_VERSION@";
$spec["-".$arg[0]] = $arg;
$spec["--".$arg[1]] = $arg;
}
- $this->assertSame($args->getSpec(), $spec);
+ $this->assertSame($args->getCompiledSpec(), $spec);
}
public function testParseNothing() {
$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");
- }
-
}
--- /dev/null
+<?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");
+ }
+}