miniMVC/src/sys/miniMVC.php

286 lines
5.8 KiB
PHP
Raw Normal View History

2012-01-13 11:58:06 -05:00
<?php
/**
* MiniMVC
*
* Convention-based micro-framework for PHP
*
* @author Timothy J. Warren
* @copyright Copyright (c) 2011 - 2012
* @link https://github.com/timw4mail/miniMVC
* @license http://philsturgeon.co.uk/code/dbad-license
*/
2012-01-13 11:58:06 -05:00
// --------------------------------------------------------------------------
2012-01-13 11:58:06 -05:00
/**
* Extend PHP's PDO class to add some more functionality
*
* @extends PDO
2012-01-13 11:58:06 -05:00
*/
class db extends PDO {
2012-01-13 11:58:06 -05:00
private $statement;
private static $instance = array();
public static function &get_instance($dbname="default", $options=array())
{
if ( ! isset(self::$instance[$dbname]))
{
//echo 'Creating new instance of db class.';
self::$instance[$dbname] = self::_get_conf($dbname, $options);
}
2012-01-13 11:58:06 -05:00
return self::$instance[$dbname];
}
// --------------------------------------------------------------------------
2012-01-13 11:58:06 -05:00
/**
* Makes DSN from config file, and creates database object
2012-01-13 11:58:06 -05:00
*
* @param string $dbname
* @param array $options
* @return db object
2012-01-13 11:58:06 -05:00
*/
private static function _get_conf($dbname="default", $options=array())
2012-01-13 11:58:06 -05:00
{
// Include the database config file
require(APP_PATH.'config/db.php');
2012-01-13 11:58:06 -05:00
// Get the correct database in the config file
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
$dsn = (stripos($type, "sqlite") === FALSE) ? "{$type}:dbname={$db}" : "{$type}:{$db}";
// Set hostname if applicable
if(isset($host))
{
$dsn .= ($host !== "") ? ";host={$host}" : "";
2012-01-13 11:58:06 -05:00
}
// Set port if applicable
if(isset($port))
{
$dsn .= ($port !== "") ? ";port={$port}" : "";
}
$user = ( ! empty($user)) ? $user : null;
$pass = ( ! empty($pass)) ? $pass : null;
// Pre-set the error mode
$opts = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
);
$options = $opts + $options;
return new db($dsn, $user, $pass, $options);
2012-01-13 11:58:06 -05:00
}
// --------------------------------------------------------------------------
/**
* Constructor to override PDO constructor - Quercus doesn't seem to override
* the parent constructor unless the arguments match.
*
* @param string $dsn
* @param string $user
* @param string $pass
* @param array $options
*/
function __construct($dsn, $user, $pass, $options)
{
// Let's try connecting now!
parent::__construct($dsn, $user, $pass, $options);
2012-01-13 11:58:06 -05:00
}
// --------------------------------------------------------------------------
2012-01-13 11:58:06 -05:00
/**
* Magic function called when cloning an object
2012-01-13 11:58:06 -05:00
*/
public function __clone()
2012-01-13 11:58:06 -05:00
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
2012-01-13 11:58:06 -05:00
}
// --------------------------------------------------------------------------
/**
* PHP magic method to facilitate dynamic methods
*
* @param string $name
* @param array $args
*/
function __call($name, $args)
{
if(is_callable($this->$name))
{
//Add $this to the beginning of the args array
array_unshift($args, $this);
//Call the dynamic function
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);
}
}
// --------------------------------------------------------------------------
2012-01-13 11:58:06 -05:00
/**
* Prints out the contents of the object when used as a string
*
* @return string
*/
function __toString()
{
if(ENVIRONMENT == 'DEVELOPMENT')
2012-01-13 11:58:06 -05:00
{
$args = func_get_args();
$method = ( ! empty($args)) ? $args[0] : "print_r";
$data = (isset($args[1])) ? $args[1] : array();
2012-01-13 11:58:06 -05:00
if(empty($data))
{
$data =& $this;
}
$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);
}
2012-01-13 11:58:06 -05:00
return $output . '</pre>';
2012-01-13 11:58:06 -05:00
}
else
{
return '';
2012-01-13 11:58:06 -05:00
}
}
// --------------------------------------------------------------------------
2012-01-13 11:58:06 -05:00
/**
* Simplifies prepared statements for database queries
2012-01-13 11:58:06 -05:00
*
* @param string $sql
* @param array $data
* @return mixed PDOStatement / FALSE
2012-01-13 11:58:06 -05:00
*/
function prepare_query($sql, $data)
2012-01-13 11:58:06 -05:00
{
// Prepare the sql
$query = $this->prepare($sql);
2012-01-13 11:58:06 -05:00
if( ! is_like_array($query))
2012-01-13 11:58:06 -05:00
{
$this->get_last_error();
return FALSE;
2012-01-13 11:58:06 -05:00
}
// Set the statement in the class variable for easy later access
$this->statement =& $query;
2012-01-13 11:58:06 -05:00
if( ! is_like_array($data))
2012-01-13 11:58:06 -05:00
{
trigger_error("Invalid data argument");
return FALSE;
2012-01-13 11:58:06 -05:00
}
// Bind the parameters
foreach($data as $k => $value)
2012-01-13 11:58:06 -05:00
{
$res = $query->bindValue($k, $value);
2012-01-13 11:58:06 -05:00
if( ! $res)
2012-01-13 11:58:06 -05:00
{
trigger_error("Parameter not successfully bound");
return FALSE;
2012-01-13 11:58:06 -05:00
}
}
return $query;
2012-01-13 11:58:06 -05:00
}
// --------------------------------------------------------------------------
2012-01-13 11:58:06 -05:00
/**
* Returns the last error from the database
2012-01-13 11:58:06 -05:00
*
* @return string
2012-01-13 11:58:06 -05:00
*/
function get_last_error()
2012-01-13 11:58:06 -05:00
{
$error = array();
if(isset($this->statement))
2012-01-13 11:58:06 -05:00
{
$error = $this->statement->errorInfo();
2012-01-13 11:58:06 -05:00
}
else
{
$error = $this->errorInfo();
2012-01-13 11:58:06 -05:00
}
$code = $error[0];
$driver_code = $error[1];
$message = $error[2];
2012-01-13 11:58:06 -05:00
// Contain the content for buffering
ob_start();
2012-01-13 11:58:06 -05:00
include(APP_PATH.'/errors/error_db.php');
2012-01-13 11:58:06 -05:00
$buffer = ob_get_contents();
ob_end_clean();
echo $buffer;
2012-01-13 11:58:06 -05:00
}
}
// End of db.php