Location: PHPKode > projects > ZZ/OSS Installer > zic-1.1.0dev1/installer/lib/ZZOSS_File/Replace.php
<?php  
    /*
    Copyright (C) 2001-2004 ZZOSS GbR, http://www.zzoss.com

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    */
    
    /**
    @version $Id: Replace.php,v 1.7 2004/04/06 14:45:37 ordnas Exp $
    @copyright Copyright &copy; 2001-2004 ZZ/OSS GbR, http://www.zzoss.com
    @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
    */

/**   
 *	Replaces characters in a file
 *   
 * @version  1   
 * @author   Sandro Zic <hide@address.com>; |    
 * @author   Christian Zonsius <hide@address.com>; |    
 */   
   
class ZZOSS_FileReplace {   

  	var $search = array();
	var $replace = array();
    var $search_escape = array();
	var $replace_escape = array();
	var $mode = 'file';
	
	function file($file) 
	{ 
		$this->file = $file; 
		$this->mode = 'file';
	} 
	
 	function data($data)
	{
		$this->data = $data;
		$this->mode = 'data';
	}
 
	function replace($values = array())
	{
	  	if(count($values)){
		  	foreach($values as $key => $val){
			  	array_push($this->search, "'".$key."'si");
				array_push($this->replace, $val);
			}
		}
	}
    
    function setEscapes($values)
    {
        if(is_array($values) && count($values)){
		  	foreach($values as $key => $val){
			  	array_push($this->search_escape, "'".$key."'si");
				array_push($this->replace_escape, $val);
			}
		}
    }
	
	function writeout($file_writeout, $do_escape = false)  
	{
		// create the directory if it does not yet exist
		System::mkdir(array('-p',dirname($file_writeout)));
	  	$file_data = $this->parse($do_escape);
		// write the parsed config file
		$fp = fopen($file_writeout, "w");  
		fputs( $fp, $file_data);  
		fclose( $fp );
	}

	function parse($do_escape = false)
	{
        // read the file
		if($this->mode!='data') {
	        $file_data = fread(fopen($this->file, 'r'), filesize($this->file));
		} else {
			$file_data = $this->data;
		}
        if(
            $do_escape &&
            count($this->search_escape) &&
            count($this->replace_escape)){
            $search =& $this->search_escape + $this->search;
            $replace =& $this->replace_escape + $this->replace;
        } else {
            $search =& $this->search;
            $replace =& $this->replace;
        }
        
        $file_data_replaced = preg_replace ($search, $replace, $file_data);
		
		return $file_data_replaced;
	}
} 
?>
Return current item: ZZ/OSS Installer