<?php

/**
 * File including common framework-wide functions
 */

/**
 * Singleton function
 */ 
function get_instance($params=array())
{
	static $result = null;
	
	if(is_null($result) === TRUE)
	{
		$result = new miniMVC($params);
	}
	
	return $result;
}

/**
 * Function to search through the tree to find the necessary file
 *
 * @param string $file
 * @return string
 */
function _find_file($file)
{

}

/**
 * Enables pure PHP templating
 *
 * @param array $data
 * @param bool $return
 * @return mixed
 */
function load_view($file, $data=array(), $return=FALSE)
{
	// Contain the content for buffering
	ob_start();
	
	// Extract the data array
	extract($data);
	
	// 
}

/**
 * Custom error handler
 */
function on_error($num, $string, $file, $line, $context)
{
	$data = array(
	);
}

/**
 * Custom exception handler
 */
function on_exception($exception)
{
	// these are our templates
    $traceline = "#%s %s(%s): %s(%s)";
    $msg = "PHP Fatal error:  Uncaught exception '%s' with message '%s' in %s:%s<br />Stack trace:<br />%s<br />  thrown in %s on line %s";

	// alter your trace as you please, here
    $trace = $exception->getTrace();
    /*foreach ($trace as $key => $stackPoint) {
        // I'm converting arguments to their type
        // (prevents passwords from ever getting logged as anything other than 'string')
        $trace[$key]['args'] = array_map('gettype', $trace[$key]['args']);
    }*/

    // build your tracelines
    $result = array();
    foreach ($trace as $key => $stackPoint) {
        $result[] = sprintf(
            $traceline,
            $key,
            $stackPoint['file'],
            $stackPoint['line'],
            $stackPoint['function'],
            implode(', ', $stackPoint['args'])
        );
    }
    // trace always ends with {main}
    $result[] = '#' . ++$key . ' {main}';

    // write tracelines into main template
    $msg = sprintf(
        $msg,
        get_class($exception),
        $exception->getMessage(),
        $exception->getFile(),
        $exception->getLine(),
        implode("<br />", $result),
        $exception->getFile(),
        $exception->getLine()
    );
    
	echo $msg;
}

set_exception_handler('on_exception');