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();
//$filepath = str_replace(BASE_PATH, "", $filepath);
// 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}';
// Contain the content for buffering
ob_start();
// write tracelines into main template
$msg = sprintf(
$msg,
get_class($exception),
$exception->getMessage(),
$exception->getFile(),
$exception->getLine(),
implode("<br />", $result),
$exception->getFile(),
$exception->getLine()
);
include(APP_PATH.'/errors/error_php_exception.php');
echo $msg;
$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;
}
// --------------------------------------------------------------------------
@ -83,6 +86,22 @@ class db extends PDO {
// --------------------------------------------------------------------------
/**
* 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);
}
}
// --------------------------------------------------------------------------
/**
* Prints out the contents of the object when used as a string
*
@ -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 end_transaction()
function commit()
{
if($this->inTransaction())
{
return parent::commit();
}
}
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,44 +1,21 @@
<?php
/**
* Base class for the framework
*/
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;
}
/**
* Constructor - Any classes loaded here become subclasses of miniMVC
* Parent class of base class, contains much of the magic
*/
public function __construct()
{
self::$instance =& $this;
}
class JSObject {
/**
* Magic function called when cloning an object
* Constructor for creating the objects
*/
public function __clone()
function __construct($members = array())
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
// Add the passed parameters to the object
foreach($members as $name => $value)
{
$this->$name = $value;
}
}
/**
@ -49,13 +26,13 @@ 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);
}
}
@ -73,13 +50,15 @@ class miniMVC{
}
}
/**
* Prints out the contents of the object when used as a string
*
* @return string
*/
function __toString()
{
// 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";
@ -107,6 +86,7 @@ class miniMVC{
return $output . '</pre>';
}
}
/**
* PHP magic method to facilitate dynamic class loading
@ -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')
{
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,9 +355,17 @@ class MM_Controller extends miniMVC {
require_once($path);
if( ! empty($args))
{
$this->$file = new $file(extract($args));
}
else
{
$this->$file = new $file;
}
}
}
// --------------------------------------------------------------------------