Some DB fixes and error handling improvements

This commit is contained in:
Timothy Warren 2012-01-04 19:47:32 -05:00
parent df35da10bb
commit 656fc8cba0
9 changed files with 287 additions and 130 deletions

View File

@ -9,5 +9,5 @@ $db_conf = array(
'db' => '',
'prefix' => '',
'persist' => '',
),
)
);

7
app/errors/error_db.php Normal file
View File

@ -0,0 +1,7 @@
<div style="position:relative; margin:0.5em auto; padding:0.5em; width:95%; border:1px solid #924949; background: #f3e6e6;">
<h4>A Database Error was encountered</h4>
<p>Code: <?php echo $code ?></p>
<p>Driver Code: <?php echo $driver_code ?></p>
<p>Message: <?php echo $message ?></p>
</div>

View File

@ -3,16 +3,7 @@
<head>
<title>Error</title>
<style type="text/css">
.message{
position:relative;
margin:0.5em auto;
padding:0.5em;
width:95%;
}
.error{
border:1px solid #924949;
background: #f3e6e6;
}
div{position:relative; margin:0.5em auto; padding:0.5em; width:95%; border:1px solid #924949; background: #f3e6e6;}
</style>
</head>
<body>

View File

@ -1,4 +1,4 @@
<div class="message error">
<div style="position:relative; margin:0.5em auto; padding:0.5em; width:95%; border:1px solid #924949; background: #f3e6e6;">
<h4>A PHP Error was encountered</h4>
<p>Severity: <?php echo $severity; ?></p>
@ -13,7 +13,7 @@
<?php if(isset($error['file']) && ! stristr($error['file'], SYS_PATH)): ?>
<p style="margin-left:10px">
File: <?php echo $error['file'] ?><br />
File: <?php echo str_replace(BASE_PATH, "", $error['file']) ?><br />
Line: <?php echo $error['line'] ?><br />
Function: <?php echo $error['function'] ?>
</p>

View File

@ -0,0 +1,23 @@
<div style="position:relative; margin:0.5em auto; padding:0.5em; width:95%; border:1px solid #924949; background: #f3e6e6;">
<h4>An uncaught exception was thrown.</h4>
<p>Message: <?php echo $message; ?></p>
<?php if(defined('SHOW_DEBUG_BACKTRACE') && SHOW_DEBUG_BACKTRACE === TRUE): ?>
<p>Backtrace: </p>
<?php foreach($exception->getTrace() as $error): ?>
<?php if(isset($error['file']) && ! stristr($error['file'], SYS_PATH)): ?>
<p style="margin-left:10px">
File: <?php echo str_replace(BASE_PATH, "", $error['file']) ?><br />
Line: <?php echo $error['line'] ?><br />
Function: <?php echo $error['function'] /*<br />
Args: <pre style="margin-left:15px"><?php echo print_r($error['args'], TRUE) ?></pre> */ ?>
</p>
<?php endif ?>
<?php endforeach ?></p>
<?php endif ?>
</div>

View File

@ -1,7 +1,7 @@
<?php
// Change this in a live environment!
error_reporting(-1);
error_reporting((-1) & ~(E_ERROR | E_PARSE));
// Set the default paths
define('BASE_PATH', __DIR__);

View File

@ -108,41 +108,18 @@ function on_error($severity, $message, $filepath, $line, $context)
*/
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";
$message = $exception->getMessage();
// 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;
//$filepath = str_replace(BASE_PATH, "", $filepath);
// Contain the content for buffering
ob_start();
include(APP_PATH.'/errors/error_php_exception.php');
$buffer = ob_get_contents();
ob_end_clean();
echo $buffer;
}
// --------------------------------------------------------------------------
@ -338,7 +315,7 @@ function route()
*/
function site_url($segment)
{
return $url = BASEURL . URL_INDEX_FILE . $segment;
return $url = BASE_URL . URL_INDEX_FILE . $segment;
}
// --------------------------------------------------------------------------

View File

