Minor restructuring, db class improvements

This commit is contained in:
Timothy Warren 2012-01-05 16:31:50 -05:00
parent 656fc8cba0
commit 42ffdd0ad9
5 changed files with 147 additions and 93 deletions

View File

@ -20,3 +20,10 @@ require(APP_PATH.'config/config.php');
// Require the most important files // Require the most important files
require(SYS_PATH . "common.php"); require(SYS_PATH . "common.php");
//Set error handlers
register_shutdown_function('shutdown');
set_error_handler('on_error');
set_exception_handler('on_exception');
// And away we go!
route();

View File

@ -32,6 +32,8 @@ TXT;
} }
} }
// --------------------------------------------------------------------------
/** /**
* Function to search through the tree to find the necessary file * Function to search through the tree to find the necessary file
* *
@ -124,6 +126,24 @@ function on_exception($exception)
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/**
* Utility function to check if a variable is set, and is an array or object
*
* @param mixed $var
* @return bool
*/
function is_like_array(&$var)
{
if( ! isset($var))
{
return FALSE;
}
return (is_array($var) OR is_object($var)) && ( ! empty($var));
}
// --------------------------------------------------------------------------
/** /**
* General 404 function * General 404 function
*/ */
@ -315,7 +335,7 @@ function route()
*/ */
function site_url($segment) function site_url($segment)
{ {
return $url = BASE_URL . URL_INDEX_FILE . $segment; return $url = BASEURL . URL_INDEX_FILE . $segment;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -358,18 +378,10 @@ function do_curl($url, $options=array())
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
//Set error handlers
register_shutdown_function('shutdown');
set_error_handler('on_error');
set_exception_handler('on_exception');
// Load Most Common libraries // Load Most Common libraries
require_once('miniMVC.php'); require_once('miniMVC.php');
require_once('output.php'); require_once('output.php');
require_once('page.php'); require_once('page.php');
require_once('db.php'); require_once('db.php');
//Route to the appropriate function // End of common.php
route();
// End of common.php

View File

@ -12,7 +12,7 @@ class db extends PDO {
if ( ! isset(self::$instance[$dbname])) if ( ! isset(self::$instance[$dbname]))
{ {
//echo 'Creating new instance of db class.'; //echo 'Creating new instance of db class.';
self::$instance[$dbname] = new db($dbname); self::$instance[$dbname] = new db($dbname, $options);
} }
return self::$instance[$dbname]; return self::$instance[$dbname];
@ -20,11 +20,22 @@ class db extends PDO {
function __construct($dbname="default", $options=array()) function __construct($dbname="default", $options=array())
{ {
//Include the database config file // Include the database config file
require(APP_PATH . 'config/db.php'); require(APP_PATH.'config/db.php');
// Array manipulation is too verbose // Get the correct database in the config file
extract($db_conf[$dbname]); if(is_like_array($db_conf[$dbname]))
{
// Array manipulation is too verbose
extract($db_conf[$dbname]);
}
else
{
// Apparently the database doesn't exist
$this->get_last_error();
trigger_error("Database does not exist", E_USER_ERROR);
die();
}
// Sqlite doesn't use dbname param // Sqlite doesn't use dbname param
$dsn = (stripos($type, "sqlite") === FALSE) ? "{$type}:dbname={$db}" : "{$type}:{$db}"; $dsn = (stripos($type, "sqlite") === FALSE) ? "{$type}:dbname={$db}" : "{$type}:{$db}";
@ -48,8 +59,7 @@ class db extends PDO {
} }
$opts = array( $opts = array(
//PDO::ATTR_PERSISTENT => (isset($persist)) ? $persist : FALSE, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
//PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING,
); );
if( ! isset($persist)) if( ! isset($persist))
@ -57,11 +67,20 @@ class db extends PDO {
unset($opts[PDO::ATTR_PERSISTENT]); unset($opts[PDO::ATTR_PERSISTENT]);
} }
$options = array_merge($opts, $options); $options = $opts + $options;
parent::__construct($dsn, $user, $pass, $options); try
{
parent::__construct($dsn, $user, $pass, $options);
}
catch(Exception $e)
{
if($e->getMessage() !== "The auto-commit mode cannot be changed for this driver")
{
get_last_error();
}
}
self::$instance[$dbname] =& $this;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -139,49 +158,52 @@ class db extends PDO {
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Constructor without the "new" * Simplifies prepared statements for database queries
* *
* @param array $members * @param string $sql
* @param array $data
* @return mixed PDOStatement / FALSE
*/ */
/*function __invoke($db="default") function prepare_query($sql, $data)
{ {
return self::get_instance($db); // Prepare the sql
} $query = $this->prepare($sql);
function start_transaction() if( ! is_like_array($query))
{
if( ! $this->inTransaction())
{
return $this->beginTransaction();
}
}
function commit()
{
if($this->inTransaction())
{
return parent::commit();
}
}
function rollback()
{
if($this->inTransaction())
{ {
return parent::rollBack(); $this->get_last_error();
return FALSE;
} }
}
// Set the statement in the class variable for easy later access
function prepare($sql, $driver_options=array()) $this->statement =& $query;
{
$this->statement = parent::prepare($sql, $driver_options);
return $this->statement; if( ! is_like_array($data))
} {
trigger_error("Invalid data argument");
function set() return FALSE;
{ }
}*/ // Bind the parameters
foreach($data as $placeholder => $value)
{
$res = $query->bindParam($placeholder, $value);
if( ! $res)
{
trigger_error("Parameter not successfully bound");
return FALSE;
}
}
return $query;
}
// --------------------------------------------------------------------------
/** /**
* Returns the last error from the database * Returns the last error from the database
@ -216,4 +238,4 @@ class db extends PDO {
} }
} }
// End of db.php // End of db.php

View File

@ -57,35 +57,38 @@ class JSObject {
*/ */
function __toString() function __toString()
{ {
$args = func_get_args();
$method = ( ! empty($args)) ? $args[0] : "print_r";
$data = (isset($args[1])) ? $args[1] : array();
// 32762 == (E_ALL|E_NOTICE|E_STRICT) & ~(E_ERROR | E_PARSE) // 32762 == (E_ALL|E_NOTICE|E_STRICT) & ~(E_ERROR | E_PARSE)
if(ini_get('error_reporting') == 32762) if(ini_get('error_reporting') == 32762 && empty($data))
{ {
$args = func_get_args(); $data =& $this;
$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>';
} }
$output = '<pre>';
if($method == "var_dump")
{
ob_start();
var_dump($data);
$output .= ob_get_contents();
ob_end_clean();
}
else if($method == "var_export")
{
ob_start();
var_export($data);
$output .= ob_get_contents();
ob_end_clean();
}
else
{
$output .= print_r($data, TRUE);
}
return $output . '</pre>';
} }
/** /**
@ -353,12 +356,15 @@ class MM_Controller extends miniMVC {
$path = MOD_PATH . "{$module}/models/{$file}.php"; $path = MOD_PATH . "{$module}/models/{$file}.php";
} }
require_once($path); if(is_file($path))
{
require_once($path);
}
if( ! empty($args)) if( ! empty($args))
{ {
$this->$file = new $file(extract($args)); $this->$file = new $file($args);
} }
else else
{ {
@ -379,9 +385,9 @@ class MM_Model extends miniMVC {
/** /**
* Adds the database class to the current model class * Adds the database class to the current model class
*/ */
function load_db($name) function load_db($name="default")
{ {
$this->db = new db($name); $this->db = db::get_instance($name);
} }
} }

View File

@ -1,5 +1,8 @@
<?php <?php
/**
* Class for content output
*/
class Output extends miniMVC { class Output extends miniMVC {
private $buffer, $headers; private $buffer, $headers;
@ -10,6 +13,8 @@ class Output extends miniMVC {
$this->headers = array(); $this->headers = array();
} }
// --------------------------------------------------------------------------
/** /**
* PHP magic method called when ending the script * PHP magic method called when ending the script
* Used for outputing HTML * Used for outputing HTML
@ -39,12 +44,10 @@ class Output extends miniMVC {
echo $this->buffer; echo $this->buffer;
ob_end_flush(); ob_end_flush();
} }
else
{
echo "No page content";
}
} }
// --------------------------------------------------------------------------
/** /**
* Sets a header for later output * Sets a header for later output
* @param string $key * @param string $key
@ -55,6 +58,8 @@ class Output extends miniMVC {
$this->headers[$key] = $val; $this->headers[$key] = $val;
} }
// --------------------------------------------------------------------------
/** /**
* Adds text to the output buffer * Adds text to the output buffer
* *
@ -65,6 +70,8 @@ class Output extends miniMVC {
$this->buffer .= $string; $this->buffer .= $string;
} }
// --------------------------------------------------------------------------
/** /**
* Sets the output buffer * Sets the output buffer
* *