]> granicus.if.org Git - php/commitdiff
- Add extract command
authorMarcus Boerger <helly@php.net>
Thu, 10 May 2007 23:14:00 +0000 (23:14 +0000)
committerMarcus Boerger <helly@php.net>
Thu, 10 May 2007 23:14:00 +0000 (23:14 +0000)
ext/phar/phar.php
ext/phar/phar/clicommand.inc
ext/phar/phar/pharcommand.inc

index 426439e3ae7b2e6ff8dc1278fa13a5b62008859e..bcb96080e60f85979b6a2e1913a7aa07ec89d4a4 100644 (file)
@@ -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'=>'<file>   Specifies the PHAR file to extract.'),
+                       ''  => array('type'=>'dir',  'val'=>'.',                 'inf'=>'<dir>    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);
index f07fcd234f9396305639209a416c732de92a8d57..5a86f32f9db2630e9779c07538673d25960bf3e8 100755 (executable)
@@ -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";
index f08bbe4756b49a1de17b09bad20d198fff1d4361..0458b0d3621ecf87104a01887c81fafc62835b3f 100755 (executable)
@@ -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'=>'<file>   Specifies the PHAR file to extract.'),
+                       ''  => array('type'=>'dir',  'val'=>'.',                 'inf'=>'<dir>    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