Fixed object inheritence, changed config to be based more on constants

This commit is contained in:
Timothy Warren 2011-12-29 10:31:04 -05:00
parent c5634b182a
commit dda867c208
12 changed files with 300 additions and 219 deletions

View File

@ -1,61 +1,95 @@
<?php <?php
/*
|--------------------------------------------------------------------------
| Display Debug backtrace
|--------------------------------------------------------------------------
|
| If set to TRUE, a backtrace will be displayed along with php errors.
|
*/
define('SHOW_DEBUG_BACKTRACE', TRUE);
return array( /*
|--------------------------------------------------------------------------
| Base Url
|--------------------------------------------------------------------------
|
| This is the url path where the framework is located. Requires trailing
| slash.
|
*/
define('BASE_URL', $default_baseurl);
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Group Style Path | Content Domain
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| |
| This is the path that is used to determine the relative path to the | This is the domain used for serving content, such as css, javascript.
| stylesheet minifier. This should not need to be changed. |
| */
*/ define('CONTENT_DOMAIN', BASE_URL);
'style_path' => STATIC_LIB_PATH . '/css.php/g/',
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Group Javascript Path | Static Lib Path
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| |
| This is the path that is used to determine the relative path to the | This is the path where the 'assets' directory is on the static domain.
| javascript minifier. This should not need to be changed. |
| */
*/ define('STATIC_LIB_PATH', CONTENT_DOMAIN.'assets/');
'script_path' => STATIC_LIB_PATH . '/js.php/g/',
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Default title | Group Style Path
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| |
| Default title for webpages | This is the path that is used to determine the relative path to the
| | stylesheet minifier. This should not need to be changed.
*/ |
*/
define('STYLE_PATH', STATIC_LIB_PATH . 'css.php/g/');
'default_title' => "miniMVC app", /*
|--------------------------------------------------------------------------
| Group Javascript Path
|--------------------------------------------------------------------------
|
| This is the path that is used to determine the relative path to the
| javascript minifier. This should not need to be changed.
|
*/
define('SCRIPT_PATH', STATIC_LIB_PATH . 'js.php/g/');
/*
|--------------------------------------------------------------------------
| Default css group
|--------------------------------------------------------------------------
|
| Default css group
|
*/
'default_css_group' => "css", /*
|--------------------------------------------------------------------------
| Default title
|--------------------------------------------------------------------------
|
| Default title for webpages
|
*/
define('DEFAULT_TITLE', "miniMVC app");
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Default js group | Default css group
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| |
| Default js group | Default css group
| |
*/ */
define('DEFAULT_CSS_GROUP', "css");
'default_js_group' => "js", /*
|--------------------------------------------------------------------------
); | Default js group
|--------------------------------------------------------------------------
|
| Default js group
|
*/
define('DEFAULT_JS_GROUP', "js");

View File

@ -1,4 +1,4 @@
<div class="error"> <div class="message error">
<h4>A PHP Error was encountered</h4> <h4>A PHP Error was encountered</h4>
<p>Severity: <?php echo $severity; ?></p> <p>Severity: <?php echo $severity; ?></p>

View File

@ -1,5 +1,5 @@
<section class="message <?= $stat_class ?>"> <div class="message <?= $stat_class ?>">
<span class="icon"></span> <span class="icon"></span>
<?= $message ?> <?= $message ?>
<span class="close"></span> <span class="close" onclick="this.parentElement.style.display='none'"></span>
</section> </div>

View File

@ -13,7 +13,7 @@
| you will need to add that folder to the document root. | you will need to add that folder to the document root.
| |
*/ */
$document_root = $_SERVER['DOCUMENT_ROOT']; $document_root = realpath("../".__DIR__);
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
@ -23,7 +23,7 @@ $document_root = $_SERVER['DOCUMENT_ROOT'];
| The folder where css files exist, in relation to the document root | The folder where css files exist, in relation to the document root
| |
*/ */
$css_root = $document_root. '/css/'; $css_root = $document_root. 'css/';
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------

View File

@ -4,6 +4,10 @@ return array(
Css Css
-----*/ -----*/
'css' => array(
'message.css'
),
/* /*
For each group create an array like so For each group create an array like so

36
assets/css/message.css Normal file
View File

@ -0,0 +1,36 @@
.message{
position:relative;
margin:0.5em auto;
padding:0.5em;
width:95%;
}
.message .close{
width:1em;
height:1em;
border:1px solid #000;
position:absolute;
right:0.5em;
top:0.5em;
}
.message .icon{
left:0.5em;
top:0.5em;
margin-right:1em;
}
.error{
border:1px solid #924949;
background: #f3e6e6;
}
.success{
border:1px solid #1f8454;
background: #70dda9;
}
.info{
border:1px solid #bfbe3a;
background: #FFFFCC;
}

View File

@ -3,53 +3,16 @@
// Change this in a live environment! // Change this in a live environment!
error_reporting(-1); error_reporting(-1);
/*
|--------------------------------------------------------------------------
| Display Debug backtrace
|--------------------------------------------------------------------------
|
| If set to TRUE, a backtrace will be displayed along with php errors.
|
*/
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 // Set the default paths
define('SYS_PATH', __DIR__.'/sys/'); define('SYS_PATH', __DIR__.'/sys/');
define('MOD_PATH', __DIR__.'/modules/'); define('MOD_PATH', __DIR__.'/modules/');
define('APP_PATH', __DIR__.'/app/'); define('APP_PATH', __DIR__.'/app/');
$default_baseurl = "//".$_SERVER['HTTP_HOST']. str_replace("index.php", "", $_SERVER['REQUEST_URI']);
// Require the basic configuratio file
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");

View File

@ -1,17 +1,20 @@
<?php <?php
class Welcome extends miniMVC { class Welcome extends MM_Controller {
function __construct() function __construct()
{ {
parent::__construct(); parent::__construct();
} }
function index() function index()
{ {
//$this->page->build_header(); $this->page->build_header();
$this->output->append_output($this->__toString());
//$this->page->build_footer(); $this->page->set_message('info', "This is just a test message");
//$this->output->append_output($this->__toString());
$this->page->build_footer();
} }
} }

View File

@ -4,16 +4,6 @@
* File including common framework-wide functions * File including common framework-wide functions
*/ */
/**
* Singleton function
*/
function mm()
{
return miniMVC::singleton();
}
// --------------------------------------------------------------------------
/** /**
* Function to search through the tree to find the necessary file * Function to search through the tree to find the necessary file
* *
@ -56,7 +46,38 @@ function load_file($file, $curr_path="")
//} //}
} }
include_once($path); if(is_file($path))
{
require_once($path);
}
}
function load_class($name, &$self=array())
{
if(is_array($self))
{
$self =& miniMVC::get_instance();
}
$path = SYS_PATH."{$name}.php";
$class = "{$name}";
if(class_exists($class, FALSE))
{
if( ! isset($self->$name))
{
$self->$name = new $class;
return;
}
}
load_file($name, 'sys');
if(class_exists($class, FALSE))
{
$self->$name = new $class;
}
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -78,7 +99,7 @@ function on_error($severity, $message, $filepath, $line, $context)
E_USER_ERROR => 'User Error', E_USER_ERROR => 'User Error',
E_USER_WARNING => 'User Warning', E_USER_WARNING => 'User Warning',
E_USER_NOTICE => 'User Notice', E_USER_NOTICE => 'User Notice',
E_STRICT => 'Runtime Notice' E_STRICT => 'Strict Error'
); );
$severity = (isset($levels[$severity])) ? $levels[$severity] : $severity; $severity = (isset($levels[$severity])) ? $levels[$severity] : $severity;
@ -166,7 +187,7 @@ function route()
//Set error handlers //Set error handlers
set_error_handler('on_error'); set_error_handler('on_error');
//set_exception_handler('on_exception'); set_exception_handler('on_exception');
// Load Most Common libraries // Load Most Common libraries
require_once('miniMVC.php'); require_once('miniMVC.php');

View File

@ -5,34 +5,35 @@
class miniMVC{ class miniMVC{
private static $instance; private static $instance;
private static $count;
private $buffer, $headers; //var $output = new Output;
//var $page = new Page;
public static function get_instance() public static function get_instance()
{ {
if ( ! isset(self::$instance)) { if( ! isset(self::$count))
echo 'Creating new instance.'; {
$className = __CLASS__; self::$count = 0;
self::$instance = new $className;
} }
return self::$instance; if ( ! isset(self::$instance))
{
self::$count++;
self::$instance = new miniMVC;
} }
function __construct() $self =& self::$instance;
{
// Load the page class return $self;
/*if( ! isset(self::$instance->page))
{
self::$instance->page &= new Page;
} }
if( ! isset($this->output)) /**
* Constructor - Any classes loaded here become subclasses of miniMVC
*/
public function __construct()
{ {
self::$instance->output &= new Output; self::$instance =& $this;
}*/
} }
/** /**
@ -43,58 +44,6 @@ class miniMVC{
trigger_error('Clone is not allowed.', E_USER_ERROR); 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 * PHP magic method to facilitate dynamic methods
* *
@ -169,27 +118,95 @@ class miniMVC{
*/ */
function __get($name) function __get($name)
{ {
//$path = SYS_PATH."{$name}.php"; $path = SYS_PATH."{$name}.php";
$class = "{$name}"; $class = "{$name}";
if(class_exists($class, FALSE)) if(class_exists($class, FALSE))
{ {
if( ! isset(self::$instance->$name)) if( ! isset($this->$name))
{ {
self::$instance->$name &= new $class; $this->$name = new $class;
return; return;
} }
} }
/*load_file($name, 'sys'); load_file($name, 'sys');
if(class_exists($class, FALSE)) if(class_exists($class, FALSE))
{ {
self::$instance->$name &= new $class; $this->$name = new $class;
}*/ }
} }
/**
* PHP magic method that is called when an object is treated as a function
*/
public static function __invoke()
{
return self::get_instance();
}
} }
class MM_Controller extends miniMVC {
function __construct()
{
parent::__construct();
$this->output = new Output;
$this->page = new Page;
}
/**
* 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', 'output');
// 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);
}
}
// End of miniMVC.php // End of miniMVC.php

View File

@ -2,6 +2,8 @@
class Output extends miniMVC { class Output extends miniMVC {
private $buffer, $headers;
function __construct() function __construct()
{ {
$this->buffer = ""; $this->buffer = "";
@ -33,7 +35,9 @@ class Output extends miniMVC {
if( ! empty($this->buffer)) if( ! empty($this->buffer))
{ {
ob_start("ob_gzhandler");
echo $this->buffer; echo $this->buffer;
ob_end_flush();
} }
} }

View File

@ -21,9 +21,8 @@ class Page
$this->body_class = ""; $this->body_class = "";
$this->body_id = ""; $this->body_id = "";
$this->base = ""; $this->base = "";
$this->config = load_file('config/config', 'app');
$this->mm &= miniMVC::get_instance(); $this->mm = miniMVC::get_instance();
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -47,7 +46,7 @@ class Page
$mime = ""; $mime = "";
//Variable for accept keyword //Variable for accept keyword
$accept = (!empty($_SERVER['HTTP_ACCEPT'])) ? $_SERVER['HTTP_ACCEPT'] : ""; $accept = ( ! empty($_SERVER['HTTP_ACCEPT'])) ? $_SERVER['HTTP_ACCEPT'] : "";
//Predefine doctype //Predefine doctype
$doctype_string = ($html5 == TRUE) ? '<!DOCTYPE html>' : '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">'; $doctype_string = ($html5 == TRUE) ? '<!DOCTYPE html>' : '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">';
@ -107,11 +106,11 @@ class Page
} }
else else
{ {
$doctype_string .= "\n<html lang='en'>"; $doctype_string .= "<html lang='en'>";
} }
// finally, output the mime type and prolog type // finally, output the mime type and prolog type
$this->mm->output->set_header("Content-Type", "$mime;charset=$charset"); $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_header("X-UA-Compatible", "chrome=1, IE=edge");
$this->mm->output->set_output($doctype_string); $this->mm->output->set_output($doctype_string);
@ -149,7 +148,7 @@ class Page
return $this; return $this;
} }
$file = $this->config['script_path'] . $group; $file = SCRIPT_PATH . $group;
$file .= ($debug == TRUE) ? "/debug/1" : ""; $file .= ($debug == TRUE) ? "/debug/1" : "";
$this->head_js .= $this->script_tag($file, FALSE); $this->head_js .= $this->script_tag($file, FALSE);
return $this; return $this;
@ -179,7 +178,7 @@ class Page
public function set_css_group($group) public function set_css_group($group)
{ {
$link = array( $link = array(
'href' => $this->config['style_path'] . $group, 'href' => STYLE_PATH . $group,
'rel' => 'stylesheet', 'rel' => 'stylesheet',
'type' => 'text/css' 'type' => 'text/css'
); );
@ -197,7 +196,7 @@ class Page
*/ */
public function set_foot_js_group($group, $debug = FALSE) public function set_foot_js_group($group, $debug = FALSE)
{ {
$file = $this->config['script_path'] . $group; $file = SCRIPT_PATH . $group;
$file .= ($debug == TRUE) ? "/debug/1" : ""; $file .= ($debug == TRUE) ? "/debug/1" : "";
$this->foot_js .= $this->script_tag($file, FALSE); $this->foot_js .= $this->script_tag($file, FALSE);
return $this; return $this;
@ -225,7 +224,7 @@ class Page
*/ */
public function set_title($title = "") public function set_title($title = "")
{ {
$title = ($title == "") ? $this->config['default_title'] : $title; $title = ($title == "") ? DEFAULT_TITLE : $title;
$this->title = $title; $this->title = $title;
@ -333,7 +332,7 @@ class Page
* @param mixed $xhtml * @param mixed $xhtml
* @param bool $html5 * @param bool $html5
* @param bool $fbml * @param bool $fbml
* @return $this * @return Page
*/ */
public function build_header($xhtml = FALSE, $html5 = TRUE) public function build_header($xhtml = FALSE, $html5 = TRUE)
{ {
@ -357,7 +356,7 @@ class Page
else else
{ {
//Set default CSS group //Set default CSS group
$this->set_css_group($this->config['default_css_group']); $this->set_css_group(DEFAULT_CSS_GROUP);
$data['css'] = $this->css; $data['css'] = $this->css;
} }
@ -365,7 +364,7 @@ class Page
$data['head_js'] = ( ! empty($this->head_js)) ? $this->head_js : ""; $data['head_js'] = ( ! empty($this->head_js)) ? $this->head_js : "";
//Set Page Title //Set Page Title
$data['title'] = ($this->title != '') ? $this->title : $this->config['default_title']; $data['title'] = ($this->title !== '') ? $this->title : DEFAULT_TITLE;
//Set Body Class //Set Body Class
$data['body_class'] = $this->body_class; $data['body_class'] = $this->body_class;
@ -400,7 +399,7 @@ class Page
$data['foot_js'] = ($this->foot_js != "") ? $this->foot_js : ''; $data['foot_js'] = ($this->foot_js != "") ? $this->foot_js : '';
$this->$this->mm->load_view('footer', $data); $this->mm->load_view('footer', $data);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -462,16 +461,16 @@ class Page
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Output * Render
* *
* Shortcut function for building a page * Shortcut function for building a page
* @param string $view * @param string $view
* @param array $data * @param array $data
*/ */
function output($view, $data=array()) function render($view, $data=array())
{ {
$this->build_header(); $this->build_header();
$this->mm->load_view($view, $data); $this->load_view($view, $data);
$this->build_footer(); $this->build_footer();
} }