cvs: php-src /pear System.php

From: Date: Mon, 15 Sep 2003 20:17:34 +0000
Subject: cvs: php-src /pear System.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-12757@lists.php.net to get a copy of this message
cox Mon Sep 15 16:17:34 2003 EDT Modified files: /php-src/pear System.php Log: * Implemented the find command (currently supported args: -type f, -type d, -maxdepth, -name) * Fixed recursion deep level in _dirToStruct() Index: php-src/pear/System.php diff -u php-src/pear/System.php:1.27 php-src/pear/System.php:1.28 --- php-src/pear/System.php:1.27 Tue Jun 10 16:03:43 2003 +++ php-src/pear/System.php Mon Sep 15 16:17:34 2003 @@ -16,7 +16,7 @@ // | Authors: Tomas V.V.Cox <cox@idecnet.com> | // +----------------------------------------------------------------------+ // -// $Id: System.php,v 1.27 2003/06/10 20:03:43 imajes Exp $ +// $Id: System.php,v 1.28 2003/09/15 20:17:34 cox Exp $ // require_once 'PEAR.php'; @@ -41,9 +41,14 @@ * print "could not delete file1 or dir1"; * } * +* In case you need to to pass file names with spaces, +* pass the params as an array: +* +* System::rm(array('-r', $file1, $dir1)); +* * @package System * @author Tomas V.V.Cox <cox@idecnet.com> -* @version $Revision: 1.27 $ +* @version $Revision: 1.28 $ * @access public * @see http://pear.php.net/manual/ */ @@ -123,15 +128,16 @@ } closedir($dir); sort($list); - foreach($list as $val) { - $path = $sPath . DIRECTORY_SEPARATOR . $val; - if (is_dir($path)) { - if ($aktinst < $maxinst || $maxinst == 0) { + if ($aktinst < $maxinst || $maxinst == 0) { + foreach($list as $val) { + $path = $sPath . DIRECTORY_SEPARATOR . $val; + echo "p: $path\n"; + if (is_dir($path)) { $tmp = System::_dirToStruct($path, $maxinst, $aktinst+1); $struct = array_merge_recursive($tmp, $struct); + } else { + $struct['files'][] = $path; } - } else { - $struct['files'][] = $path; } } return $struct; @@ -419,8 +425,8 @@ */ function which($program, $fallback = false) { - // is_executable() is not available on windows - if (OS_WINDOWS) { + // is_executable() is not available on windows + if (OS_WINDOWS) { $pear_is_executable = 'is_file'; } else { $pear_is_executable = 'is_executable'; @@ -444,6 +450,84 @@ } } return $fallback; + } + + /** + * The "find" command + * + * Usage: + * + * System::find($dir); + * System::find("$dir -type d"); + * System::find("$dir -type f"); + * System::find("$dir -name *.php"); + * System::find("$dir -name *.php -name *.htm*"); + * System::find("$dir -maxdepth 1"); + * + * Params implmented: + * $dir -> Start the search at this directory + * -type d -> return only directories + * -type f -> return only files + * -maxdepth <n> -> max depth of recursion + * -name <pattern> -> search pattern (bash style). Multiple -name param allowed + * + * @param mixed Either array or string with the command line + * @return array Array of found files + * + */ + function find($args) + { + if (!is_array($args)) { + $args = preg_split('/\s+/', $args, -1, PREG_SPLIT_NO_EMPTY); + } + $dir = array_shift($args); + $patterns = array(); + $depth = 0; + $do_files = $do_dirs = true; + for ($i = 0; $i < count($args); $i++) { + switch ($args[$i]) { + case '-type': + if (in_array($args[$i+1], array('d', 'f'))) { + if ($args[$i+1] == 'd') { + $do_files = false; + } else { + $do_dirs = false; + } + } + $i++; + break; + case '-name': + $patterns[] = "(" . preg_replace(array('/\./', '/\*/'), + array('\.', '.*'), + $args[$i+1]) + . ")"; + $i++; + break; + case '-maxdepth': + $depth = $args[$i+1]; + break; + } + } + $path = System::_dirToStruct($dir, $depth); + if ($do_files && $do_dirs) { + $files = array_merge($path['files'], $path['dirs']); + } elseif ($do_dirs) { + $files = $path['dirs']; + } else { + $files = $path['files']; + } + if (count($patterns)) { + $patterns = implode('|', $patterns); + echo "p: $patterns\n"; + $ret = array(); + for ($i = 0; $i < count($files); $i++) { + if (preg_match("#^$patterns\$#", $files[$i])) { + $ret[] = $files[$i]; + } + } + return $ret; + } + return $files; } } ?>

« previous php.pear.cvs (#12757) next »