Skip to content

Commit

Permalink
change layout so upgrades to system components are easy
Browse files Browse the repository at this point in the history
  • Loading branch information
allynbauer committed Apr 15, 2010
1 parent 2be0bce commit 8443ec0
Show file tree
Hide file tree
Showing 21 changed files with 1,282 additions and 7 deletions.
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2010 Allyn Bauer

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
2 changes: 0 additions & 2 deletions README
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,4 @@ purpose of each folder
how to upgrade

To Do:
make paths that match a non-existant controller error
add error page handling
refactor lib to be only lib y
Empty file modified controllers/app_controller.php
100644 → 100755
Empty file.
Empty file modified controllers/example_controller.php
100644 → 100755
Empty file.
12 changes: 9 additions & 3 deletions index.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@
=========================================
*/

define('WEB_ROOT', '/condor'); // NO TRAILING SLASH

require_once('lib/init.php');
define('WEB_ROOT', str_replace($_SERVER['DOCUMENT_ROOT'], '', dirname(__FILE__)));
require_once('system/init.php');

// process the request URL
$url = $_SERVER['REQUEST_URI'];
$manager = new PathManager($url);
$route = $manager->build_route();
$instance = $manager->controller_instance($route->controller);

if ($instance === FALSE) {
die("Fatal Error: Cannot find a controller called '{$route->controller}'.");
} elseif (method_exists($instance, $route->action) === FALSE) {
die("Fatal Error: Controller '{$route->controller}' does not respond to action '{$route->action}'.");
}

$action = $route->action;
$instance->$action($route->params);
render($route);
Expand Down
Empty file modified lib/database.php
100644 → 100755
Empty file.
2 changes: 2 additions & 0 deletions lib/helpers.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
<?php

?>
Empty file modified lib/user_functions.php
100644 → 100755
Empty file.
11 changes: 9 additions & 2 deletions lib/userdata_app.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
<?php
require_once("userdata.php");

// We extend the UserData class to seperate local changes
// from the global validator
class App_UserData extends UserData {
/* define your custom validators here */

/*
function deliverPackage() {
$data = new OpenStruct();
$data->winsauce = $this->getWinSauce();
return $data;
// etcs
}
*/
}
?>
Empty file modified resources/scripts/app.js
100644 → 100755
Empty file.
Empty file modified resources/styles/app.css
100644 → 100755
Empty file.
49 changes: 49 additions & 0 deletions system/flash.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

class FlashHelper {
function __construct() {
// flashes need a default array to keep track of existing flashes
// that way we don't have to know what they are called, we just pull them out of here
if (!isset($_SESSION['flash'])) {
$_SESSION['flash'] = array();
}
}

// return the currently registered flashes
function get_list() {
return array_keys($_SESSION['flash']);
}

// true if name exists, false if not
function exists($name) {
return in_array($name, $this->get_list());
}

// render as HTML
function render() {
$contents = array();
$count = 0;
foreach($this->get_list() as $name) {
$count++;
$contents[] = "<div id='flash_$count' class='flash $name'><span>{$this->$name}<span></div>";
}
return implode("\n", $contents);
}

function __set($name, $value) {
$_SESSION['flash'][$name] = $value;
}

function __get($name) {
if ($this->exists($name)) {
$contents = $_SESSION['flash'][$name];
unset($_SESSION['flash'][$name]);

return $contents;
} else {
return FALSE;
}
}
}

?>
38 changes: 38 additions & 0 deletions system/init.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

//Turn magic quotes off!
ini_set('magic_quotes_gpc', 'off');
ini_set('magic_quotes_runtime', 'off');

define('SYSTEM_ROOT', dirname(dirname(__FILE__)));
$__system__ = constant('SYSTEM_ROOT') . '/system';
$__lib__ = constant('SYSTEM_ROOT') . '/lib';

require_once("$__system__/userdata.php");
require_once("$__lib__/userdata_app.php");
$getData = new App_UserData('GET');
$postData = new App_UserData('POST');

// flash! longer then a blink and shorter then a moment
require_once("$__system__/flash.php");
session_start();
$flash = new FlashHelper();

// SYSTEM FILES
require_once("$__system__/openstruct.php");
require_once("$__system__/sys.php");
require_once("$__system__/path_manager.php");
require_once("$__system__/system_functions.php");

// LIB FILES
require_once("$__lib__/database.php");
require_once("$__lib__/user_functions.php");
require_once("$__lib__/helpers.php");

