Skip to content
Browse files

Basic middleware implementation

  • Loading branch information...
1 parent 49ca241 commit b5e3d0e52bfb1096b3e3393286e70373d272dd92 @brucespang committed Sep 25, 2010
Showing with 86 additions and 14 deletions.
  1. +32 −8 frank/core.php
  2. +9 −2 frank/frank.php
  3. +10 −1 frank/library.php
  4. +18 −1 index.php
  5. +17 −2 tests/FrankTest.php
View
40 frank/core.php
@@ -74,7 +74,7 @@ class Frank{
);
/**
- * List of errors and their corresponding functions
+ * Array of errors and their corresponding functions
*
* @var array
*/
@@ -88,6 +88,13 @@ class Frank{
private static $templates = array();
/**
+ * Array of middleware classes
+ *
+ * @var array
+ */
+ private static $middleware = array();
+
+ /**
* Template directory location
*
* @var string
@@ -182,7 +189,7 @@ public static function render_template($name, $options){
*
* @param array $options Output options
*/
- public static function output($options=array()){
+ public static function output($output, $options=array()){
// Mark Frank as dead if told to die
if(isset($options['die']) && $options['die'] == true)
self::$dead = true;
@@ -242,15 +249,15 @@ public static function output($options=array()){
);
- if(isset($status_codes[self::$status])){
- $status_message = $status_codes[self::$status];
- header('HTTP/1.1 '.self::$status." $status_message");
+ if(isset($status_codes[$output[0]])){
+ $status_message = $status_codes[$output[0]];
+ header('HTTP/1.1 '.$output[0]." $status_message");
}
- foreach(self::$headers as $type => $header)
- header("$type: $header", self::$status);
+ foreach($output[1] as $type => $header)
+ header("$type: $header", $output[0]);
- echo(self::$body);
+ echo($output[2]);
// Clean up status in case this is run again. (Probably shouldn't happen, but who knows?)
self::set_status(array(200, array(), ''));
@@ -360,6 +367,23 @@ public static function set_method($method){
self::$method = $method;
}
+ /**
+ * Adds a piece of middleware
+ *
+ * @param object or string $middleware Object for middleware, or name of middleware class
+ */
+ public static function add_middleware($middleware){
+ self::$middleware[] = $middleware;
+ }
+
+ /**
+ * Gets an array of middleware to use
+ *
+ * @return array List of middleware
+ */
+ public static function middleware(){
+ return self::$middleware;
+ }
/**
* Private functions
View
11 frank/frank.php
@@ -9,8 +9,15 @@
*/
function run(){
if(Frank::was_run() !== true){
- Frank::call();
- Frank::output();
+ $output = Frank::call();
+ foreach(Frank::middleware() as $middleware){
+ if(gettype($middleware) == 'string')
+ $middleware = new $middleware;
+
+ $output = $middleware->call($output);
+ }
+
+ Frank::output($output);
}
}
View
11 frank/library.php
@@ -137,7 +137,16 @@ function halt(){
}
Frank::set_status(array($status, $headers, $body));
- Frank::output(array('die' => true));
+ Frank::output(Frank::get_status(), array('die' => true));
+ }
+
+ /**
+ * Adds a middleware to use
+ *
+ * @param string or object $middleware
+ */
+ function middleware($middleware){
+ Frank::add_middleware($middleware);
}
?>
View
19 index.php
@@ -13,7 +13,7 @@ function hello($name){
});
after(function(){
- echo " AFTER!";
+ echo ". Good bye!";
});
get("/", function(){
@@ -63,5 +63,22 @@ function hello($name){
not_found(function(){
echo "This file wasn't found, yo!";
});
+
+ class Middleware{
+ function call($output){
+ return array(200, array(), 'asdf');
+ }
+ }
+
+ class WrapMiddleware{
+ function call($output){
+ return array(200, array(), "Before $output[2] After");
+ }
+ }
+
+ get("/middleware", function(){
+ middleware('Middleware');
+ middleware('WrapMiddleware');
+ });
?>
View
19 tests/FrankTest.php
@@ -74,6 +74,13 @@ public function testSplat(){
public function testCaptures(){
$this->assertEquals("test", $this->get_data('/captures/test', array('pass' => true)));
}
+
+ /**
+ * Tests if middleware works
+ */
+ public function testMiddleware(){
+ $this->assertEquals("Before asdf After", $this->get_data('/middleware'));
+ }
/**
* Private Functions
@@ -92,8 +99,16 @@ private function get_data($url, $options=array(), $method='get'){
Frank::set_method($method);
Frank::set_run(true);
Frank::set_status(array(200, array(), false));
- $status = Frank::call($options);
- return $status[2];
+ $output = Frank::call($options);
+
+ foreach(Frank::middleware() as $middleware){
+ if(gettype($middleware) == 'string')
+ $middleware = new $middleware;
+
+ $output = $middleware->call($output);
+ }
+
+ return $output[2];
}
}

0 comments on commit b5e3d0e

Please sign in to comment.
Something went wrong with that request. Please try again.