From dda867c2081c2d41db76ce778a6049144a8b2168 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 29 Dec 2011 10:31:04 -0500 Subject: [PATCH] Fixed object inheritence, changed config to be based more on constants --- app/config/config.php | 146 +++++++++++-------- app/errors/error.php | 2 +- app/views/message.php | 6 +- assets/config/config.php | 4 +- assets/config/css_groups.php | 4 + assets/css/message.css | 36 +++++ index.php | 47 +----- modules/welcome/controllers/welcome.php | 11 +- sys/common.php | 47 ++++-- sys/miniMVC.php | 181 +++++++++++++----------- sys/output.php | 4 + sys/page.php | 31 ++-- 12 files changed, 300 insertions(+), 219 deletions(-) create mode 100644 assets/css/message.css diff --git a/app/config/config.php b/app/config/config.php index 8d2caab..e01f400 100644 --- a/app/config/config.php +++ b/app/config/config.php @@ -1,61 +1,95 @@ STATIC_LIB_PATH . '/css.php/g/', - - /* - |-------------------------------------------------------------------------- - | 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. - | - */ - 'script_path' => STATIC_LIB_PATH . '/js.php/g/', - - - /* - |-------------------------------------------------------------------------- - | Default title - |-------------------------------------------------------------------------- - | - | Default title for webpages - | - */ - - 'default_title' => "miniMVC app", - - /* - |-------------------------------------------------------------------------- - | Default css group - |-------------------------------------------------------------------------- - | - | Default css group - | - */ +/* +|-------------------------------------------------------------------------- +| Base Url +|-------------------------------------------------------------------------- +| +| This is the url path where the framework is located. Requires trailing +| slash. +| +*/ +define('BASE_URL', $default_baseurl); - 'default_css_group' => "css", - - /* - |-------------------------------------------------------------------------- - | Default js group - |-------------------------------------------------------------------------- - | - | Default js group - | - */ +/* +|-------------------------------------------------------------------------- +| Content Domain +|-------------------------------------------------------------------------- +| +| This is the domain used for serving content, such as css, javascript. +| +*/ +define('CONTENT_DOMAIN', BASE_URL); - 'default_js_group' => "js", +/* +|-------------------------------------------------------------------------- +| Static Lib Path +|-------------------------------------------------------------------------- +| +| This is the path where the 'assets' directory is on the static domain. +| +*/ +define('STATIC_LIB_PATH', CONTENT_DOMAIN.'assets/'); -); \ No newline at end of file + +/* +|-------------------------------------------------------------------------- +| Group Style Path +|-------------------------------------------------------------------------- +| +| 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/'); + +/* +|-------------------------------------------------------------------------- +| 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 title +|-------------------------------------------------------------------------- +| +| Default title for webpages +| +*/ +define('DEFAULT_TITLE', "miniMVC app"); + +/* +|-------------------------------------------------------------------------- +| Default css group +|-------------------------------------------------------------------------- +| +| Default css group +| +*/ +define('DEFAULT_CSS_GROUP', "css"); + +/* +|-------------------------------------------------------------------------- +| Default js group +|-------------------------------------------------------------------------- +| +| Default js group +| +*/ +define('DEFAULT_JS_GROUP', "js"); \ No newline at end of file diff --git a/app/errors/error.php b/app/errors/error.php index 7d87fac..9f31b3f 100644 --- a/app/errors/error.php +++ b/app/errors/error.php @@ -1,4 +1,4 @@ -
+

A PHP Error was encountered

Severity:

diff --git a/app/views/message.php b/app/views/message.php index fba8c99..8f1fbd0 100644 --- a/app/views/message.php +++ b/app/views/message.php @@ -1,5 +1,5 @@ -
+
- -
\ No newline at end of file + +
\ No newline at end of file diff --git a/assets/config/config.php b/assets/config/config.php index fe5b22d..2953829 100755 --- a/assets/config/config.php +++ b/assets/config/config.php @@ -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/'; /* |-------------------------------------------------------------------------- diff --git a/assets/config/css_groups.php b/assets/config/css_groups.php index ff104bb..9f1c1d1 100755 --- a/assets/config/css_groups.php +++ b/assets/config/css_groups.php @@ -4,6 +4,10 @@ return array( Css -----*/ + 'css' => array( + 'message.css' + ), + /* For each group create an array like so diff --git a/assets/css/message.css b/assets/css/message.css new file mode 100644 index 0000000..9eb6977 --- /dev/null +++ b/assets/css/message.css @@ -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; +} \ No newline at end of file diff --git a/index.php b/index.php index 5c4c728..0adeb1c 100644 --- a/index.php +++ b/index.php @@ -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"); diff --git a/modules/welcome/controllers/welcome.php b/modules/welcome/controllers/welcome.php index 0856102..efd997b 100644 --- a/modules/welcome/controllers/welcome.php +++ b/modules/welcome/controllers/welcome.php @@ -1,17 +1,20 @@ 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(); } } \ No newline at end of file diff --git a/sys/common.php b/sys/common.php index 08c510a..18f7b99 100644 --- a/sys/common.php +++ b/sys/common.php @@ -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'); diff --git a/sys/miniMVC.php b/sys/miniMVC.php index d270a47..cf43d6e 100644 --- a/sys/miniMVC.php +++ b/sys/miniMVC.php @@ -5,35 +5,36 @@ 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; + } + + if ( ! isset(self::$instance)) + { + self::$count++; + self::$instance = new miniMVC; } - return self::$instance; + $self =& self::$instance; + + return $self; + } + + /** + * Constructor - Any classes loaded here become subclasses of miniMVC + */ + public function __construct() + { + self::$instance =& $this; } - - function __construct() - { - // Load the page class - /*if( ! isset(self::$instance->page)) - { - self::$instance->page &= new Page; - } - - if( ! isset($this->output)) - { - self::$instance->output &= new Output; - }*/ - } /** * Magic function called when cloning an object @@ -42,58 +43,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 @@ -168,28 +117,96 @@ class miniMVC{ * @param string $name */ 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 { -// End of miniMVC.php + 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 \ No newline at end of file diff --git a/sys/output.php b/sys/output.php index cf741aa..418af70 100644 --- a/sys/output.php +++ b/sys/output.php @@ -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(); } } diff --git a/sys/page.php b/sys/page.php index 2051344..5f35f7e 100644 --- a/sys/page.php +++ b/sys/page.php @@ -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(); } // -------------------------------------------------------------------------- @@ -47,7 +46,7 @@ class Page $mime = ""; //Variable for accept keyword - $accept = (!empty($_SERVER['HTTP_ACCEPT'])) ? $_SERVER['HTTP_ACCEPT'] : ""; + $accept = ( ! empty($_SERVER['HTTP_ACCEPT'])) ? $_SERVER['HTTP_ACCEPT'] : ""; //Predefine doctype $doctype_string = ($html5 == TRUE) ? '' : ''; @@ -107,11 +106,11 @@ class Page } else { - $doctype_string .= "\n"; + $doctype_string .= ""; } // 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(); }