miniMVC/sys/common.php

201 lines
4.0 KiB
PHP
Raw Normal View History

2011-12-27 13:24:28 -05:00
<?php
/**
* File including common framework-wide functions
*/
/**
* Function to search through the tree to find the necessary file
*
* @param string $file
* @param string $curr_path
* @return void
2011-12-27 13:24:28 -05:00
*/
function load_file($file, $curr_path="")
2011-12-27 13:24:28 -05:00
{
$path = "";
if($curr_path === "app")
{
$path = APP_PATH."{$file}.php";
}
else if($curr_path === "sys")
{
$path = SYS_PATH."{$file}.php";
}
else
{
$path = MOD_PATH."{$curr_path}/{$file}.php";
}
if( ! is_file($path))
{
/*if($curr_path !== "")
{
$matches = array();
if(preg_match("`modules/([a-z 0-9~%.:_\-])/?`i", $curr_path, $matches))
{
$module = $matches[1];
$path = MOD_PATH."{$module}/{$file}.php";
}
}
else
{*/
$path = MOD_PATH."welcome/{$file}.php";
//}
}
if(is_file($path))
{
require_once($path);
}
}
function load_class($name, &$self=array())
{
if(is_array($self))
{
$self =& miniMVC::get_instance();
}
$path = SYS_PATH."{$name}.php";
$class = "{$name}";
if(class_exists($class, FALSE))
{
if( ! isset($self->$name))
{
$self->$name = new $class;
return;
}
}
load_file($name, 'sys');
if(class_exists($class, FALSE))
{
$self->$name = new $class;
}
2011-12-27 13:24:28 -05:00
}
// --------------------------------------------------------------------------
2011-12-27 13:24:28 -05:00
/**
* Custom error handler
*/
function on_error($severity, $message, $filepath, $line, $context)
2011-12-27 13:24:28 -05:00
{
$levels = array(
E_ERROR => 'Error',
E_WARNING => 'Warning',
E_PARSE => 'Parsing Error',
E_NOTICE => 'Notice',
E_CORE_ERROR => 'Core Error',
E_CORE_WARNING => 'Core Warning',
E_COMPILE_ERROR => 'Compile Error',
E_COMPILE_WARNING => 'Compile Warning',
E_USER_ERROR => 'User Error',
E_USER_WARNING => 'User Warning',
E_USER_NOTICE => 'User Notice',
E_STRICT => 'Strict Error'
2011-12-27 13:24:28 -05:00
);
$severity = (isset($levels[$severity])) ? $levels[$severity] : $severity;
// Contain the content for buffering
ob_start();
include(APP_PATH.'/errors/error.php');
$buffer = ob_get_contents();
ob_end_clean();
echo $buffer;
2011-12-27 13:24:28 -05:00
}
// --------------------------------------------------------------------------
2011-12-27 13:24:28 -05:00
/**
* 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();
// 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;
}
// --------------------------------------------------------------------------
/**
* Calls the appropriate module/controller/function based on the url
*/
function route()
{
$controller = "welcome";
$module = "welcome";
$func = "index";
if( ! empty($_SERVER['PATH_INFO']))
{
$segments = explode('/', $_SERVER['PATH_INFO']);
}
load_file("controllers/{$controller}", $module);
$class = new $controller;
call_user_func(array($class, $func));
}
// --------------------------------------------------------------------------
//Set error handlers
set_error_handler('on_error');
set_exception_handler('on_exception');
// Load Most Common libraries
require_once('miniMVC.php');
require_once('output.php');
require_once('page.php');
require_once('db.php');
//Route to the appropriate function
route();
// End of common.php