@ -4,24 +4,24 @@
*/
class db extends PDO {
private $where;
private $statement;
private static $instance;
public static function get_instance($dbname="default")
public static function get_instance($dbname="default", $options=array())
{
if ( ! isset(self::$instance))
if ( ! isset(self::$instance[$dbname]))
{
echo 'Creating new instance of db class.';
self::$instance = new db($dbname);
//echo 'Creating new instance of db class.';
self::$instance[$dbname] = new db($dbname);
}
return self::$instance;
return self::$instance[$dbname];
}
function __construct($dbname="default", $options=array())
{
//Include the database config file
load_file('config/db','app');
require(APP_PATH . 'config/db.php');
// Array manipulation is too verbose
extract($db_conf[$dbname]);
@ -48,7 +48,8 @@ class db extends PDO {
}
$opts = array(
PDO::ATTR_PERSISTENT => (isset($persist)) ? $persist : FALSE,
//PDO::ATTR_PERSISTENT => (isset($persist)) ? $persist : FALSE,
//PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING,
);
if( ! isset($persist))
@ -59,6 +60,8 @@ class db extends PDO {
$options = array_merge($opts, $options);
parent::__construct($dsn, $user, $pass, $options);
self::$instance[$dbname] =& $this;
}
// --------------------------------------------------------------------------
@ -80,6 +83,22 @@ class db extends PDO {
return call_user_func_array($this->$name, $args);
}
}
// --------------------------------------------------------------------------
/**
* PHP magic methods to call non-static methods statically
*
* @param string $name
* @param array $args
*/
public static function __callStatic($name, $args)
{
if(is_callable(parent::$name))
{
return call_user_func_array(parent::$name, $args);
}
}
// --------------------------------------------------------------------------
@ -124,18 +143,76 @@ class db extends PDO {
*
* @param array $members
*/
function __invoke($db="default")
/*function __invoke($db="default")
{
return self::get_instance($db);
}
function start_transaction()
{
if( ! $this->inTransaction())
{
return $this->beginTransaction();
}
}
function commit()
{
if($this->inTransaction())
{
return parent::commit();
}
}
function end_transaction()
function rollback()
{
if($this->inTransaction())
{
return parent::rollBack();
}
}
function prepare($sql, $driver_options=array())
{
$this->statement = parent::prepare($sql, $driver_options);
return $this->statement;
}
function set()
{
}*/
/**
* Returns the last error from the database
*
* @return string
*/
function get_last_error()
{
$error = array();
if(isset($this->statement))
{
$error = $this->statement->errorInfo();
}
else
{
$error = $this->errorInfo();
}
$code = $error[0];
$driver_code = $error[1];
$message = $error[2];
// Contain the content for buffering
ob_start();
include(APP_PATH.'/errors/error_db.php');
$buffer = ob_get_contents();
ob_end_clean();
echo $buffer;
}
}

View File

@ -1,47 +1,24 @@
<?php
/**
* Base class for the framework
* Parent class of base class, contains much of the magic
*/
class miniMVC{
private static $instance;
private static $count;
public static function get_instance()
{
if( ! isset(self::$count))
{
self::$count = 0;
}
if ( ! isset(self::$instance))
{
self::$count++;
self::$instance = new miniMVC;
}
$self =& self::$instance;
return $self;
}
class JSObject {
/**
* Constructor - Any classes loaded here become subclasses of miniMVC
*/
public function __construct()
{
self::$instance =& $this;
}
/**
* Magic function called when cloning an object
*/
public function __clone()
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
/**
* Constructor for creating the objects
*/
function __construct($members = array())
{
// Add the passed parameters to the object
foreach($members as $name => $value)
{
$this->$name = $value;
}
}
/**
* PHP magic method to facilitate dynamic methods
*
* @param string $name
@ -49,16 +26,16 @@ class miniMVC{
*/
function __call($name, $args)
{
if(is_callable(self::$instance->$name))
if(is_callable($this->$name))
{
//Add $this object to args
array_push($args, $this);
//Call the dynamic function
return call_user_func_array(self::$instance->$name, $args);
return call_user_func_array($this->$name, $args);
}
}
/**
* PHP magic method to facilitate dynamically set static methods
*
@ -73,7 +50,6 @@ class miniMVC{
}
}
/**
* Prints out the contents of the object when used as a string
*
@ -81,31 +57,35 @@ class miniMVC{
*/
function __toString()
{
$args = func_get_args();
$method = ( ! empty($args)) ? $args[0] : "print_r";
// 32762 == (E_ALL|E_NOTICE|E_STRICT) & ~(E_ERROR | E_PARSE)
if(ini_get('error_reporting') == 32762)
{
$args = func_get_args();
$method = ( ! empty($args)) ? $args[0] : "print_r";
$output = '<pre>';
$output = '<pre>';
if($method == "var_dump")
{
ob_start();
var_dump($this);
$output .= ob_get_contents();
ob_end_clean();
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>';
}
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>';
}
/**
@ -135,6 +115,60 @@ class miniMVC{
}
}
/**
* PHP magic method that is called when an object is treated as a function
*/
public static function __invoke()
{
$class = __CLASS__;
return new $class;
}
}
// --------------------------------------------------------------------------
/**
* Base class for the framework
*/
class miniMVC extends JSObject{
private static $instance;
private static $count;
/**
* Constructor - Any classes loaded here become subclasses of miniMVC
*/
public function __construct()
{
self::$instance =& $this;
}
/**
* PHP magic method to facilitate dynamic methods
*
* @param string $name
* @param array $args
*/
function __call($name, $args)
{
if(is_callable(self::$instance->$name))
{
//Add $this object to args
array_push($args, $this);
//Call the dynamic function
return call_user_func_array(self::$instance->$name, $args);
}
}
/**
* Magic function called when cloning an object
*/
public function __clone()
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
/**
* PHP magic method that is called when an object is treated as a function
*/
@ -143,14 +177,54 @@ class miniMVC{
return self::get_instance();
}
/**
* Singleton getter function
*
* @return miniMVC object
*/
public static function get_instance()
{
if( ! isset(self::$count))
{
self::$count = 0;
}
if ( ! isset(self::$instance))
{
self::$count++;
self::$instance = new miniMVC;
}
$self =& self::$instance;
return $self;
}
/**
* Method to load classes into the singleton
*
* @param string $name
*/
function load_class($name)
function load_class($name, $type='class')
{
$path = APP_PATH . "classes/{$name}.php";
switch($type)
{
default:
$path = APP_PATH . "classes/{$name}.php";
break;
case "sys":
$path = SYS_PATH . "{$name}.php";
break;
}
// In a subdirectory? No problem
if(strpos("/", $name) !== FALSE)
{
$n = explode("/", $name);
$name = $n[count($n) -1];
}
$class = "{$name}";
if(class_exists($class, FALSE))
@ -262,7 +336,7 @@ class MM_Controller extends miniMVC {
*
* @param string $file
*/
function load_model($file)
function load_model($file, $args=array())
{
$path = "";
@ -281,7 +355,15 @@ class MM_Controller extends miniMVC {
require_once($path);
$this->$file = new $file;
if( ! empty($args))
{
$this->$file = new $file(extract($args));
}
else
{
$this->$file = new $file;
}
}
}