Skip to content

Commit

Permalink
first copy minus path
Browse files Browse the repository at this point in the history
  • Loading branch information
allynbauer committed Apr 11, 2010
1 parent 7f071c2 commit 4277ef6
Show file tree
Hide file tree
Showing 17 changed files with 1,407 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Condor is a real, real simple PHP framework.
So simple, I don't feel like explaining it right now.
22 changes: 22 additions & 0 deletions controllers/app_controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/*
Original Author: Allyn Bauer
Faculty: John Wynstra
Date: 2010-02-26 (Fri, 26 Feb 2010)
Rod Library - University of Northern Iowa
=========================================
*/

class AppController {
public $routes = array(
'imagine' => 'dream/:for/win/:sf'
);

function index() {
}

function imagine($params) {
sys()->content = "url parameter ':for' is: " . $params['for'] . "<br>url parameter ':sf' is: " . $params['sf'];
}
}
9 changes: 9 additions & 0 deletions controllers/example_controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

class ExampleController {
function test() {
// if you have no view for an action the content will just be outputted to screen
sys()->content = 'do you see how excellent this thing is. its so handy!' .
'<br><a href="<?php echo $web_root ?>">home</a>';
}
}
20 changes: 20 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/*
Original Author: Allyn Bauer
Date: 2010-02-26 (Fri, 26 Feb 2010)
=========================================
*/

require_once('lib/init.php');

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

?>
19 changes: 19 additions & 0 deletions lib/database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

$hostname = 'host';
$username = 'username';
$password = 'password';
$database = 'db';

try {
if ($hostname != 'host') {
$db = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
}
} catch (PDOException $e) {
echo 'A database connection error has occured.<br />';
echo 'Please try again later or contact the <a href="mailto:' . $_SERVER['SERVER_ADMIN'] . '">system administrator.</a>';
echo $e->getMessage();
die();
}

?>
49 changes: 49 additions & 0 deletions lib/flash.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
// (c) 2009 by Allyn Bauer <[email protected]>
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;
}
}
}

?>
65 changes: 65 additions & 0 deletions lib/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

function has_error() {
return sys()->error_count != 0;
}

function add_error($txt) {
sys()->flash->error = $txt;
sys()->_error_count += 1;
}

function image_path($image) {
return CONSTANT('WEB_ROOT') . "/resources/images/$image";
}

function include_stylesheet($style) {
$path = CONSTANT('WEB_ROOT') . "/resources/styles/$style.css";
return "<link href='$path' rel='stylesheet' type='text/css' />\n";
}

function include_script($script) {
$path = CONSTANT('WEB_ROOT') . "/resources/scripts/$script.js";
return "<script src='$path' type='text/javascript'></script>\n";
}

// 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);
}

// return a string that is custom js or css includes for this |view|, if they exist.
function custom_includes($view) {
$root = CONSTANT('SYSTEM_ROOT');
$js_file = "$root/javascript/custom/$view.js";
$css_file = "$root/css/custom/$view.css";
$returns = '';
if (file_exists($js_file)) $returns .= "<script src='javascript/custom/$view.js' type='text/javascript'></script>";
if (file_exists($css_file)) $returns .= "<link href='css/custom/$view.css' rel='stylesheet' type='text/css' />";
return $returns;
}

// 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/_template.php');
}

?>
9 changes: 9 additions & 0 deletions lib/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
/*
Original Author: Allyn Bauer
Faculty: John Wynstra
Date: 2010-02-26 (Fri, 26 Feb 2010)
Rod Library - University of Northern Iowa
=========================================
*/
35 changes: 35 additions & 0 deletions lib/init.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

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

define('SYSTEM_ROOT', dirname(dirname(__FILE__)));
define('WEB_ROOT', '/sites/app_template'); // NO TRAILING SLASH

$dir = dirname(__FILE__);

require_once("$dir/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("$dir/flash.php");
session_start();
$flash = new FlashHelper();

// include the needed files
require_once("$dir/database.php");
require_once("$dir/functions.php");
require_once("$dir/helpers.php");
require_once("$dir/openstruct.php");
require_once("$dir/sys.php");
require_once("$dir/path_manager.php");

// for rendering
$content = '';
$mappings = array();
$data = new OpenStruct();
$_error_count = 0;

?>
21 changes: 21 additions & 0 deletions lib/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;
}
}

?>
107 changes: 107 additions & 0 deletions lib/path_manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?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);

// handle this match
$params = array();
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) {
/* error */
}
}
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";
}

}
Loading

0 comments on commit 4277ef6

Please sign in to comment.