<?php

require_once 'common/AbstractObject.php';
require_once 'common/collection/BaseIterator.php';

/**
 * Util file system abstract class. 
 * 
 * This class holds util methods for filesystem access. 
 * It's aimed for abstract use.
 * 
 * @author  $Author: pruna $
 * @package filesystem
 * @version $Revision: 1.3 $
 * @abstract 
 * @created 18-Mar-2005
 * @modified $Date: 2005/12/06 02:56:45 $
 */
class FileSystemUtils extends AbstractObject {

    /**
     * Method that checks if file exists.
     * Filepath can include path information.
     * 
     * @access  public
     * @param   string   $filePath     Name of file to look for
     * @return  boolean
     */
	function fileExists($filePath) {
		return file_exists($filePath);
	}
	
    /**
     * Method that returns string file path. 
     * It checks that it's safe and accesible.
     * 
     * @access  public
     * @param   string   $filePath     Name of file to check
     * @return  mixed    string or null
     */
	function getCheckedFilePath($filePath) {
	    if (self::isAccesible(realpath($filePath))) {
	    	return realpath($filePath);
	    }
	    return null;
	}
	
    /**
     * Method that returns string file path. If $path is a Collection, 
     * looks for a file that exists.
     * 
     * @access  public
     * @param   string   $filename     Name of file to include
     * @param   mixed    $path         String or collection with paths
     * @return  mixed    string
     */
	function getFilePath($filename, $path) {
		if (is_string($path)) {
			return self::getCheckedFilePath($path . $filename);
		} else {
			$it = BaseIterator::castCollection($path);
			while ($it->hasMore()) {
				if (self::getCheckedFilePath($it->next() . $filename)) {
					return self::getCheckedFilePath($it->current() . $filename);
				}
			}
			return self::getCheckedFilePath($filename);
		}
	}
	
    /**
     * Method that checks if file is accesible (exists and is readable).
     * 
     * @access  public
     * @param   string   $pathFile     Name of file to look for
     * @return  boolean
     */
	function isAccesible($pathFile) {
		return (self::fileExists($pathFile) 
		    && self::isReadable($pathFile));
	}
	
    /**
     * Method that checks if file is readable.
     * 
     * @access  public
     * @param   string   $pathFile     Name of file to look for
     * @return  boolean
     */
	function isReadable($pathFile) {
		return is_readable($pathFile);
	}
	
    /**
     * Method that checks if file is writable.
     * 
     * @access  public
     * @param   string   $pathFile     Name of file to look for
     * @return  boolean
     */
	function isWritable($pathFile) {
		return is_writable($pathFile);
	}
	
}

?>