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,6 +1,45 @@
<?php
/*
|--------------------------------------------------------------------------
| 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', $default_baseurl);
/*
|--------------------------------------------------------------------------
| Content Domain
|--------------------------------------------------------------------------
|
| This is the domain used for serving content, such as css, javascript.
|
*/
define('CONTENT_DOMAIN', BASE_URL);
/*
|--------------------------------------------------------------------------
| Static Lib Path
|--------------------------------------------------------------------------
|
| This is the path where the 'assets' directory is on the static domain.
|
*/
define('STATIC_LIB_PATH', CONTENT_DOMAIN.'assets/');
return array(
/*
|--------------------------------------------------------------------------
@ -11,7 +50,7 @@ return array(
| stylesheet minifier. This should not need to be changed.
|
*/
'style_path' => STATIC_LIB_PATH . '/css.php/g/',
define('STYLE_PATH', STATIC_LIB_PATH . 'css.php/g/');
/*
|--------------------------------------------------------------------------
@ -22,7 +61,7 @@ return array(
| javascript minifier. This should not need to be changed.
|
*/
'script_path' => STATIC_LIB_PATH . '/js.php/g/',
define('SCRIPT_PATH', STATIC_LIB_PATH . 'js.php/g/');
/*
@ -33,8 +72,7 @@ return array(
| Default title for webpages
|
*/
'default_title' => "miniMVC app",
define('DEFAULT_TITLE', "miniMVC app");
/*
|--------------------------------------------------------------------------
@ -44,8 +82,7 @@ return array(
| Default css group
|
*/
'default_css_group' => "css",
define('DEFAULT_CSS_GROUP', "css");
/*
|--------------------------------------------------------------------------
@ -55,7 +92,4 @@ return array(
| Default js group
|
*/
'default_js_group' => "js",
);
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>
<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>
<?= $message ?>
<span class="close"></span>
</section>
<span class="close" onclick="this.parentElement.style.display='none'"></span>
</div>

View File

@ -13,7 +13,7 @@
| 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
|
*/
$css_root = $document_root. '/css/';
$css_root = $document_root. 'css/';
/*
|--------------------------------------------------------------------------

View File

@ -4,6 +4,10 @@ return array(
Css
-----*/
'css' => array(
'message.css'
),
/*
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!
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
define('SYS_PATH', __DIR__.'/sys/');
define('MOD_PATH', __DIR__.'/modules/');
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(SYS_PATH . "common.php");

View File

@ -1,17 +1,20 @@
<?php
class Welcome extends miniMVC {
class Welcome extends MM_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
//$this->page->build_header();
$this->output->append_output($this->__toString());
//$this->page->build_footer();
$this->page->build_header();
$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
*/
/**
* Singleton function
*/
function mm()
{
return miniMVC::singleton();
}
// --------------------------------------------------------------------------
/**
* 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_WARNING => 'User Warning',
E_USER_NOTICE => 'User Notice',
E_STRICT => 'Runtime Notice'
E_STRICT => 'Strict Error'
);
$severity = (isset($levels[$severity])) ? $levels[$severity] : $severity;
@ -166,7 +187,7 @@ function route()
//Set error handlers
set_error_handler('on_error');
//set_exception_handler('on_exception');
set_exception_handler('on_exception');
// Load Most Common libraries
require_once('miniMVC.php');

View File

@ -5,34 +5,35 @@
class miniMVC{
private static $instance;
private static $count;
private $buffer, $headers;
//var $output = new Output;
//var $page = new Page;
public static function get_instance()
{
if ( ! isset(self::$instance)) {
echo 'Creating new instance.';
$className = __CLASS__;
self::$instance = new $className;
if( ! isset(self::$count))
{
self::$count = 0;
}
return self::$instance;
if ( ! isset(self::$instance))
{
self::$count++;
self::$instance = new miniMVC;
}
function __construct()
{
// Load the page class
/*if( ! isset(self::$instance->page))
{
self::$instance->page &= new Page;
$self =& self::$instance;
return $self;
}
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);
}
/**
* 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
*
@ -169,27 +118,95 @@ class miniMVC{
*/
function __get($name)
{
//$path = SYS_PATH."{$name}.php";
$path = SYS_PATH."{$name}.php";
$class = "{$name}";
if(class_exists($class, FALSE))
{
if( ! isset(self::$instance->$name))
if( ! isset($this->$name))
{
self::$instance->$name &= new $class;
$this->$name = new $class;
return;
}
}
/*load_file($name, 'sys');
load_file($name, 'sys');
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

View File

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

View File

@ -21,9 +21,8 @@ class Page
$this->body_class = "";
$this->body_id = "";
$this->base = "";
$this->config = load_file('config/config', 'app');
$this->mm &= miniMVC::get_instance();
$this->mm = miniMVC::get_instance();
}
// --------------------------------------------------------------------------
@ -107,11 +106,11 @@ class Page
}
else
{
$doctype_string .= "\n<html lang='en'>";
$doctype_string .= "<html lang='en'>";
}
// 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_output($doctype_string);
@ -149,7 +148,7 @@ class Page
return $this;
}
$file = $this->config['script_path'] . $group;
$file = SCRIPT_PATH . $group;
$file .= ($debug == TRUE) ? "/debug/1" : "";
$this->head_js .= $this->script_tag($file, FALSE);
return $this;
@ -179,7 +178,7 @@ class Page
public function set_css_group($group)
{
$link = array(
'href' => $this->config['style_path'] . $group,
'href' => STYLE_PATH . $group,
'rel' => 'stylesheet',
'type' => 'text/css'
);
@ -197,7 +196,7 @@ class Page
*/
public function set_foot_js_group($group, $debug = FALSE)
{
$file = $this->config['script_path'] . $group;
$file = SCRIPT_PATH . $group;
$file .= ($debug == TRUE) ? "/debug/1" : "";
$this->foot_js .= $this->script_tag($file, FALSE);
return $this;
@ -225,7 +224,7 @@ class Page
*/
public function set_title($title = "")
{
$title = ($title == "") ? $this->config['default_title'] : $title;
$title = ($title == "") ? DEFAULT_TITLE : $title;
$this->title = $title;
@ -333,7 +332,7 @@ class Page
* @param mixed $xhtml
* @param bool $html5
* @param bool $fbml
* @return $this
* @return Page
*/
public function build_header($xhtml = FALSE, $html5 = TRUE)
{
@ -357,7 +356,7 @@ class Page
else
{
//Set default CSS group
$this->set_css_group($this->config['default_css_group']);
$this->set_css_group(DEFAULT_CSS_GROUP);
$data['css'] = $this->css;
}
@ -365,7 +364,7 @@ class Page
$data['head_js'] = ( ! empty($this->head_js)) ? $this->head_js : "";
//Set Page Title
$data['title'] = ($this->title != '') ? $this->title : $this->config['default_title'];
$data['title'] = ($this->title !== '') ? $this->title : DEFAULT_TITLE;
//Set Body Class
$data['body_class'] = $this->body_class;
@ -400,7 +399,7 @@ class Page
$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
* @param string $view
* @param array $data
*/
function output($view, $data=array())
function render($view, $data=array())
{
$this->build_header();
$this->mm->load_view($view, $data);
$this->load_view($view, $data);
$this->build_footer();
}