Various improvements, need to fix class auto-loading

This commit is contained in:
Timothy Warren 2011-12-28 19:33:41 -05:00
parent e03cad02be
commit c5634b182a
9 changed files with 437 additions and 370 deletions

View File

@ -1,37 +1,6 @@
<?php
$config = array(
/*
|--------------------------------------------------------------------------
| Base Url
|--------------------------------------------------------------------------
|
| This is the url path where the framework is located. Requires trailing
| slash.
|
*/
'base_url' => '',
/*
|--------------------------------------------------------------------------
| Content Domain
|--------------------------------------------------------------------------
|
| This is the domain used for serving content, such as css, javascript.
|
*/
'content_domain' => '',
/*
|--------------------------------------------------------------------------
| Static Lib Path
|--------------------------------------------------------------------------
|
| This is the path where the 'assets' directory is on the static domain.
|
*/
'static_lib_path' => $config['content_domain'].'assets/',
return array(
/*
|--------------------------------------------------------------------------
@ -42,7 +11,7 @@ $config = array(
| stylesheet minifier. This should not need to be changed.
|
*/
'style_path' => $config['static_lib_path'] . '/css.php/g/';
'style_path' => STATIC_LIB_PATH . '/css.php/g/',
/*
|--------------------------------------------------------------------------
@ -53,7 +22,7 @@ $config = array(
| javascript minifier. This should not need to be changed.
|
*/
'script_path' => $config['static_lib_path'] . '/js.php/g/';
'script_path' => STATIC_LIB_PATH . '/js.php/g/',
/*
@ -65,7 +34,7 @@ $config = array(
|
*/
'default_title' => "Tim's Home Page",
'default_title' => "miniMVC app",
/*
|--------------------------------------------------------------------------
@ -89,4 +58,4 @@ $config = array(
'default_js_group' => "js",
);
);

View File

@ -13,6 +13,37 @@ error_reporting(-1);
*/
define('SHOW_DEBUG_BACKTRACE', TRUE);
/*
|--------------------------------------------------------------------------
| Base Url
|--------------------------------------------------------------------------
|
| This is the url path where the framework is located. Requires trailing
| slash.
|
*/
define('BASE_URL', '');
/*
|--------------------------------------------------------------------------
| Content Domain
|--------------------------------------------------------------------------
|
| This is the domain used for serving content, such as css, javascript.
|
*/
define('CONTENT_DOMAIN', '');
/*
|--------------------------------------------------------------------------
| Static Lib Path
|--------------------------------------------------------------------------
|
| This is the path where the 'assets' directory is on the static domain.
|
*/
define('STATIC_LIB_PATH', CONTENT_DOMAIN.'assets/');
// Set the default paths
define('SYS_PATH', __DIR__.'/sys/');
@ -21,7 +52,4 @@ define('APP_PATH', __DIR__.'/app/');
// Require the most important files
require(SYS_PATH . "common.php");
require(SYS_PATH . "miniMVC.php");
require(SYS_PATH . 'db.php');
echo get_instance()->__toString();

View File

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

View File

@ -0,0 +1,17 @@
<?php
class Welcome extends miniMVC {
function __construct()
{
parent::__construct();
}
function index()
{
//$this->page->build_header();
$this->output->append_output($this->__toString());
//$this->page->build_footer();
}
}

View File

@ -7,28 +7,24 @@
/**
* Singleton function
*/
function get_instance($params=array())
function mm()
{
static $result = null;
if(is_null($result) === TRUE)
{
$result = new miniMVC($params);
}
return $result;
return miniMVC::singleton();
}
// --------------------------------------------------------------------------
/**
* Function to search through the tree to find the necessary file
*
* @param string $file
* @param string $module
* @param string $curr_path
* @return void
*/
function load_file($file, $curr_path="", $module="")
function load_file($file, $curr_path="")
{
$path = "";
if($curr_path === "app")
{
$path = APP_PATH."{$file}.php";
@ -37,14 +33,14 @@ function load_file($file, $curr_path="", $module="")
{
$path = SYS_PATH."{$file}.php";
}
if($module !== "")
{
$path = MOD_PATH."{$module}/{$file}.php";
}
else
{
if($curr_path !== "")
$path = MOD_PATH."{$curr_path}/{$file}.php";
}
if( ! is_file($path))
{
/*if($curr_path !== "")
{
$matches = array();
if(preg_match("`modules/([a-z 0-9~%.:_\-])/?`i", $curr_path, $matches))
@ -55,42 +51,15 @@ function load_file($file, $curr_path="", $module="")
}
}
else
{
$path = MOD_PATH."default/{$file}.php";
}
{*/
$path = MOD_PATH."welcome/{$file}.php";
//}
}
require_once($path);
include_once($path);
}
/**
* 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);
// Include the file
_find_file($file);
$buffer = ob_get_contents();
ob_end_clean();
if($return)
{
return $buffer;
}
echo $buffer;
}
// --------------------------------------------------------------------------
/**
* Custom error handler
@ -117,7 +86,6 @@ function on_error($severity, $message, $filepath, $line, $context)
// Contain the content for buffering
ob_start();
// Extract the data array
include(APP_PATH.'/errors/error.php');
$buffer = ob_get_contents();
@ -125,6 +93,8 @@ function on_error($severity, $message, $filepath, $line, $context)
echo $buffer;
}
// --------------------------------------------------------------------------
/**
* Custom exception handler
*/
@ -136,11 +106,6 @@ function on_exception($exception)
// 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();
@ -172,95 +137,44 @@ function on_exception($exception)
echo $msg;
}
//Set error handlers
set_error_handler('on_error');
set_exception_handler('on_exception');
// --------------------------------------------------------------------------
/**
* JSObject
*
* Class for creating object-literal-like constructs in PHP
* Calls the appropriate module/controller/function based on the url
*/
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()
function route()
{
$controller = "welcome";
$module = "welcome";
$func = "index";
if( ! empty($_SERVER['PATH_INFO']))
{
$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>';
$segments = explode('/', $_SERVER['PATH_INFO']);
}
/**
* Constructor without the "new"
*
* @param array $members
*/
function __invoke($members=array())
{
return new JSObject($members);
}
}
load_file("controllers/{$controller}", $module);
$class = new $controller;
call_user_func(array($class, $func));
}
// --------------------------------------------------------------------------
//Set error handlers
set_error_handler('on_error');
//set_exception_handler('on_exception');
// Load Most Common libraries
require_once('miniMVC.php');
require_once('output.php');
require_once('page.php');
require_once('db.php');
//Route to the appropriate function
route();
// End of common.php

View File

@ -1,21 +1,27 @@
<?php
//Include the database config file
load_file('config/db','app');
/**
* Just for giggles extend PHP's PDO class
*/
class MM_PDO extends PDO {
class db extends PDO {
private $where;
private static $instance;
function __construct($dbname, $options=array())
public static function get_instance()
{
if ( ! isset(self::$instance)) {
echo 'Creating new instance of db class.';
$className = __CLASS__;
self::$instance = new $className;
}
return self::$instance;
}
function __construct($dbname="default", $options=array())
{
global $db_conf;
if( ! isset($dbname))
{
$dbname = "default";
}
//Include the database config file
load_file('config/db','app');
// Array manipulation is too verbose
extract($db_conf[$dbname]);
@ -120,21 +126,8 @@ class MM_PDO extends PDO {
*/
function __invoke($members=array())
{
return new MM_PDO($members);
return self::$instance;
}
}
class MM_db extends MM_PDO {
function __construct($dbname)
{
parent::__construct($dbname);
}
function __destruct()
{
}
}
// End of db.php

View File

@ -1,55 +1,165 @@
<?php
/**
* Base class for the framework
*/
class miniMVC extends JSObject {
class miniMVC{
protected $buffer;
protected $headers;
private static $instance;
private $buffer, $headers;
public static function get_instance()
{
if ( ! isset(self::$instance)) {
echo 'Creating new instance.';
$className = __CLASS__;
self::$instance = new $className;
}
return self::$instance;
}
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;
}
),
));
// Load the page class
/*if( ! isset(self::$instance->page))
{
self::$instance->page &= new Page;
}
if( ! isset($this->output))
{
self::$instance->output &= new Output;
}*/
}
/**
* PHP magic method called when ending the script
* Used for outputing HTML
* Magic function called when cloning an object
*/
function __destruct()
{
// Set headers
foreach($this->headers as $key => $val)
{
if( ! isset($val))
{
@header($key);
}
else
{
@header("$key: $val");
}
public function __clone()
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
/**
* Function for loading a view
*
* @param string $file
* @param array $data
* @return mixed
*/
function load_view($file, $data, $return=FALSE)
{
$path = "";
// The module is the lower of the class name
// need to figure out a way to allow multiple controllers
// in one module
$module = strtolower(get_class($this));
$not_modules = array('miniMVC', 'page', 'db');
// If it's a module, look in the module view folder
if( ! in_array($module, $not_modules))
{
$path = MOD_PATH . "{$module}/views/{$file}.php";
}
// If it's not a module, or doesn't exist in the module view folder
// look in the app view folder
if( ! is_file($path))
{
$path = APP_PATH . "views/{$file}.php";
}
// Contain the content for buffering
ob_start();
// Extract the data array
extract($data);
// Include the file
include($path);
$buffer = ob_get_contents();
ob_end_clean();
if($return)
{
return $buffer;
}
$this->output->append_output($buffer);
}
/**
* 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);
}
}
/**
* PHP magic method to facilitate dynamically set static methods
*
* @param string $name
* @param array $args
*/
public static function __callStatic($name, $args)
{
if(is_callable(self::$name))
{
return call_user_func_array(self::$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>';
}
/**
@ -59,45 +169,27 @@ class miniMVC extends JSObject {
*/
function __get($name)
{
$path = SYS_PATH."{$name}.php";
$class = "MM_{$key}";
//$path = SYS_PATH."{$name}.php";
$class = "{$name}";
load_file($name, 'sys');
if(class_exists($class, FALSE))
if(class_exists($class, FALSE))
{
if( ! isset(self::$instance->$name))
{
$this->$name = new $class();
self::$instance->$name &= new $class;
return;
}
}
/*load_file($name, 'sys');
if(class_exists($class, FALSE))
{
self::$instance->$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();
}
}
// End of miniMVC.php

71
sys/output.php Normal file
View File

@ -0,0 +1,71 @@
<?php
class Output extends miniMVC {
function __construct()
{
$this->buffer = "";
$this->headers = array();
}
/**
* PHP magic method called when ending the script
* Used for outputing HTML
*/
function __destruct()
{
if( ! empty($this->headers))
{
// Set headers
foreach($this->headers as $key => $val)
{
if( ! isset($val))
{
@header($key);
}
else
{
@header("$key: $val");
}
}
}
if( ! empty($this->buffer))
{
echo $this->buffer;
}
}
/**
* Sets a header for later output
* @param string $key
* @param string $val
*/
function set_header($key, $val)
{
$this->headers[$key] = $val;
}
/**
* Adds text to the output buffer
*
* @param string $string
*/
function append_output($string)
{
$this->buffer .= $string;
}
/**
* Sets the output buffer
*
* @param string $string
*/
function set_output($string)
{
$this->buffer = $string;
}
}
// End of Output.php

View File

@ -1,15 +1,14 @@
<?php
(defined('BASEPATH')) OR exit('No direct script access allowed');
/**
* Class for building pages
*
* All methods are chainable, with the exception of the constructor,
* build_header(), build_footer(), build_page() and _headers() methods.
*/
class Page extends JSObject
class Page
{
private static $meta, $head_js, $foot_js, $css, $title, $head_tags, $body_class, $body_id, $base;
private $CI;
private $meta, $head_js, $foot_js, $css, $title, $head_tags, $body_class, $body_id, $base;
public function __construct()
{
@ -22,7 +21,9 @@ class Page extends JSObject
$this->body_class = "";
$this->body_id = "";
$this->base = "";
$this->MM =& get_instance();
$this->config = load_file('config/config', 'app');
$this->mm &= miniMVC::get_instance();
}
// --------------------------------------------------------------------------
@ -40,9 +41,9 @@ class Page extends JSObject
*/
private function _headers($xhtml, $html5)
{
$this->MM->output->set_header("Cache-Control", "must-revalidate, public");
$this->mm->output->set_header("Cache-Control", "must-revalidate, public");
$this->MM->output->set_header("Vary", "Accept");
$this->mm->output->set_header("Vary", "Accept");
$mime = "";
//Variable for accept keyword
@ -110,9 +111,9 @@ class Page extends JSObject
}
// finally, output the mime type and prolog type
$this->MM->output->set_header("Content-Type", "$mime;charset=$charset");
$this->MM->output->set_header("X-UA-Compatible", "chrome=1, IE=edge");
$this->MM->output->set_output($doctype_string);
$this->mm->output->set_header("Content-Type", "$mime;charset=$charset");
$this->mm->output->set_header("X-UA-Compatible", "chrome=1, IE=edge");
$this->mm->output->set_output($doctype_string);
return $this;
}
@ -148,7 +149,7 @@ class Page extends JSObject
return $this;
}
$file = $this->MM->config->item('group_js_path') . $group;
$file = $this->config['script_path'] . $group;
$file .= ($debug == TRUE) ? "/debug/1" : "";
$this->head_js .= $this->script_tag($file, FALSE);
return $this;
@ -178,11 +179,11 @@ class Page extends JSObject
public function set_css_group($group)
{
$link = array(
'href' => $this->MM->config->item('group_style_path') . $group,
'href' => $this->config['style_path'] . $group,
'rel' => 'stylesheet',
'type' => 'text/css'
);
$this->css .= link_tag($link);
$this->css .= $this->_link_tag($link);
return $this;
}
@ -196,7 +197,7 @@ class Page extends JSObject
*/
public function set_foot_js_group($group, $debug = FALSE)
{
$file = $this->MM->config->item('group_js_path') . $group;
$file = $this->config['script_path'] . $group;
$file .= ($debug == TRUE) ? "/debug/1" : "";
$this->foot_js .= $this->script_tag($file, FALSE);
return $this;
@ -224,7 +225,7 @@ class Page extends JSObject
*/
public function set_title($title = "")
{
$title = ($title == "") ? $this->MM->config->item('default_title') : $title;
$title = ($title == "") ? $this->config['default_title'] : $title;
$this->title = $title;
@ -280,13 +281,20 @@ class Page extends JSObject
*/
public function set_css_tag($name, $domain = TRUE, $media = "all")
{
$path = $this->MM->config->item('content_domain');
$path = CONTENT_DOMAIN;
$css_file = $path . "/css/" . $name . ".css";
if ($domain == FALSE)
{
$css_file = $name;
}
$this->css_tags .= link_tag($name, "stylesheet", "text/css", "", $media);
$this->css_tags .= $this->_link_tag(array(
'rel' => 'stylesheet',
'type' => 'text/css',
'media' => $media,
'href' => $css_file,
));
return $this;
}
@ -334,35 +342,30 @@ class Page extends JSObject
//Set Meta Tags
$this->meta = ($html5 == TRUE)
? '<meta charset="utf-8" />'. $this->meta
: meta('content-type', 'text/html; charset=utf-8', 'equiv') . $this->meta;
: $this->_meta(array(
'http-equiv' => 'Content-Type',
'content' => 'text/html; charset=utf-8',
)) . $this->meta;
$data['meta'] = $this->meta;
//Set CSS
if ($this->css != "")
if ($this->css !== "")
{
$data['css'] = $this->css;
}
else
{
//Set default CSS group
$this->set_css_group($this->MM->config->item('default_css_group'));
$this->set_css_group($this->config['default_css_group']);
$data['css'] = $this->css;
}
//Set head javascript
if ($this->head_js != "")
{
$data['head_js'] = $this->head_js;
}
else
{
$this->set_head_js_group($this->MM->config->item('default_head_js_group'));
$data['head_js'] = $this->head_js;
}
$data['head_js'] = ( ! empty($this->head_js)) ? $this->head_js : "";
//Set Page Title
$data['title'] = ($this->title != '') ? $this->title : $this->MM->config->item('default_title');
$data['title'] = ($this->title != '') ? $this->title : $this->config['default_title'];
//Set Body Class
$data['body_class'] = $this->body_class;
@ -380,7 +383,7 @@ class Page extends JSObject
$this->_headers($xhtml, $html5);
//Output Header
$this->MM->load->view('header', $data);
$this->mm->load_view('header', $data);
return $this;
}
@ -397,7 +400,7 @@ class Page extends JSObject
$data['foot_js'] = ($this->foot_js != "") ? $this->foot_js : '';
$this->MM->load->view('footer', $data);
$this->$this->mm->load_view('footer', $data);
}
// --------------------------------------------------------------------------
@ -413,7 +416,7 @@ class Page extends JSObject
*/
private function script_tag($js, $domain = TRUE)
{
$path = $this->MM->config->item('content_domain');
$path = CONTENT_DOMAIN;
$js_file = $path . "/js/" . $js . ".js";
if ($domain == FALSE)
@ -426,48 +429,6 @@ class Page extends JSObject
// --------------------------------------------------------------------------
/**
* Quick Build
*
* A function to make building pages faster
* @param mixed $view
* @param mixed $data
* @param bool $xhtml
* @param bool $html5
*/
public function quick_build($view, $data, $xhtml = TRUE, $html5 = TRUE)
{
//Set up header
if ($title != '')
{
$this->set_title($title);
}
else
{
$this->set_title($this->MM->config->item('default_title'));
}
$this->build_header($xhtml, $html5);
//Load view(s)
if (is_array($view))
{
foreach ($view as $v)
{
$this->MM->load->view($v, $data);
}
}
else
{
$this->MM->load->view($view, $data);
}
//Create footer
$this->build_footer();
}
// --------------------------------------------------------------------------
/**
* Set Message
*
@ -481,7 +442,7 @@ class Page extends JSObject
$data['stat_class'] = $type;
$data['message'] = $message;
$this->MM->load->view('message', $data);
$this->mm->load_view('message', $data);
}
// --------------------------------------------------------------------------
@ -494,8 +455,24 @@ class Page extends JSObject
*/
function redirect_303($url)
{
$this->MM->output->set_header("HTTP/1.1 303 See Other");
$this->MM->output->set_header("Location:" . $url);
$this->mm->output->set_header("HTTP/1.1 303 See Other");
$this->mm->output->set_header("Location:" . $url);
}
// --------------------------------------------------------------------------
/**
* Output
*
* Shortcut function for building a page
* @param string $view
* @param array $data
*/
function output($view, $data=array())
{
$this->build_header();
$this->mm->load_view($view, $data);
$this->build_footer();
}
// --------------------------------------------------------------------------
@ -513,4 +490,20 @@ class Page extends JSObject
return $string;
}
}
private function _link_tag($params)
{
$string = "<link ";
foreach($params as $k => $v)
{
$string .= $k . '="'.$v.'" ';
}
$string .= " />";
return $string;
}
}
// End of page.php