Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.0.8

Request #9504 Allow arrays to be passed to pushCommand as command line arguments
Submitted: 2006-11-29 20:47 UTC
From: joshualross Assigned:
Status: Verified Package: System_Command (version 1.0.5)
PHP Version: 5.1.6 OS: Any
Roadmaps: 1.1.0    
Subscription  


 [2006-11-29 20:47 UTC] joshualross (Joshua Ross)
Description: ------------ System_Command version 1.0.5 It would be beneficial if arguments to pushCommand could be sent as Arrays. In the case I am dealing with I am attempting to zip a large list of files from different directories. The list of files is in an array. I suggest a method that handles arrays by recursing through them. I successfully tested the following rewrite with and without arrays: Command.php@ line 313: $argv = func_get_args(); array_shift($argv); // foreach($argv as $arg) { // if (is_array($arg)) { // // // if (strpos($arg, '-') === 0) { // $command .= ' ' . $arg; // } // elseif ($arg != '') { // $command .= ' ' . escapeshellarg($arg); // } // } $command .= $this->_processArgv($argv); Then add the following method: /** * Used to recursively escape command line arguments * * @param array $argv Arguments for command * * @access private * @return string */ function _processArgv($argv) { $cmd = ''; foreach($argv as $arg) { if (is_array($arg)) { $cmd .= $this->_processArgv($arg); } elseif (strpos($arg, '-') === 0) { $cmd .= ' ' . $arg; } elseif ($arg != '') { $cmd .= ' ' . escapeshellarg($arg); } } return $cmd; } I hope this can be implemented. ;) Test script: --------------- $out_file = '/tmp/test.zip'; $files = array('/tmp/text1.txt', '/tmp/text2.txt'); $options = 'mj9'; $cmd = new System_Command(); $cmd->pushCommand('zip', '-' . $options, $out_file, $files); $rc = $cmd->execute(); Expected result: ---------------- A command like this: /usr/bin/zip -mj9 '/tmp/test.zip' '/tmp/text1.txt' '/tmp/text2.txt' Actual result: -------------- Above returns: /usr/bin/zip -mj9 '/tmp/test.zip' 'Array' If I loop through the files adding a space between files like this: foreach ($files as $file) { $to_zip .= ' ' . $file; } Result: /usr/bin/zip -mj9 '/tmp/test.zip' '/tmp/text1.txt /tmp/text2.txt' The entire string is quoted as it should be but then the command reads the space as part of the filename, hence one long filename instead of multiple files.

Comments