if ($argc < 2)
{
- echo "No command given, check ${argv[0]} help\n";
- exit(1);
+ self::error("No command given, check ${argv[0]} help\n");
}
elseif (!isset($this->cmds[$argv[1]]['run']))
{
- echo "Unknown command '${argv[1]}', check ${argv[0]} help\n";
- exit(1);
+ self::error("Unknown command '${argv[1]}', check ${argv[0]} help\n");
}
else
{
$arg = $argv[$i][1];
if (++$i >= $argc)
{
- echo "Missing argument to parameter '$arg' of command '$command', check ${argv[0]} help\n";
- exit(1);
+ self::error("Missing argument to parameter '$arg' of command '$command', check ${argv[0]} help\n");
}
else
{
}
else
{
- echo "Unknown parameter '${argv[$i]}' to command $command, check ${argv[0]} help\n";
- exit(1);
+ self::error("Unknown parameter '${argv[$i]}' to command $command, check ${argv[0]} help\n");
}
}
else
}
else if ($i < $argc)
{
- echo "Unexpected default arguments to command $command, check ${argv[0]} help\n";
- exit(1);
+ self::error("Unexpected default arguments to command $command, check ${argv[0]} help\n");
}
+ $missing = '';
foreach($this->args as $arg => $inf)
{
if (strlen($arg) && !isset($inf['val']) && isset($inf['required']) && $inf['required'])
{
- echo "Missing parameter '-$arg' to command $command, check ${argv[0]} help\n";
- $missing = true;
+ $missing .= "Missing parameter '-$arg' to command $command.\n";
}
}
- if ($missing)
+ if (strlen($missing))
{
- exit(1);
+ $missing .= "Check ${argv[0]} help\n";
+ self::error($missing);
}
}
call_user_func(array($this, $this->cmds[$command]['run']), $this->args);
}
+ static function error($msg, $exit_code = 1)
+ {
+ fprintf(STDERR, $msg);
+ exit($exit_code);
+ }
+
function checkArgTyp($arg, $i, $argc, $argv)
{
$typ = $this->args[$arg]['typ'];
if (isset($this->typs[$typ]['typ']))
{
- return call_user_func(array($this, $this->typs[$typ]['typ']), $argv[$i], $this->args[$arg]);
+ return call_user_func(array($this, $this->typs[$typ]['typ']), $argv[$i], $this->args[$arg], $arg);
}
else
{
return self::getSubFuncs($cmdclass, 'cli_arg_', array('typ'));
}
- static function cli_arg_typ_bool($arg)
+ static function cli_arg_typ_bool($arg, $cfg, $key)
{
return (bool)$arg;
}
- static function cli_arg_typ_regex($arg)
+ static function cli_arg_typ_int($arg, $cfg, $key)
+ {
+ if ((int)$arg != $arg)
+ {
+ self::error("Argument to -$key must be an integer.\n");
+ }
+ return (int)$arg;
+ }
+
+ static function cli_arg_typ_regex($arg, $cfg, $key)
{
if (strlen($arg))
{
}
}
- static function cli_arg_typ_select($arg, $cfg)
+ static function cli_arg_typ_select($arg, $cfg, $key)
{
if (!in_array($arg, array_keys($cfg['select'])))
{
- echo "Parameter value '$arg' not one of '" . join("', '", array_keys($cfg['select'])) . "'.\n";
- exit(1);
+ self::error("Parameter value '$arg' not one of '" . join("', '", array_keys($cfg['select'])) . "'.\n");
}
return $arg;
}
- static function cli_arg_typ_dir($arg)
+ static function cli_arg_typ_dir($arg, $cfg, $key)
{
$f = realpath($arg);
if ($f===false || !file_exists($f) || !is_dir($f))
{
- echo "Requested path '$arg' does not exist.\n";
- exit(1);
+ self::error("Requested path '$arg' does not exist.\n");
}
return $f;
}
- static function cli_arg_typ_file($arg)
+ static function cli_arg_typ_file($arg, $cfg, $key)
{
- $f = new SplFileInfo($arg);
- $f = $f->getRealPath();
+ $f = realpath($arg);
if ($f===false || !file_exists($f))
{
- echo "Requested file '$arg' does not exist.\n";
- exit(1);
+ self::error("Requested file '$arg' does not exist.\n");
}
return $f;
}
- static function cli_arg_typ_filenew($arg)
+ static function cli_arg_typ_filenew($arg, $cfg, $key)
{
$d = dirname($arg);
$f = realpath($d);
if ($f === false)
{
- echo "Path for file '$arg' does not exist.\n";
- exit(1);
+ self::error("Path for file '$arg' does not exist.\n");
}
return $f . substr($arg, strlen($d));;
}
- static function cli_arg_typ_filecont($arg)
+ static function cli_arg_typ_filecont($arg, $cfg, $key)
{
- return file_get_contents(self::cli_arg_typ_file($arg));
+ return file_get_contents(self::cli_arg_typ_file($arg, $cfg, $key));
}
function cli_get_SP2($l1, $arg_inf)
{
if (count($which) != 1)
{
- echo "More than one command given.\n";
- exit(1);
+ self::error("More than one command given.\n");
}
$which = $which[0];
if (!array_key_exists($which, $this->cmds))
{
- echo "Unknown command, cannot retrieve help.\n";
- exit(1);
+ self::error("Unknown command, cannot retrieve help.\n");
}
$l = strlen($which);
$cmds = array($which => $this->cmds[$which]);
'c' => array(
'typ' => 'compalg',
'val' => NULL,
- 'inf' => '<algo> Compression algorithmus.',
+ 'inf' => '<algo> Compression algorithm.',
'select' => array(
'0' => 'No compression',
'none' => 'No compression',
'typ' => 'any',
'val' => NULL,
'inf' => '<index> Subscription index to work on.',
+ ),
+ 'l' => array(
+ 'typ' => 'int',
+ 'val' => 0,
+ 'inf' => '<level> Number of preceeding subdirectories to strip from file entries.',
),
'm' => array(
'typ' => 'any',
'p' => array(
'typ' => 'file',
'val' => NULL,
- 'inf' => '<loader> Location of PHP_Archive class file (pear list-files PHP_Archive).',
+ 'inf' => '<loader> Location of PHP_Archive class file. To find the file use the '
+ .'following: \'pear list-files PHP_Archive|grep Archive.php\'.',
),
's' => array(
'typ' => 'file',
return substr($haystack, -strlen($needle)) == $needle;
}
- static function cli_arg_typ_pharnew($arg)
+ static function cli_arg_typ_pharnew($arg, $cfg, $key)
{
- $arg = self::cli_arg_typ_filenew($arg);
+ $arg = self::cli_arg_typ_filenew($arg, $cfg, $key);
if (!Phar::isValidPharFilename($arg))
{
- echo "Phar files must have file extension '.phar', '.phar.php', '.phar.bz2' or 'phar.gz'.\n";
- exit(1);
+ self::error("Phar files must have file extension '.phar', '.phar.php', '.phar.bz2' or 'phar.gz'.\n");
}
return $arg;
}
- static function cli_arg_typ_pharfile($arg)
+ static function cli_arg_typ_pharfile($arg, $cfg, $key)
{
try
{
- $pharfile = self::cli_arg_typ_file($arg);
+ $pharfile = self::cli_arg_typ_file($arg, $cfg, $key);
if (!Phar::loadPhar($pharfile))
{
- "Unable to open phar '$arg'\n";
- exit(1);
+ self::error("Unable to open phar '$arg'\n");
}
return $pharfile;
}
catch(Exception $e)
{
- echo "Exception while opening phar '$arg':\n";
- echo $e->getMessage() . "\n";
- exit(1);
+ self::error("Exception while opening phar '$arg':\n" . $e->getMessage() . "\n");
}
}
- static function cli_arg_typ_pharurl($arg)
+ static function cli_arg_typ_pharurl($arg, $cfg, $key)
{
- return 'phar://' . self::cli_arg_typ_pharfile($arg);
+ return 'phar://' . self::cli_arg_typ_pharfile($arg, $cfg, $key);
}
- static function cli_arg_typ_phar($arg)
+ static function cli_arg_typ_phar($arg, $cfg, $key)
{
try
{
- return new Phar(self::cli_arg_typ_pharfile($arg));
+ return new Phar(self::cli_arg_typ_pharfile($arg, $cfg, $key));
}
catch(Exception $e)
{
- echo "Exception while opening phar '$argv':\n";
- echo $e->getMessage() . "\n";
- exit(1);
+ self::error("Exception while opening phar '$argv':\n" . $e->getMessage() . "\n");
}
}
- static function cli_arg_typ_entry($arg)
+ static function cli_arg_typ_entry($arg, $cfg, $key)
{
// no further check atm, maybe check for no '/' at beginning
return $arg;
}
- static function cli_arg_typ_compalg($arg, $cfg)
+ static function cli_arg_typ_compalg($arg, $cfg, $key)
{
- $arg = self::cli_arg_typ_select($arg, $cfg);
+ $arg = self::cli_arg_typ_select($arg, $cfg, $key);
switch($arg)
{
case 'auto':
static function cli_cmd_inf_pack()
{
return "Pack files into a PHAR archive.\n"
- . "When using -s <stub>, then the stub file is being excluded from the list of input files/dirs."
+ . "When using -s <stub>, then the stub file is being excluded from the list of input files/dirs.\n"
+ . "To create an archive that contains PEAR class PHP_Archive then point -p argument to PHP/Archive.php.\n"
;
}
static function cli_cmd_arg_pack()
{
- $args = self::phar_args('acFhipsx', 'pharnew');
+ $args = self::phar_args('acFhilpsx', 'pharnew');
$args[''] = array('typ'=>'any', 'val'=>NULL, 'required'=>1, 'inf'=>' Any number of input files and directories. If -i is in use then ONLY files and matching thegiven regular expression are being packed. If -x is given then files matching that regular expression are NOT being packed.');
return $args;
}
{
if (ini_get('phar.readonly'))
{
- echo "Creating phar files is disabled by ini setting 'phar.readonly'.\n";
- exit(1);
+ self::error("Creating phar files is disabled by ini setting 'phar.readonly'.\n");
}
if (!Phar::canWrite())
{
- echo "Creating phar files is disabled, Phar::canWrite() returned false.\n";
- exit(1);
+ self::error("Creating phar files is disabled, Phar::canWrite() returned false.\n");
}
$alias = $this->args['a']['val'];
$archive = $this->args['f']['val'];
$hash = $this->args['h']['val'];
$regex = $this->args['i']['val'];
+ $level = $this->args['l']['val'];
$loader = $this->args['p']['val'];
$stub = $this->args['s']['val'];
$invregex= $this->args['x']['val'];
if (!is_array($input))
{
- $this->phar_add($phar, $input, $regex, $invregex, $stub);
+ $this->phar_add($phar, $level, $input, $regex, $invregex, $stub, NULL);
}
else
{
foreach($input as $i)
{
- $this->phar_add($phar, $i, $regex, $invregex, $stub);
+ $this->phar_add($phar, $level, $i, $regex, $invregex, $stub, NULL);
}
}
exit(0);
}
- static function phar_add(Phar $phar, $input, $regex, $invregex, SplFileInfo $stub)
+ static function phar_add(Phar $phar, $level, $input, $regex, $invregex, SplFileInfo $stub = NULL, $compress = NULL)
{
+ if (@is_file($input) && !is_dir($input))
+ {
+ return self::phar_add_file($phar, $level, $input, $input, $compress);
+ }
$dir = new RecursiveDirectoryIterator($input);
$dir = new RecursiveIteratorIterator($dir);
{
if (empty($stub) || $file->getRealPath() != $stub->getRealPath())
{
- $f = $dir->getSubPathName();
- echo "$f\n";
- $phar[$f] = file_get_contents($file);
+ self::phar_add_file($phar, $level, $dir->getSubPathName(), $file, $compress);
}
}
}
catch(Excpetion $e)
{
- echo "Unable to complete operation on file '$file'\n";
- echo $e->getMessage() . "\n";
- exit(1);
+ self::error("Unable to complete operation on file '$file'\n" . $e->getMessage() . "\n");
+ }
+ }
+
+ static function phar_add_file(Phar $phar, $level, $entry, $file, $compress)
+ {
+ $entry = str_replace('//', '/', $entry);
+ while($level-- > 0 && ($p = strpos($entry, '/')) !== false)
+ {
+ $entry = substr($entry, $p+1);
+ }
+ echo "$entry\n";
+ $phar[$entry] = file_get_contents($file);
+ switch($compress)
+ {
+ case 'gz':
+ case 'gzip':
+ $phar[$entry]->setCompressedGZ();
+ break;
+ case 'bz2':
+ case 'bzip2':
+ $phar[$entry]->setCompressedBZIP2();
+ break;
+ default:
+ break;
}
}
{
if (count($dir) != 1)
{
- echo "Only one target directory allowed.\n";
- exit(1);
+ self::error("Only one target directory allowed.\n");
}
else
{
if (!@mkdir(dirname($target)))
{
echo " ..unable to create dir\n";
- exit(1);
+ self::error("Operation could not be completed\n");
}
}
echo "$sub";
}
}
+ static function cli_cmd_inf_delete()
+ {
+ return "Delete entry from a PHAR package.";
+ }
+
+ static function cli_cmd_arg_delete()
+ {
+ return self::phar_args('FE', 'phar');
+ }
+
+ function cli_cmd_run_delete()
+ {
+ $phar = $this->args['f']['val'];
+ $entry = $this->args['e']['val'];
+
+ $phar->startBuffering();
+ unset($phar[$entry]);
+ $phar->stopBuffering();
+ }
+
+ static function cli_cmd_inf_add()
+ {
+ return "Add entries to a PHAR package.";
+ }
+
+ static function cli_cmd_arg_add()
+ {
+ $args = self::phar_args('acFilx', 'phar');
+ $args[''] = array('typ'=>'any', 'val'=>NULL, 'required'=>1, 'inf'=>' Any number of input files and directories. If -i is in use then ONLY files and matching thegiven regular expression are being packed. If -x is given then files matching that regular expression are NOT being packed.');
+ return $args;
+ }
+
+ function cli_cmd_run_add()
+ {
+ $compress= $this->args['c']['val'];
+ $phar = $this->args['f']['val'];
+ $regex = $this->args['i']['val'];
+ $level = $this->args['l']['val'];
+ $invregex= $this->args['x']['val'];
+ $input = $this->args['']['val'];
+
+ $phar->startBuffering();
+
+ if (!is_array($input))
+ {
+ $this->phar_add($phar, $level, $input, $regex, $invregex, NULL, $compress);
+ }
+ else
+ {
+ foreach($input as $i)
+ {
+ $this->phar_add($phar, $level, $i, $regex, $invregex, NULL, $compress);
+ }
+ }
+ $phar->stopBuffering();
+ exit(0);
+ }
+
function cli_cmd_inf_stub_set()
{
return "Set the stub of a PHAR file. "
return $args;
}
- function cli_cmd_run_stub_set($args)
+ function cli_cmd_run_stub_set()
{
$phar = $this->args['f']['val'];
$stub = $this->args['s']['val'];
return $args;
}
- function cli_cmd_run_stub_get($args)
+ function cli_cmd_run_stub_get()
{
$phar = $this->args['f']['val'];
$stub = $this->args['s']['val'];
function cli_cmd_arg_meta_set()
{
- $args = self::phar_args('FekM', 'phar');
- return $args;
+ return self::phar_args('FekM', 'phar');
}
function cli_cmd_run_meta_set()
}
if (!is_array($old))
{
- echo "Metadata is a flat value while an index operation was issued.";
- exit(1);
+ self::error('Metadata is a flat value while an index operation was issued.');
}
$old[$index] = $meta;
$meta = $old;
function cli_cmd_arg_meta_get()
{
- $args = self::phar_args('Fek', 'phar');
- return $args;
+ return self::phar_args('Fek', 'phar');
}
function cli_cmd_run_meta_get()
return self::phar_args('Fk', 'phar');
}
- function cli_cmd_run_info($args)
+ function cli_cmd_run_info()
{
$phar = $this->args['f']['val'];
$index = $this->args['k']['val'];
$infos['Uncompressed-size'] = $usize;
$infos['Compressed-size'] = $csize;
$infos['Compression-ratio'] = sprintf('%.3g%%', ($csize * 100) / $usize);
- $infos['Metadata-global'] = $phar->hasMetadata();
+ $infos['Metadata-global'] = $phar->hasMetadata()*1;
$infos['Metadata-files'] = $mcount;
if (isset($index))
{
if (!isset($infos[$index]))
{
- echo "Requested value does not exist.";
- exit(1);
+ self::error("Requested value '$index' does not exist.\n");
}
echo $infos[$index];
exit(0);