Okay, though it isn't by any means complete, pretty basic in fact for the moment 
Hoping to improve upon it soon though. Here though is what I have for the time being, enjoy 
PHP Code:
# ./root/web/modules/essentials.class.php
# essential application functionality
class BasePgView {
var $error;
var $template;
function BasePgView( $filename ) {
$this -> error = false;
if( !file_exists( $filename ) ) {
$this -> error = true;
}
$fp = @fopen( $filename, 'r' );
$template = @fread( $fp, filesize( $filename ) );
@fclose( $fp );
$this -> template = &$template;
}
function IsError() {
return $this -> error;
}
function Replace( $tag, $string ) {
$this -> template = ereg_replace( $tag, $string, $this -> template );
}
function Finalise() {
if( !$this -> IsError() ) {
#
$this -> Replace( '<page_date />', date( DATE_FULL ) );
echo( $this -> template );
exit;
}
}
}
class Module {
var $modules;
var $includes;
function Module() {
$this -> tags = array( '<page_news />' );
$this -> includes = array( 'news/news.class.php' );
$this -> Initialise();
}
function Fetch() {
return $this -> modules;
}
function Initialise() {
$count = 0;
$modules['tags'] = $this -> tags;
foreach( array( 'News' ) /* any given module(s) */ as $item ) {
include_once( $this -> includes[$count] );
# execute a module controller and pass over model data to module view
$cntrl = &new $item;
#
$v = $item.'View';
$v = &new $v( $cntrl -> Fetch() );
$modules[$count] = $v -> Render();
$count++;
}
$this -> modules = &$modules;
}
}
class BaseModuleView {
var $error;
var $template;
function BaseModuleView( $filename ) {
$this -> error = false;
if( !file_exists( $filename ) ) {
$this -> error = true;
}
$fp = @fopen( $filename, 'r' );
$template = @fread( $fp, filesize( $filename ) );
@fclose( $fp );
$this -> template = &$template;
}
function IsError() {
return $this -> error;
}
function Replace( $tag, $string ) {
$this -> template = ereg_replace( $tag, $string, $this -> template );
}
function Finalise() {
if( !$this -> IsError() ) {
#
return $this -> template;
}
}
}
PHP Code:
require_once( 'implementation.class.php' );
class News {
var $model;
function News() {
$this -> Execute();
}
function Fetch() {
return $this -> model;
}
function Execute() {
$db = &db::getInstance();
# implement model
$this -> model = FindByLimit::Load( Gateway::FindByLimit( &$db ) );
}
}
class NewsView {
var $model;
function NewsView( &$model ) {
$this -> model = &$model;
}
function Render() {
$html = '';
foreach( $this -> model as $model ) {
$content = &new BaseModuleView( 'modules/news/templates/news.fragment.html' );
$content -> Replace( '<id />', $model -> GetId() );
$content -> Replace( '<date />', /* date filter */ date( DATE_SHORT, $model -> GetDate() ) );
$content -> Replace( '<title />', $model -> GetTitle() );
#
$html .= $content -> Finalise();
}
return $html;
}
}
PHP Code:
# implementation models
class Gateway {
function Gateway() {
}
function FindById( &$db, $id ) {
$findById = "SELECT
id,
UNIX_TIMESTAMP(date) AS date,
title,
body FROM tbl_news WHERE id = '".$id."'";
$statement = $db -> Query( $findById );
#
return $statement -> Execute();
}
function FindByLimit( &$db, $limit = 5 ) {
$findByLimit = "SELECT
id,
UNIX_TIMESTAMP(date) AS date,
title,
body FROM tbl_news ORDER BY date DESC LIMIT ".$limit;
$statement = $db -> Query( $findByLimit );
#
return $statement -> Execute();
}
}
class Archiver {
var $ar;
function Archiver() {
$this -> ar = array();
}
function SetId( $id ) {
$this -> ar['id'] = $id;
}
function SetDate( $date ) {
$this -> ar['date'] = $date;
}
function SetTitle( $title ) {
$this -> ar['title'] = $title;
}
function SetBody( $body ) {
$this -> ar['body'] = $body;
}
}
class FindById {
function FindById() {
}
function Load( &$resultset ) {
$iterator = &new QueryIterator( $resultset );
$row = $iterator -> GetCurrent();
#
$archive = &new ByIdArchive;
$archive -> Invoke( $row );
return $archive;
}
}
class FindByLimit {
function FindByLimit() {
}
function Load( &$resultset ) {
$indexer = array();
$iterator = &new QueryIterator( $resultset );
#
while( $iterator -> IsValid() ) {
$row = $iterator -> GetCurrent();
$iterator -> GetNext();
#
$archive = &new ByLimitArchive;
$archive -> Invoke( $row );
#
$indexer[] = &$archive;
}
return $indexer;
}
}
class ByIdArchive extends Archiver {
function ByIdArchive() {
Archiver::Archiver();
}
function Invoke( $row ) {
$this -> SetId( $row['id'] );
$this -> SetDate( $row['date'] );
$this -> SetTitle( $row['title'] );
$this -> SetBody( $row['body'] );
}
function GetId() {
return $this -> ar['id'];
}
function GetDate() {
return $this -> ar['date'];
}
function GetTitle() {
return $this -> ar['title'];
}
function GetBody() {
return $this -> ar['body'];
}
}
class ByLimitArchive extends Archiver {
function ByLimitArchive() {
Archiver::Archiver();
}
function Invoke( $row ) {
$this -> SetId( $row['id'] );
$this -> SetDate( $row['date'] );
$this -> SetTitle( $row['title'] );
}
function GetId() {
return $this -> ar['id'];
}
function GetDate() {
return $this -> ar['date'];
}
function GetTitle() {
return $this -> ar['title'];
}
}
The HTML template fragment required by a module view, ie News
Code:
<div id="newsurl"><date /></div>
<div id="newsheading"><a href="news.php?task=findById&id=<id />"><title /></a></div>
And the part of the Page Controller which actually creates the instance of the Module class to build the seperate components.
PHP Code:
# ./root/web/index.php
# page controller @ home page
error_reporting( E_ALL );
# application configuration
require_once( 'configure.php' );
# core application libraries
require_once( LIBRARY_DB );
require_once( LIBRARY_GD );
require_once( LIBRARY_SESSION );
# application dependents
require_once( REQUIRED_ESSENTIALS );
class Page {
function Page() {
}
function Dispatch() {
$page = &new PgController;
$page -> Execute();
$view = &new PgView;
$view -> Render( new Module );
}
function Select() {
} // page controller requires no logic
}
class PgController {
function PgController() {
}
function Fetch() {
}
function Execute() {
}
}
class PgView extends BasePgView {
function PgView() {
BasePgView::BasePgView( TEMPLATE_DEFAULT );
}
function Render( $modules ) {
$count = 0;
$data = $modules -> Fetch();
foreach( $data['tags'] as $tag ) {
# for every given tag to replace
$this -> Replace( $tag, $data[$count] );
$count++;
}
$this -> Finalise();
}
}
# begin application
Session::Start();
Page::Dispatch(); // dispatch page
die();
Looking at an earlier post from last week, cannot remember exact one sorry, but it was centred on using pre and post processing yes ?
There was some nice idea's in the posted script which I plan to look at which might actually help improve on my own script ?
Obviously though, this isn't pre or post processing as such. If you've any idea's of your own, I'm open to suggestions as well
Bookmarks