Location: PHPKode > scripts > dyn_php > dyn_php/dyn_php.class.php
<?php

/*
	Dynamic PHP class (dyn_php) is intended for creation of PHP config files on the fly 
	using an object oriented way.
	
	License:
		Free for commercial or non-commercial use.
		All kind of modifications,improvements is granted but my author details must be 
		intact AND all patches made by you to the original code must be marked.
		
				
		provided "AS IS" without any warranties.
 	
 		Svetoslav Marinov 
 		svetoslavm#gmail.com
 		http://php-dyn.devquickref.com
*/

/**
 * Dynamic PHP class (dyn_php) is intended for creation of PHP files on the fly using an object oriented way.
 * 
 * @author Svetoslav Marinov <svetoslavm#gmail.com>
 * @link http://php-dyn.devquickref.com
 */
class dyn_php {

	var $buffer;
	/**
	 * class constructor
	 * @param void
	 * @return void
	 * @return dynphp
	 */
	function dyn_php() {
		$this->buffer = '';
	}
	
	/**
	 * error getter
	 * @param  void
	 * @return string
	 */
	function error() {
		
	}

	/**
	 * returns e.g. " $test = "test_value"\n;
	 * can be used if we create php vars at runtime
	 *
	 * @param string $name
	 * @param string $value
	*/
	function add_scalar($name, $value = "") {
		if (!is_scalar($value))
			return false;
		$value = addslashes($value);
		$str = "\t\$$name = \"$value\";\n";
		
		$this->buffer .= $str;
	}

	/**
	 * returns e.g. " define("TEST", "test_value");\n
	 * can be used if we create php vars at runtime
	 *
	 * @param string $name
	 * @param string $value
	*/
	function add_constant($name, $value = "") {
		if (!is_scalar($value))
			return false;
		
		$name = strtoupper($name);
		$value = addslashes($value);
		$name = preg_replace("@[^\w]*@si", "", $name);
		$str = "\tdefine(\"$name\", \"$value\");\n";
		
		$this->buffer .= $str;
	}


	/**
	 * dumps array contents using var_export function
	 *
	 * @param string $name
	 * @param array $value
	*/
	function add_array($name, $value = array()) {
		if (!is_array($value))
			return false;
			
		$value = var_export($value, 1);
		$str = "\t\$$name = $value;\n";
		$this->buffer .= $str;
	}
	
	/**
	 * wrap string with starting and ending php tags useful if we create PHP file at runtime
	 *
	 * @param string $str the php code that will be wrapped
	 */
	function wrap_php() {
		$str = '<' . '?' . "php\n";
		$str .= "\t// Generated by dyn_php class\n\n";
		$str .= $this->buffer;
		$str .= '?' . '>';
	
		$this->buffer = $str;
	}
	
	/**
	 * returns current buffer's contents. if file is specified, result is saved there
	 * 
	 * @param string $file
	 */
	function code($filename = "") {
		$this->wrap_php();
		
		// Let's make sure the file exists and is writable first.
		if (!empty($filename)) {

		   // In our example we're opening $filename in append mode.
		   // The file pointer is at the bottom of the file hence 
		   // that's where $somecontent will go when we fwrite() it.
		   if (!$handle = @fopen($filename, 'w')) {
		        $this->error = "Cannot open file ($filename)";
		        return false;
		   }
	
		   // Write $somecontent to our opened file.
		   if (fwrite($handle, $this->buffer ) === FALSE) {
		       $this->error = "Cannot write to file ($filename)";
		       return false;
		   }
		   
		   @fclose($handle);
		   return true;
		}   		   
		
		return $this->buffer;	
	}
}
Return current item: dyn_php