miniMVC/sys/common.php

266 lines
5.0 KiB
PHP
Raw Normal View History

2011-12-27 13:24:28 -05:00
<?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
* @param string $module
* @param string $curr_path
* @return void
2011-12-27 13:24:28 -05:00
*/
function load_file($file, $curr_path="", $module="")
2011-12-27 13:24:28 -05:00
{
if($curr_path === "app")
{
$path = APP_PATH."{$file}.php";
}
else if($curr_path === "sys")
{
$path = SYS_PATH."{$file}.php";
}
if($module !== "")
{
$path = MOD_PATH."{$module}/{$file}.php";
}
else
{
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."default/{$file}.php";
}
}
require_once($path);
2011-12-27 13:24:28 -05:00
}
/**
* 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);
// Include the file
_find_file($file);
$buffer = ob_get_contents();
ob_end_clean();
if($return)
{
return $buffer;
}
echo $buffer;
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 => 'Runtime Notice'
2011-12-27 13:24:28 -05:00
);
$severity = (isset($levels[$severity])) ? $levels[$severity] : $severity;
// Contain the content for buffering
ob_start();
// Extract the data array
include(APP_PATH.'/errors/error.php');
$buffer = ob_get_contents();
ob_end_clean();
echo $buffer;
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();
/*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 error handlers
set_error_handler('on_error');
set_exception_handler('on_exception');
/**
* JSObject
*
* Class for creating object-literal-like constructs in PHP
*/
class JSObject {
/**
* Constructor for creating the objects
*/
function __construct()
{
$args = func_get_args();
$members = $args[0];
// Add the passed parameters to the object
foreach($members as $name => $value)
{
if(is_array($value))
{
$value = new JSObject($value);
}
$this->$name = $value;
}
}
/**
* PHP magic method to facilitate dynamic methods
*
* @param string $name
* @param array $args
*/
function __call($name, $args)
{
if(is_callable($this->$name))
{
//Call the dynamic function
return call_user_func_array($this->$name, $args);
}
}
/**
* Prints out the contents of the object when used as a string
*
* @return string
*/
function __toString()
{
$args = func_get_args();
$method = ( ! empty($args)) ? $args[0] : "print_r";
$output = '<pre>';
if($method == "var_dump")
{
ob_start();
var_dump($this);
$output .= ob_get_contents();
ob_end_clean();
}
else if($method == "var_export")
{
ob_start();
var_export($this);
$output .= ob_get_contents();
ob_end_clean();
}
else
{
$output .= print_r($this, TRUE);
}
return $output . '</pre>';
}
/**
* Constructor without the "new"
*
* @param array $members
*/
function __invoke($members=array())
{
return new JSObject($members);
}
}