From 26683702fdc53d2431ae2bc5081439ac12685d1b Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Sun, 8 Mar 2015 20:42:03 +0100 Subject: [PATCH] reorder code and files, remove the filtered source dir implementation --- Makefile | 2 +- bin/pharext | Bin 39819 -> 38388 bytes src/pharext/{CliArgs.php => Cli/Args.php} | 4 +- .../{CliCommand.php => Cli/Command.php} | 11 +- src/pharext/FilteredSourceDir.php | 97 ------------------ src/pharext/Installer.php | 2 + src/pharext/Openssl/PrivateKey.php | 25 ++++- src/pharext/Packager.php | 33 +++--- src/pharext/PharextSourceDir.php | 32 ------ .../{GitSourceDir.php => SourceDir/Git.php} | 7 +- .../{PeclSourceDir.php => SourceDir/Pecl.php} | 11 +- src/pharext/SourceDir/Pharext.php | 61 +++++++++++ src/pharext_install.tpl.php | 2 + src/pharext_packager.php | 1 - tests/src/pharext/FilteredSourceDirTest.php | 41 -------- 15 files changed, 126 insertions(+), 203 deletions(-) rename src/pharext/{CliArgs.php => Cli/Args.php} (99%) rename src/pharext/{CliCommand.php => Cli/Command.php} (94%) delete mode 100644 src/pharext/FilteredSourceDir.php delete mode 100644 src/pharext/PharextSourceDir.php rename src/pharext/{GitSourceDir.php => SourceDir/Git.php} (90%) rename src/pharext/{PeclSourceDir.php => SourceDir/Pecl.php} (94%) create mode 100644 src/pharext/SourceDir/Pharext.php delete mode 100644 tests/src/pharext/FilteredSourceDirTest.php diff --git a/Makefile b/Makefile index 1027186..cc88f70 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ all: bin/pharext -bin/pharext: src/* src/pharext/* +bin/pharext: src/* src/pharext/* src/pharext/*/* @echo "Linting changed source files ... " @for file in $?; do php -l $$file | sed -ne '/^No syntax errors/!p' && exit $${PIPESTATUS[0]}; done @echo "Creating bin/pharext ... " diff --git a/bin/pharext b/bin/pharext index 76396714bb29123affc8317d1e3c3176596f018f..c0c47e4762f0c695e129d539cfc19ab25de5214d 100755 GIT binary patch delta 1462 zcmb_cU1$_n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Hq4_-yRqSgTv_lmNh zY3M%M5R4C2@*3J_snWoz07ghES7ltrims8|!WJ@6FRbC_M7fs7b|8%e(1xHD6pf(0 zY^*5h@hIDi*jNd2WA_h}O+!&~@aQV?SzY%rRhU&pPb7F=tSFkU)87)1k8RcXA4cs)AnXHK7d67i)D*$7M&KNNneFd~%x4CTCvABzJJ= z*aNU*yQZeN%nZe+(#;7jok>kT&|rFdI?HdHoS8PAM(C41BE#?~R8%a8B^0*WAQcrj zuv=mO*^dMbg{(gjG*8W?0t!O|F^`*blBR26)~qmYqu7XzV?ugK()vahn=PoSQ0L27 zE#mMpT`!`38O8<()o`Jvh&l|C**wP|!_Yv$nyyM^s%h$G8Pt>}rPXGEAk(B5_4P;4 z$O!6=ci%I&>|<~m?WXn9Ps5ff)@!PQXarl;=%q7*SC;1_%cRny6C9r&&rD8ne1_Y@ zPoy*4v_l4{L2GuO+mW5gSz0St#H4{CJEMNGW#|bQHsRUy;^f-n?#zdmfw$#(4dWH( ziG(1ExT=$z*f(hX3|pm!3JNsI?(iX851U=09g;L2-mfqKOp?fr`9AWO+Sat*5lJbc zE^VIz6F?DW8_f3Dy(tK?hUqCi3{_SYl@5(&9G?$) z$k_vJ#&zymhw;aO_uOO%%jC-V1>;45bN8C4gGhoq1q*ppDImRsp`Wnc1T~MWH_pDa z$jSV3wlvSO{}=R(HPv5a2E?Ixt*Ud&fz=mwq@IfyjzNruZsh#m+m+7aImze zedUe;MQUwo6l8f;5Xq^NtU=!R(_@@}^Hd8F-`Y=3znu&W hbPpUm`t67PKPAU@ex<&Yd*w#kby%dX@vS?y{s&6?)sO%H diff --git a/src/pharext/CliArgs.php b/src/pharext/Cli/Args.php similarity index 99% rename from src/pharext/CliArgs.php rename to src/pharext/Cli/Args.php index 717bc00..49d85ab 100644 --- a/src/pharext/CliArgs.php +++ b/src/pharext/Cli/Args.php @@ -1,11 +1,11 @@ \n\n", VERSION); + printf("pharext v%s (c) Michael Wallner \n\n", + \pharext\VERSION); } /** diff --git a/src/pharext/FilteredSourceDir.php b/src/pharext/FilteredSourceDir.php deleted file mode 100644 index 859df5c..0000000 --- a/src/pharext/FilteredSourceDir.php +++ /dev/null @@ -1,97 +0,0 @@ -cmd = $cmd; - $this->path = $path; - parent::__construct( - new \RecursiveIteratorIterator( - new \RecursiveDirectoryIterator($path, - \FilesystemIterator::KEY_AS_PATHNAME | - \FilesystemIterator::CURRENT_AS_FILEINFO | - \FilesystemIterator::SKIP_DOTS - ) - ) - ); - foreach ([".gitignore", ".hgignore"] as $ignore) { - if (file_exists("$path/$ignore")) { - $this->filter = array_merge($this->filter, - array_map(function($pat) { - $pat = trim($pat); - if (substr($pat, -1) == '/') { - $pat .= '*'; - } - return $pat; - }, file("$path/$ignore", - FILE_IGNORE_NEW_LINES | - FILE_SKIP_EMPTY_LINES - )) - ); - } - } - } - - /** - * @inheritdoc - * @see \pharext\SourceDir::getBaseDir() - */ - public function getBaseDir() { - return $this->path; - } - - /** - * Implements FilterIterator - * @see FilterIterator::accept() - */ - public function accept() { - $fn = $this->key(); - if (is_dir($fn)) { - if ($this->cmd->getArgs()->verbose) { - $this->info("Excluding %s\n", $fn); - } - return false; - } - $pl = strlen($this->path) + 1; - $pn = substr($this->key(), $pl); - foreach ($this->filter as $pat) { - if (fnmatch($pat, $pn)) { - if ($this->cmd->getArgs()->verbose) { - $this->info("Excluding %s\n", $pn); - } - return false; - } - } - if ($this->cmd->getArgs()->verbose) { - $this->info("Packaging %s\n", $pn); - } - return true; - } -} \ No newline at end of file diff --git a/src/pharext/Installer.php b/src/pharext/Installer.php index ed82211..589abd6 100644 --- a/src/pharext/Installer.php +++ b/src/pharext/Installer.php @@ -3,6 +3,8 @@ namespace pharext; use Phar; +use pharext\Cli\Args as CliArgs; +use pharext\Cli\Command as CliCommand; /** * The extension install command executed by the extension phar diff --git a/src/pharext/Openssl/PrivateKey.php b/src/pharext/Openssl/PrivateKey.php index 1d3aed1..470983d 100644 --- a/src/pharext/Openssl/PrivateKey.php +++ b/src/pharext/Openssl/PrivateKey.php @@ -4,19 +4,38 @@ namespace pharext\Openssl; class PrivateKey { + /** + * OpenSSL pkey resource + * @var resource + */ private $key; - + + /** + * Read a private key + * @param string $file + * @param string $password + * @throws \Exception + */ function __construct($file, $password) { $this->key = openssl_pkey_get_private("file://$file", $password); if (!is_resource($this->key)) { throw new \Exception("Could not load private key"); } } - + + /** + * Sign the PHAR + * @param \Phar $package + */ function sign(\Phar $package) { $package->setSignatureAlgorithm(\Phar::OPENSSL, $this->key); } - + + /** + * Export the public key to a file + * @param string $file + * @throws \Exception + */ function exportPublicKey($file) { if (!file_put_contents("$file.tmp", openssl_pkey_get_details($this->key)["key"]) || !rename("$file.tmp", $file) diff --git a/src/pharext/Packager.php b/src/pharext/Packager.php index 28b7050..f268765 100644 --- a/src/pharext/Packager.php +++ b/src/pharext/Packager.php @@ -3,6 +3,8 @@ namespace pharext; use Phar; +use pharext\Cli\Args as CliArgs; +use pharext\Cli\Command as CliCommand; /** * The extension packaging command executed by bin/pharext @@ -24,8 +26,6 @@ class Packager implements Command $this->args = new CliArgs([ ["h", "help", "Display this help", CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT], - [null, "signature", "Dump signature", - CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT], ["v", "verbose", "More output", CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG], ["q", "quiet", "Less output", @@ -36,9 +36,9 @@ class Packager implements Command 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", + ["g", "git", "Use `git ls-tree` to determine file list", CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG], - ["p", "pecl", "Use PECL package.xml instead of the standard ignore filter", + ["p", "pecl", "Use PECL package.xml to determine file list, name and release", CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG], ["d", "dest", "Destination directory", CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG, @@ -47,8 +47,10 @@ class Packager implements Command CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG], ["Z", "bzip", "Create additional PHAR compressed with bzip", CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG], - ["S", "sign", "Sign the *.ext.phar with a private key", - CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG] + ["S", "sign", "Sign the PHAR with a private key", + CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::REQARG], + [null, "signature", "Dump signature", + CliArgs::OPTIONAL|CliArgs::SINGLE|CliArgs::NOARG|CliArgs::HALT], ]); } @@ -75,13 +77,11 @@ class Packager implements Command try { if ($this->args["source"]) { if ($this->args["pecl"]) { - $this->source = new PeclSourceDir($this, $this->args["source"]); + $this->source = new SourceDir\Pecl($this, $this->args["source"]); } elseif ($this->args["git"]) { - $this->source = new GitSourceDir($this, $this->args["source"]); - } elseif (realpath($this->args["source"]."/pharext_package.php")) { - $this->source = new PharextSourceDir($this, $this->args["source"]); + $this->source = new SourceDir\Git($this, $this->args["source"]); } else { - $this->source = new FilteredSourceDir($this, $this->args["source"]); + $this->source = new SourceDir\Pharext($this, $this->args["source"]); } } } catch (\Exception $e) { @@ -126,10 +126,11 @@ class Packager implements Command * @return Generator */ private function bundle() { - foreach (scandir(__DIR__) as $entry) { - if (fnmatch("*.php", $entry)) { - yield "pharext/$entry" => __DIR__."/$entry"; - } + $rdi = new \RecursiveDirectoryIterator(__DIR__); + $rii = new \RecursiveIteratorIterator($rdi); + for ($rii->rewind(); $rii->valid(); $rii->next()) { + yield "pharext/". $rii->getSubPathname() => $rii->key(); + } } @@ -167,7 +168,7 @@ class Packager implements Command $package->startBuffering(); $package->buildFromIterator($this->source, $this->source->getBaseDir()); - $package->buildFromIterator($this->bundle()); + $package->buildFromIterator($this->bundle(__DIR__)); $package->addFile(__DIR__."/../pharext_installer.php", "pharext_installer.php"); $package->setDefaultStub("pharext_installer.php"); $package->setStub("#!/usr/bin/php -dphar.readonly=1\n".$package->getStub()); diff --git a/src/pharext/PharextSourceDir.php b/src/pharext/PharextSourceDir.php deleted file mode 100644 index e0d9a2a..0000000 --- a/src/pharext/PharextSourceDir.php +++ /dev/null @@ -1,32 +0,0 @@ -cmd = $cmd; - $this->path = $path; - - $callable = include "$path/pharext_package.php"; - if (!is_callable($callable)) { - throw new \Exception("Package hook did not return a callable"); - } - $this->iter = $callable($cmd, $path); - } - - public function getBaseDir() { - return $this->path; - } - - public function getIterator() { - if (!is_callable($this->iter)) { - throw new \Exception("Package hook callback did not return a callable"); - } - return call_user_func($this->iter, $this->cmd, $this->path); - } -} \ No newline at end of file diff --git a/src/pharext/GitSourceDir.php b/src/pharext/SourceDir/Git.php similarity index 90% rename from src/pharext/GitSourceDir.php rename to src/pharext/SourceDir/Git.php index f18f97f..a16b1e6 100644 --- a/src/pharext/GitSourceDir.php +++ b/src/pharext/SourceDir/Git.php @@ -1,11 +1,14 @@ sxe->xpath("/pecl:package/pecl:dependencies/pecl:required/pecl:package"); @@ -115,7 +118,7 @@ class PeclSourceDir implements \IteratorAggregate, SourceDir self::loadHook($configure, $dependencies); ob_end_flush(); rewind($fd); - yield "pharext_install.php" => $fd; + yield "pharext_install.php" => $fd; } } diff --git a/src/pharext/SourceDir/Pharext.php b/src/pharext/SourceDir/Pharext.php new file mode 100644 index 0000000..2675a78 --- /dev/null +++ b/src/pharext/SourceDir/Pharext.php @@ -0,0 +1,61 @@ +cmd = $cmd; + $this->path = $path; + + $callable = include "$path/pharext_package.php"; + if (!is_callable($callable)) { + throw new \Exception("Package hook did not return a callable"); + } + $this->iter = $callable($cmd, $path); + } + + /** + * @inheritdoc + * @see \pharext\SourceDir::getBaseDir() + */ + public function getBaseDir() { + return $this->path; + } + + /** + * Implements IteratorAggregate + * @see IteratorAggregate::getIterator() + */ + public function getIterator() { + if (!is_callable($this->iter)) { + return new Git($this->cmd, $this->path); + } + return call_user_func($this->iter, $this->cmd, $this->path); + } +} diff --git a/src/pharext_install.tpl.php b/src/pharext_install.tpl.php index 9bf8271..04d84b2 100644 --- a/src/pharext_install.tpl.php +++ b/src/pharext_install.tpl.php @@ -5,6 +5,8 @@ */ namespace pharext; +use pharext\Cli\Args as CliArgs; + return function(Installer $installer) { $args = $installer->getArgs(); diff --git a/src/pharext_packager.php b/src/pharext_packager.php index 125db61..f85c2f1 100644 --- a/src/pharext_packager.php +++ b/src/pharext_packager.php @@ -2,7 +2,6 @@ /** * The packager sub-stub for bin/pharext */ - spl_autoload_register(function($c) { return include strtr($c, "\\_", "//") . ".php"; }); diff --git a/tests/src/pharext/FilteredSourceDirTest.php b/tests/src/pharext/FilteredSourceDirTest.php deleted file mode 100644 index c561583..0000000 --- a/tests/src/pharext/FilteredSourceDirTest.php +++ /dev/null @@ -1,41 +0,0 @@ -args = new CliArgs; - } - function run($argc, array $argv) { - } -} - -class FilteredSourceDirTest extends \PHPUnit_Framework_TestCase -{ - /** - * @var FilteredSourceDir - */ - protected $source; - - protected function setUp() { - $this->source = new FilteredSourceDir(new Cmd2, "."); - } - - public function testIterator() { - $gitdir = new GitSourceDir(new Cmd2, "."); - $gitfiles = iterator_to_array($gitdir); - sort($gitfiles); - - $filtered = array_values(iterator_to_array($this->source)); - $fltfiles = array_map(function($fi) { - return $fi->getRealpath(); - }, $filtered); - sort($fltfiles); - $this->assertEquals(array(), array_diff($gitfiles, $fltfiles)); - $this->assertEquals($gitfiles, $fltfiles); - } -} \ No newline at end of file -- 2.30.2