Location: PHPKode > scripts > Event Handler > event-handler/class._Main.php
<?php
/**
* @package _Main Class
* @author Jean Machuca
* @version 1.0.20060731
*/
class _Main {
	var $oTemplate;
	var $_templateFile = "";
	var $oEvent;
	var $errorHandler;
	var $shutDownHandler;
	
	// Events
	var $onLoad;
	var $onRender;
	var $onDisplay;
	var $onPostRequest;
	var $onGetRequest;
	var $onHeadRequest;
	var $onPutRequest;
	var $onNoRequest;
	var $onError;
	var $onShutDown;
	
	function _Main(){
		$oEvent = new Event($this);
		$this->oEvent = &$oEvent;
		if ($this->_Load() 
			&& $this->_Request()
			&& $this->_Render()
		){
			return $this;
		} else {
			return null;
		}
	}
	
	function _Load(){
		$this->errorHandler = array(&$this,'_Error');
		$this->shutDownHandler = array(&$this,'_ShutDown');
		error_reporting(0);
		set_error_handler($this->errorHandler);
		register_shutdown_function($this->shutDownHandler);
		$this->oTemplate = new Smarty();
		$this->oTemplate->left_delimiter = "{#";
		$this->oTemplate->right_delimiter = "#}";
		$this->oTemplate->compile_dir = dirname(__FILE__).'/smarty/templates_c/';
		$this->oTemplate->cache_dir = dirname(__FILE__).'/smarty/libs/cache/';
		$this->_setTemplateFile(dirname(__FILE__)."/".strtolower(get_class($this).".tpl.htm"));
		return $this->oEvent->fire('Load');
	}
	
	function _Request(){
		switch ($_SERVER['REQUEST_METHOD']){
			case 'GET':
				return $this->oEvent->fire('GetRequest');
			break;
			case 'HEAD':
				return $this->oEvent->fire('HeadRequest');
			break;
			case 'POST':
				return $this->oEvent->fire('PostRequest');
			break;
			case 'PUT':
				return $this->oEvent->fire('PutRequest');
			break;
			default:
				return $this->oEvent->fire('NoRequest');
			break;
		}
		return false;
	}
	
	function _Render(){
		$render = $this->oEvent->fire('Render');
		if ($render){
			$header = $this->oTemplate->fetch(dirname(__FILE__)."/header.tpl");
			$footer = $this->oTemplate->fetch(dirname(__FILE__)."/footer.tpl");
			$this->oTemplate->assign('cabecera',$header);
			$this->oTemplate->assign('pie',$footer);
			$display = $this->oEvent->fire('Display');
			if ($display){
				$this->oTemplate->display($this->_templateFile);
			} else {
				return $render;
			}
		}
		return $render;
	}
	
	function _Error($errno, $errstr, $errfile, $errline){
		if (is_object($this)){
			$args = func_get_args();
			if ($errno == E_NOTICE && substr($errstr, 0, 17) == "Undefined index: ") return true;
			if ($errno == E_NOTICE && substr($errstr, 0, 20) == "Undefined property: ") return true;
			$aErrors = array(
				E_ERROR=>'Run-Time Error',
				E_WARNING=>'Run-Time Warning',
				E_PARSE=>'Compile-Time Parse Error',
				E_NOTICE=>'Run-Time Notice',
				E_CORE_ERROR=>'Fatal Startup Error',
				E_CORE_WARNING=>'Warning',
				E_COMPILE_ERROR=>'Fatal compile-time Error',
				E_COMPILE_WARNING=>'Compile-Time Warning',
				E_USER_ERROR=>'User-generated Error',
				E_USER_WARNING=>'User-generated Warning',
				E_USER_NOTICE=>'User-generated Notice');
			$aNoDisplayErrors = array(E_NOTICE);
			$aFatalErrors = array(E_ERROR,E_PARSE,E_CORE_ERROR,E_COMPILE_ERROR);
			if ((!in_array($errno,$aNoDisplayErrors)) && $this->oEvent->fire('Error',$args)){
				if(!function_exists('debug_backtrace'))
				{
					echo 'function debug_backtrace does not exists'."\r\n";
					return false;
				}
					echo '<pre>';
					echo "\r\n".'--:: '.$aErrors[$errno].': '.$errstr.' ::--'."\r\n";
					echo "\r\n".'----------------'."\r\n";
					echo 'Debug Back Trace:'."\r\n";
					echo '----------------'."\r\n";
				$backtrace = debug_backtrace();
				if (sizeof($backtrace)>2){$backtrace = array_slice($backtrace,1);}
				foreach($backtrace as $t)
				{
					echo "\t" . '@ ';
				if(isset($t['file'])) echo basename($t['file']) . ':' . $t['line'];
				else
				{
					// if file was not set, I assumed the functioncall
					// was from PHP compiled source (ie XML-callbacks).
					echo '<PHP inner-code>';
				}
					echo ' => ';
				if(isset($t['class'])) echo $t['class'] . $t['type'];
					echo $t['function'];
					if(isset($t['args']) && sizeof($t['args']) > 0) echo '(...)';
					else echo '()';
					echo "\r\n";
				}
				echo '</pre>';
				if (in_array($errno,$aFatalErrors)) exit;
				return false;
			}
			return true;
		} else {
			echo "Error Handling is not possible there";
			exit;
			return false;
		}
	}
	
	function _ShutDown(){
		return $this->oEvent->fire('ShutDown');
	}
	
	function _setTemplateFile($templateFile = ""){
		$this->_templateFile = $templateFile;
	}
	
	function __destruct(){
		unset($this);
	}
	
	
	/*
	* Default Events Methods
	* If an Event haven't a default method declared here, then the event definition of $this->onEvent type is obligatory
	*
	*/
	function fnLoad (){return true;}
	function fnRender (){return true;}
	function fnDisplay (){return true;}
	function fnPostRequest (){return true;}
	function fnGetRequest (){return true;}
	function fnHeadRequest (){return true;}
	function fnPutRequest (){return true;}
	function fnNoRequest (){return true;}
	function fnError (){return true;}
	function fnShutDown (){return true;}
	
}
?>
Return current item: Event Handler