First commit

This commit is contained in:
Timothy Warren 2011-12-27 13:24:28 -05:00
commit 8fff8a2df2
7 changed files with 501 additions and 0 deletions

0
README.md Normal file
View File

25
app/errors/error.php Normal file
View File

@ -0,0 +1,25 @@
<div class="error">
<h4>A PHP Error was encountered</h4>
<p>Severity: <?php echo $severity; ?></p>
<p>Message: <?php echo $message; ?></p>
<p>Filename: <?php echo $filepath; ?></p>
<p>Line Number: <?php echo $line; ?></p>
<?php if(defined('SHOW_DEBUG_BACKTRACE') && SHOW_DEBUG_BACKTRACE === TRUE): ?>
<p>Backtrace: </p>
<?php foreach(debug_backtrace() as $error): ?>
<?php if(isset($error['file']) && ! stristr($error['file'], SYS_DIR)): ?>
<p style="margin-left:10px">
File: <?php echo $error['file'] ?><br />
Line: <?php echo $error['line'] ?><br />
Function: <?php echo $error['function'] ?>
</p>
<?php endif ?>
<?php endforeach ?></p>
<?php endif ?>
</div>

30
index.php Normal file
View File

@ -0,0 +1,30 @@
<?php
// Change this in a live environment!
error_reporting(-1);
/*
|--------------------------------------------------------------------------
| Display Debug backtrace
|--------------------------------------------------------------------------
|
| If set to TRUE, a backtrace will be displayed along with php errors.
|
*/
define('SHOW_DEBUG_BACKTRACE', TRUE);
// Set the default paths
define('SYS_PATH', __DIR__.'/sys/');
define('MOD_PATH', __DIR__.'/modules/');
define('APP_PATH', __DIR__.'/app/');
// Require the most important files
require(SYS_PATH . "miniMVC.php");
require(SYS_PATH . 'db.php');
$MM =& get_instance();
$MM->db = new MM_db('pgsql');
echo $MM->__toString();

View File

@ -0,0 +1,10 @@
<?php
class Test extends MM_Controller {
function __construct()
{
parent::__construct();
}
}

107
sys/common.php Normal file
View File

@ -0,0 +1,107 @@
<?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
* @return string
*/
function _find_file($file)
{
}
/**
* 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);
//
}
/**
* Custom error handler
*/
function on_error($num, $string, $file, $line, $context)
{
$data = array(
);
}
/**
* 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_exception_handler('on_exception');

140
sys/db.php Normal file
View File

@ -0,0 +1,140 @@
<?php
//Include the database config file
require(APP_PATH.'config/db.php');
/**
* Just for giggles extend PHP's PDO class
*/
class MM_PDO extends PDO {
function __construct($dbname, $options=array())
{
global $db_conf;
if( ! isset($dbname))
{
$dbname = "default";
}
// Array manipulation is too verbose
extract($db_conf[$dbname]);
// 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}" : "";
}
// Set port if applicable
if(isset($port))
{
$dsn .= ($port !== "") ? ";port={$port}" : "";
}
if($user === "" && $pass === "")
{
$user = null;
$pass = null;
}
$opts = array(
PDO::ATTR_PERSISTENT => (isset($persist)) ? $persist : FALSE,
);
if( ! isset($persist))
{
unset($opts[PDO::ATTR_PERSISTENT]);
}
$options = array_merge($opts, $options);
parent::__construct($dsn, $user, $pass, $options);
}
// --------------------------------------------------------------------------
/**
* 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);
}
}
// --------------------------------------------------------------------------
/**
* 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 MM_PDO($members);
}
}
class MM_db extends MM_PDO {
function __construct($dbname)
{
parent::__construct($dbname);
}
function __destruct()
{
}
}

189
sys/miniMVC.php Normal file
View File

@ -0,0 +1,189 @@
<?php
//Include common function
require('common.php');
/**
* 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);
}
}
/**
* Base class for the framework
*/
class miniMVC extends JSObject {
function __construct()
{
$this->buffer = "";
$this->headers = array();
parent::__construct(array(
'output' => array(
'set_header' => function($key, $val)
{
$this->headers[$key] = $val;
},
'append_output' => function($string)
{
$this->buffer .= $string;
},
'set_output' => function($string)
{
$this->buffer = $string;
}
),
));
}
/**
* PHP magic method called when ending the script
* Used for outputing HTML
*/
function __destruct()
{
// Set headers
foreach($this->headers as $key => $val)
{
@header("$key: $val");
}
}
/**
* PHP magic method to facilitate dynamic class loading
*
* @param string $name
*/
function __get($name)
{
$path = SYS_PATH."{$name}.php";
$class = "MM_{$key}";
//if(is_file($path))
//{
include($path);
if(class_exists($class, FALSE))
{
$this->$name = new $class();
}
//}
}
}
// --------------------------------------------------------------------------
class MM_Model extends miniMVC {
function __construct()
{
parent::__construct();
}
function __destruct()
{
parent::__destruct();
}
}
// --------------------------------------------------------------------------
class MM_Controller extends miniMVC {
function __construct()
{
parent::__construct();
}
function __destruct()
{
parent::__destruct();
}
}