From: Marcus Boerger Date: Thu, 10 May 2007 23:14:00 +0000 (+0000) Subject: - Add extract command X-Git-Tag: RELEASE_1_2_0~108 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b032d36ce5b8c18ffb3d22bc7085a533ec2a27b6;p=php - Add extract command --- diff --git a/ext/phar/phar.php b/ext/phar/phar.php index 426439e3ae..bcb96080e6 100644 --- a/ext/phar/phar.php +++ b/ext/phar/phar.php @@ -174,7 +174,7 @@ if (!class_exists('CLICommand')) } } - call_user_func(array($this, $this->cmds[$command]['run'])); + call_user_func(array($this, $this->cmds[$command]['run']), $this->args); } static function getSubFuncs(CLICommand $cmdclass, $prefix, array $subs) @@ -230,6 +230,17 @@ if (!class_exists('CLICommand')) return $arg; } + static function cli_arg_typ_dir($arg) + { + $f = realpath($arg); + if ($f===false || !file_exists($f) || !is_dir($f)) + { + echo "Requested path '$arg' does not exist.\n"; + exit(1); + } + return $f; + } + static function cli_arg_typ_file($arg) { $f = realpath($arg); @@ -287,7 +298,7 @@ EOF; $sp = str_repeat(' ', $l+2); foreach($this->cmds as $name => $funcs) { - $inf = sprintf("%${l}s ", $name); + $inf = $name . substr($sp, strlen($name)); if (isset($funcs['inf'])) { $inf .= call_user_func(array($this, $funcs['inf'])) . "\n"; @@ -539,6 +550,64 @@ class PharCommand extends CLICommand echo "$f\n"; } } + + static function cli_cmd_inf_extract() + { + return "Extract a PHAR package to a directory."; + } + + static function cli_cmd_arg_extract() + { + return array( + 'f' => array('type'=>'phar', 'val'=>NULL, 'required'=>1, 'inf'=>' Specifies the PHAR file to extract.'), + '' => array('type'=>'dir', 'val'=>'.', 'inf'=>' Directory to extract to (defaults to \'.\''), + ); + } + + static function cli_cmd_run_extract($args) + { + $dir = $args['']['val']; + if (is_array($dir)) + { + if (count($dir) != 1) + { + echo "Only one target directory allowed.\n"; + exit(1); + } + else + { + $dir = $dir[0]; + } + } + $phar = $args['f']['val']; + $base = $phar->getPathname(); + $bend = strpos($base, '.phar'); + $bend = strpos($base, '/', $bend); + $base = substr($base, 0, $bend + 1); + $blen = strlen($base); + foreach(new RecursiveIteratorIterator($phar) as $pn => $f) + { + $sub = substr($pn, $blen); + $target = $dir . '/' . $sub; + if (!file_exists(dirname($target))) + { + if (!@mkdir(dirname($target))) + { + echo " ..unable to create dir\n"; + exit(1); + } + } + echo "$sub"; + if (!@copy($f, $target)) + { + echo " ...error\n"; + } + else + { + echo " ...ok\n"; + } + } + } } new PharCommand($argc, $argv); diff --git a/ext/phar/phar/clicommand.inc b/ext/phar/phar/clicommand.inc index f07fcd234f..5a86f32f9d 100755 --- a/ext/phar/phar/clicommand.inc +++ b/ext/phar/phar/clicommand.inc @@ -123,7 +123,7 @@ abstract class CLICommand } } - call_user_func(array($this, $this->cmds[$command]['run'])); + call_user_func(array($this, $this->cmds[$command]['run']), $this->args); } static function getSubFuncs(CLICommand $cmdclass, $prefix, array $subs) @@ -179,6 +179,18 @@ abstract class CLICommand return $arg; } + static function cli_arg_typ_dir($arg) + { + $f = realpath($arg); +echo "DIR: '$arg' = '$f'\n"; + if ($f===false || !file_exists($f) || !is_dir($f)) + { + echo "Requested path '$arg' does not exist.\n"; + exit(1); + } + return $f; + } + static function cli_arg_typ_file($arg) { $f = new SplFileInfo($arg); @@ -237,7 +249,7 @@ Commands: $sp = str_repeat(' ', $l+2); foreach($this->cmds as $name => $funcs) { - $inf = sprintf("%${l}s ", $name); + $inf = $name . substr($sp, strlen($name)); if (isset($funcs['inf'])) { $inf .= call_user_func(array($this, $funcs['inf'])) . "\n"; diff --git a/ext/phar/phar/pharcommand.inc b/ext/phar/phar/pharcommand.inc index f08bbe4756..0458b0d362 100755 --- a/ext/phar/phar/pharcommand.inc +++ b/ext/phar/phar/pharcommand.inc @@ -225,6 +225,64 @@ class PharCommand extends CLICommand echo "$f\n"; } } + + static function cli_cmd_inf_extract() + { + return "Extract a PHAR package to a directory."; + } + + static function cli_cmd_arg_extract() + { + return array( + 'f' => array('type'=>'phar', 'val'=>NULL, 'required'=>1, 'inf'=>' Specifies the PHAR file to extract.'), + '' => array('type'=>'dir', 'val'=>'.', 'inf'=>' Directory to extract to (defaults to \'.\''), + ); + } + + static function cli_cmd_run_extract($args) + { + $dir = $args['']['val']; + if (is_array($dir)) + { + if (count($dir) != 1) + { + echo "Only one target directory allowed.\n"; + exit(1); + } + else + { + $dir = $dir[0]; + } + } + $phar = $args['f']['val']; + $base = $phar->getPathname(); + $bend = strpos($base, '.phar'); + $bend = strpos($base, '/', $bend); + $base = substr($base, 0, $bend + 1); + $blen = strlen($base); + foreach(new RecursiveIteratorIterator($phar) as $pn => $f) + { + $sub = substr($pn, $blen); + $target = $dir . '/' . $sub; + if (!file_exists(dirname($target))) + { + if (!@mkdir(dirname($target))) + { + echo " ..unable to create dir\n"; + exit(1); + } + } + echo "$sub"; + if (!@copy($f, $target)) + { + echo " ...error\n"; + } + else + { + echo " ...ok\n"; + } + } + } } ?> \ No newline at end of file