Exemple #1
0
	/**
	 * Load a PHP file from a file path or a directory path.
	 * 
	 * Internally, it uses the "PurFile::browse" method to find all
	 * files with a ".php" extension. It accept the same options as
	 * the "PurFile::browse" method.
	 * 
	 * @return 
	 * @param object $path
	 * @param object $options[optional]
	 */
	public static function load($path,$options=array()){
		if(!is_readable($path)) throw new Exception('Provided path not readable: '.$path);
		if(is_file($path)){
			require_once($path);
		}else if(is_dir($path)){
			$files = PurFile::browse($path,array_merge(array(
				'file_only'=>true,
				'absolute'=>true,
				'include'=>'**/*.php'),$options));
			foreach($files as $file){
				require_once($file);
			}
		}
	}
Exemple #2
0
	/**
	 * Delete a file or a directory (including its sub-directories).
	 * 
	 * Options may include:
	 * - include mixed(array or string list)
	 * - exclude mixed(array or string list)
	 * 
	 * The source path to delete may be a pattern.
	 * 
	 * Exemple to remove all files in a directory while preserving the directory
	 *     PurFile::delete('/path/to/directory/**');
	 * 
	 * @return string Path being destroyed or null if path does not exist
	 * @param string $path Path to the file or directory
	 */
	public static function delete($path,$options=array()) {
		// Sanitize include option
		if(array_key_exists('include',$options)){
			if(is_string($options['include'])) $options['include'] = array($options['include']);
			else if(!is_array($options['include'])) throw new InvalidArgumentException('Invalid Option "include": string or array expected of file selectors expected');
		}else{
			$options['include'] = array();
		}
		// Sanitize exclude option
		if(array_key_exists('exclude',$options)){
			if(is_string($options['exclude'])) $options['exclude'] = array($options['exclude']);
			else if(!is_array($options['exclude'])) throw new InvalidArgumentException('Invalid Option "exclude": string or array expected of file selectors expected');
		}else{
			$options['exclude'] = array();
		}
		if(is_file($path)){
			if(!is_readable($path)) throw new Exception('Permission Denied: '.$path);
			// Windows does not allow removal of 0444 mask
			if(DIRECTORY_SEPARATOR=='\\'){
				if(!is_writable($path)){
					chmod($path,0666);;
				}
			}
			unlink($path);
			return $path;
		}
		$options['absolute'] = true;
		try{
			$files = PurFile::browse($path,$options);
		}catch(Exception $e){
			// there is nothing to remove, just return null
			return null;
		}
		
		$files = array_reverse($files);
		while(list(,$file) = each($files)){
			if(is_file($file)){
				if(!is_readable($file)) throw new Exception('Permission Denied: '.$file);
				// Windows does not allow removal of 0444 mask
				if(DIRECTORY_SEPARATOR=='\\'){
					if(!is_writable($file)){
						chmod($file,0666);;
					}
				}
				unlink($file);
			}else if(is_dir($file)){
				rmdir($file);
			}
		}
		if(is_dir($path)){
			$handler = opendir($path);
			$found = false;
			while(false!==($file=readdir($handler))){
				if($file == "." or $file == ".."){
					continue;
				}
				$found = true;
				break;
			}
			closedir($handler);
			if(!$found) rmdir($path);
		}
		return $path;
	}