// SYSTEM USE VARIABLES
$content = ''; // content to render
$data = new OpenStruct(); // data mapped to rendering view
$_error_count = 0; // count of fatal app errors
template('template'); // set the default template

?>
21 changes: 21 additions & 0 deletions system/openstruct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

class OpenStruct {
private $_data = array();

function __set($name, $value) {
$this->_data[$name] = $value;
}

function __get($name) {
$obj = $this->_data[$name];
if (!$obj) $obj = FALSE;
return $obj;
}

function as_ary() {
return $this->_data;
}
}

?>
112 changes: 112 additions & 0 deletions system/path_manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

class PathManager {
function __construct($url) {
$this->url = $this->remove_base_url($url);
}

function build_route() {
$url = $this->url; // this method is non-destructive of object state
$controller = array_shift($url);
$klass = $this->controller_instance($controller);
$route = NULL;

// attempt to build a path
if (isset($klass->routes)) {
$routes = $klass->routes;
foreach($routes as $method => $sequence) {
$params = $this->match($sequence);
if ($params) {
$route = new OpenStruct();
$route->controller = $controller;
$route->action = $method;
$route->params = $params;
}
}
}

if (empty($route)) {
$route = $this->build_generic_route();
}

return $route;
}

function remove_base_url($url) {
$url = explode('/', $url);
$base = explode('/', constant('WEB_ROOT'));
foreach($base as $bit) {
if ($url[0] == $bit) {
array_shift($url);
} else {
/* error */
}
}
return $url;
}

// |route| is the route we're testing matches for
function match($route) {
$base = explode('/', CONSTANT('WEB_ROOT'));
$parts = $this->remove_base_url($route);
$url = $this->url;

// this method is supposed to route a controller, so we already know what it is
array_shift($url);

$params = array();

if (count($url) === 1 && $url[0] === '') { // route matches '$controller/' (index action)
return $params;
}

// handle this match
foreach($parts as $piece) {
$url_part = array_shift($url);
if (strpos($piece, ':') === 0) { // style is ':token'
$params[preg_replace('/:/', '', $piece)] = $url_part;
} elseif($url_part != $piece) {
return FALSE;
}
}
if (!empty($params)) return $params;
return FALSE;
}

// build and return the path parameters
function build_generic_route() {
$parts = $this->url;
$route = new OpenStruct();

$controller = array_shift($parts);
if ($controller == '') $controller = 'app';

$action = array_shift($parts);
if ($action == '') $action = 'index';

$route->controller = $controller;
$route->action = $action;
$route->params = $parts;

return $route;
}

// return an instance of the |name| controller, or FALSE if there wasn't one
function controller_instance($name) {
$controller_path = $this->controller_path($name);
$klass = null;
if (file_exists($controller_path)) {
require_once($controller_path);
$klass = ucfirst($name) . 'Controller';
$klass = new $klass();
} else {
return FALSE;
}
return $klass;
}

function controller_path($name) {
return CONSTANT('SYSTEM_ROOT') . "/controllers/$name" . "_controller.php";
}

}
23 changes: 23 additions & 0 deletions system/sys.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/*
Sys is a generic wrapper for global variables. It lets us access global variables in
daughter scopes without having to use $_GLOBALS or 'global'.
t is used like this:
sys()->|global|->|action|
Where |global| is the global identifier.
*/

class Sys {
function __get($name) {
global $$name;
return $$name;
}
}

function sys() {
global $sys;
return $sys;
}

$sys = new Sys();
31 changes: 31 additions & 0 deletions system/system_functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/* THIS FILE IS FOR FUNCTIONS THAT THE SYSTEM USES AT A CORE LEVEL.
*/

// return the contents of a view associated with |name|
function get_view($name) {
$path = CONSTANT('SYSTEM_ROOT') . "/views/$name.php";
if (!file_exists($path)) return FALSE;
return file_get_contents($path);
}

// render the template with the current |view|
function render($path) {
$view = $path->controller . '/' . $path->action;
sys()->_view = $view;
$template = get_view($view);
if ($template !== false) sys()->content = $template; // if there's a view file, save it
sys()->data->web_root = constant('WEB_ROOT');
ob_start();

// apply the info in $data in this scope so they can be accsesed in the views
foreach(sys()->data->as_ary() as $key => $val) {
$$key = $val;
}

// eval the view
eval('?>' . sys()->content . '<?');
sys()->content = ob_get_contents();
ob_end_clean();
require_once(CONSTANT('SYSTEM_ROOT') . '/views/' . sys()->_TEMPLATE);
}
Loading

0 comments on commit 8443ec0

Please sign in to comment.