Login   Register  
PHP Classes
elePHPant
Icontem

File: class.config_writer.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Keyvan Minoukadeh  >  Config Manager  >  class.config_writer.php  >  Download  
File: class.config_writer.php
Role: ???
Content type: text/plain
Description: Class - config writer
Class: Config Manager
Sorry, no longer supported
Author: By
Last change:
Date: 12 years ago
Size: 6,093 bytes
 

Contents

Class file image Download
<?php
/* vim: set ai tabstop=4: */
// $Date: 2002/04/09 01:29:38 $
// $Revision: 1.3 $
// +----------------------------------------------------------------------+
// | CONFIG MANAGER 0.1.2 - 09-Apr-2002                                   |
// +----------------------------------------------------------------------+
// | Author: Keyvan Minoukadeh - keyvan@k1m.com - http://www.k1m.com      |
// +----------------------------------------------------------------------+
// | PHP class for managing plain text config files.                      |
// +----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or        |
// | modify it under the terms of the GNU General Public License          |
// | as published by the Free Software Foundation; either version 2       |
// | of the License, or (at your option) any later version.               |
// |                                                                      |
// | This program 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 General Public License for more details.                         |
// +----------------------------------------------------------------------+

if (defined('CONFIGMAN_DIR')) {
	require_once(CONFIGMAN_DIR.'class.config_base.php');
}

/**
* Config manager writer class
*
* Use this class to write plain text config files.
* This is an extension to the base config class.
*
* @author   Keyvan Minoukadeh <keyvan@k1m.com>
* @version  0.1.2
*/
class config_writer extends config_base
{
	/**
	* prefix with var type when writing config?
	*/
	var $prefix_with_type		= true;

	/**
	* chmod value (see: http://www.php.net/chmod)
	*/
	var $chmod					= 0644;

	/////////////
	// methods
	/////////////


	/**
	* Constructor
	*
	* @param	string	$config				config file to use
	* @param	bool	$prefix_with_type	null: use default
	*/
	function config_writer($config=null, $prefix_with_type=null)
	{
		// run base constructor
		if (!is_null($config)) {
			$this->config_base($config);
		}

		if (!is_null($prefix_with_type)) {
			$this->prefix_with_type = $prefix_with_type;
		}
	}

	/**
	* Write config
	*
	* write config file to $this->config
	*
	* @param	bool	$prefix_with_type	prefix the type on the var name? (default: null)
	* @param	int		$chmod				chmod octal value (see: http://www.php.net/chmod)
	* @return	bool						true on success, false otherwise
	*/
	function write($prefix_with_type=null, $chmod=null)
	{
		$func_name = 'write';

		clearstatcache();

		if (is_null($chmod)) {
			$chmod = $this->chmod;
		}

		$config = @implode("\n", $this->build($prefix_with_type));
		
		// write file
		$fp = @fopen($this->config, "w");
		if (!$fp) {
			if ($this->debug) $this->_debug("$func_name: Could not create/access file");
			return false;
		} else {
			// exclusive lock
			flock($fp, 2);
			$result = @fwrite($fp, $config);
			// release lock
			flock($fp, 3);
			fclose($fp);
			if (!$result) {
				if ($this->debug) $this->_debug("$func_name: Could not write to file");
				return false;
			} else {
				if ($this->debug) $this->_debug("$func_name: Config file written");
				// touch file with modification time of main config file (used for comparison)
				touch($this->config);
				@chmod($this->config, $chmod);
				return true;
			}
		}
		return false;
	}

	/**
	* Build
	*
	* @param	bool	$prefix_with_type	prefix the type on the var name?
	* @return	array						array containing the config lines
	*/
	function build($prefix_with_type=null)
	{
		$func_name = 'build_string';

		if (is_null($prefix_with_type)) {
			$prefix_with_type = $this->prefix_with_type;
		}

		$write = array();
		$lines = array();
		$type_prefix = array_flip($this->type_prefix);
		foreach ($this->param as $var => $val) {
			$write[(string)key($val)]["$var"] = $val;
		}
		// loop sections
		foreach ($write as $section => $val) {
			$lines[] = "[$section]";
			foreach ($val as $key => $v) {
				// we know section name already, no need to keep typing it
				$v = $v["$section"];
				// add comment
				if (!empty($v['comment'])) {
					$comment_lines = explode("\n", $v['comment']);
					foreach ($comment_lines as $comment) {
						$lines[] = $this->comment." $comment";
					}
				}
				// set cur value
				$cur_val = $v['value'];
				// is array?
				if (is_array($cur_val)) {
					$is_array = true;
				} else {
					$is_array = false;
				}
				
				do {
					if ($is_array) {
						$cur_val = each($v['value']);
					} else {
						$cur_val = array(0, $v['value']);
					}

					// break if past last array element
					if ($cur_val === false) {
						break;
					}

					// add type prefix?
					if ($prefix_with_type) {
						$cur_key = $type_prefix[$this->_get_type($cur_val[1])]."$key";
					} else {
						$cur_key = $key;
					}

					// is associative array?
					if (is_string($cur_val[0])) {
						$assoc = ".{$cur_val[0]}";
					} else {
						$assoc = "";
					}

					// add value
					if (is_string($cur_val[1])) {
						$new_val = str_replace(array("\n","\r","\t"), array('\n','\r','\t'), $cur_val[1]);
						if ($new_val !== $cur_val[1]) {
							$new_val = preg_replace('/\\\\(?!n|r|t)/', '\\\\\\', $new_val);
							$lines[] = "{$cur_key}{$assoc} {$this->separator} \"$new_val\"";
						} else {
							$lines[] = "{$cur_key}{$assoc} {$this->separator} '$new_val'";
						}
					} else {
						// is value boolean?
						if (is_bool($cur_val[1]) && $prefix_with_type) {
							$cur_val[1] = (($cur_val[1] === true) ? 'true' : 'false');
						}
						$new_val = $cur_val[1];
						$lines[] = "{$cur_key}{$assoc} {$this->separator} $new_val";
					}
				} while ($is_array);
				// blank line
				$lines[] = '';
			}
		}
		return $lines;
	}

}

?>