cvs: php4 /pear/Experimental System.php

From: Date: Sun, 07 Oct 2001 16:06:55 +0000
Subject: cvs: php4 /pear/Experimental System.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-948@lists.php.net to get a copy of this message
cox Sun Oct 7 12:06:55 2001 EDT Added files: /php4/pear/Experimental System.php Log: Static functions for different operations. Should work under Unix and Windows. The names and usage has been taken from its repectively GNU commands. (initial commit and experimental state) Index: php4/pear/Experimental/System.php +++ php4/pear/Experimental/System.php <?php // // +----------------------------------------------------------------------+ // | PHP version 4.0 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2001 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Tomas V.V.Cox <cox@idecnet.com> | // | | // +----------------------------------------------------------------------+ // // $Id: System.php,v 1.1 2001/10/07 16:06:55 cox Exp $ // // TODO: // - Test under Windows (help is really appreciated in this point) // - Build strong tests // - Error reporting (now shows standar php errors) require_once 'PEAR.php'; require_once 'Console/Getopt.php'; /** * Static functions for different operations. Should work under * Unix and Windows. The names and usage has been taken from its repectively * GNU commands. * * Usage: System::rm('-r file1 dir1'); * * ------------------- EXPERIMENTAL STATUS ------------------- */ class System extends PEAR { function _parseArgs($argv, $short_options, $long_options = null) { if (!is_array($argv)) { $argv = preg_split('/\s+/', $argv); } $options = Console_Getopt::getopt($argv, $short_options); return $options; } /** * System::_dirToStruct('dir1', 0) => * Array * ( * [dirs] => Array * ( * [0] => dir1 * ) * * [files] => Array * ( * [0] => dir1/file2 * [1] => dir1/file3 * ) * ) */ function _dirToStruct($sPath, $maxinst, $aktinst = 0) { $struct = array('dirs' => array(), 'files' => array()); if (($dir = @opendir($sPath)) === false) { return $struct; // XXX could not open error } $struct['dirs'][] = $sPath; // XXX don't add if '.' or '..' ? $list = array(); while ($file = readdir($dir)) { if ($file != '.' && $file != '..') { $list[] = $file; } } closedir($dir); sort($list); foreach($list as $val) { $path = $sPath . DIRECTORY_SEPARATOR . $val; if (is_dir($path)) { if ($aktinst < $maxinst || $maxinst == 0) { $tmp = System::_dirToStruct($path, $maxinst, $aktinst+1); $struct = array_merge_recursive($struct, $tmp); } } else { $struct['files'][] = $path; } } return $struct; } /** * * @param array $files Array listing files and dirs * @return array * @see System::_dirToStruct() */ function _multipleToStruct($files) { $struct = array('dirs' => array(), 'files' => array()); foreach($files as $file) { if (is_dir($file)) { $tmp = System::_dirToStruct($file, 0); $struct = array_merge_recursive($struct, $tmp); } else { $struct['files'][] = $file; } } return $struct; } /** * The rm command for removing files. Supports multiple files and dirs * and also recursive deletes * */ function rm($args) { $opts = System::_parseArgs($args, 'rf'); // "f" do nothing but like it :-) if (PEAR::isError($opts)) { return $opts; } foreach($opts[0] as $opt) { if ($opt[0] == 'r') { $do_recursive = true; } } if (isset($do_recursive)) { $struct = System::_multipleToStruct($opts[1]); if (PEAR::isError($struct)) { return $struct; } foreach($struct['files'] as $file) { unlink($file); // XXXX Works under Windows? } foreach($struct['dirs'] as $dir) { rmdir($dir); } } else { foreach ($opts[1] as $file) { $delete = (is_dir($file)) ? 'rmdir' : 'unlink'; // XXXX Windows? $delete($file); } } return true; } /** * Make directories * */ function mkDir($args) { $opts = System::_parseArgs($args, 'pm:'); if (PEAR::isError($opts)) { return $opts; } $mode = 0777; // default mode foreach($opts[0] as $opt) { if ($opt[0] == 'p') { $create_parents = true; } elseif($opt[0] == 'm') { $mode = $opt[1]; } } if (isset($create_parents)) { foreach($opts[1] as $dir) { $dirstack = array(); while (!@is_dir($dir) && $dir != DIRECTORY_SEPARATOR) { array_unshift($dirstack, $dir); $dir = dirname($dir); } while ($newdir = array_shift($dirstack)) { if (!mkdir($newdir, $mode)) { break; // XXX error } } } } else { foreach($opts[1] as $dir) { if (!mkdir($newdir, $mode)) { continue; // XXX error } } } return true; } } ?>

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