Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branches 'dev' and 'master'

  • Loading branch information...
commit e063f2a295c97e5885eeddbd1d6b71a23f6587c2 2 parents 3960548 + 8e5b888
@brucespang authored
View
9 README
@@ -2,11 +2,10 @@ Frank.php is a clone of Sinatra.rb. It's much like Sinatra, with a few things mi
What Frank.php is missing:
1. Spiffy template languages like haml and sass.
-2. Middleware.
-3. Error handling besides not_found(function(){});
-4. Environments
-5. Testing
-6. Routes with a block parameter. You'll have to use $params['captures'][0] instead.
+2. Error handling besides not_found(function(){});
+3. Environments
+4. Testing
+5. Routes with a block parameter. You'll have to use $params['captures'][0] instead.
Here is Sinatra's hello world:
require 'sinatra'
View
45 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,11 +88,11 @@ class Frank{
private static $templates = array();
/**
- * Template directory location
+ * Array of middleware classes
*
- * @var string
+ * @var array
*/
- public static $view_path = '';
+ private static $middleware = array();
/**
* Public functions
@@ -155,6 +155,8 @@ public static function call($options=array()){
public static function render_template($name, $options){
$locals = $options['locals'] ? $options['locals'] : array();
+ $view_path = settings::get('views');
+
if(isset(self::$templates[$name])){
$template = self::$templates[$name];
@@ -164,14 +166,14 @@ public static function render_template($name, $options){
self::$body .= ob_get_contents();
ob_end_clean();
- }elseif(file_exists(self::$view_path.'/'.$name.'.html')){
+ }elseif(file_exists($view_path.'/'.$name)){
$template = function($path, $locals){
require($path);
};
ob_start();
- $template(self::$view_path.'/'.$name.'.html', $locals);
+ $template($view_path.'/'.$name, $locals);
self::$body .= ob_get_contents();
ob_end_clean();
}
@@ -182,7 +184,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 +244,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 +362,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
12 frank/frank.php
@@ -2,6 +2,7 @@
require('helpers.php');
require('core.php');
+ require('settings.php');
require('library.php');
/**
@@ -9,8 +10,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
35 frank/library.php
@@ -55,10 +55,28 @@ function not_found($function){
* @param array $options Options for Frank
*/
function set($options){
- if(isset($options['views']))
- Frank::$view_path = $options['views'];
+ foreach($options as $option => $value)
+ settings::set($option, $value);
}
-
+
+ /**
+ * Sets a setting to true
+ *
+ * @param string $setting Name of setting
+ */
+ function enable($setting){
+ settings::set($setting, true);
+ }
+
+ /**
+ * Sets a setting to false
+ *
+ * @param string $setting Name of setting
+ */
+ function disable($setting){
+ settings::set($setting, false);
+ }
+
/**
* API for adding a template function
*
@@ -137,7 +155,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
38 frank/settings.php
@@ -0,0 +1,38 @@
+<?php
+
+ class settings{
+
+ /**
+ * Array of settings and their values
+ *
+ * @var array
+ */
+ private static $settings = array();
+
+ /**
+ * Set a setting
+ *
+ * @param string $setting_name Name of the setting
+ * @param mixed $value Value of the setting
+ */
+ function set($setting_name, $value){
+ self::$settings[$setting_name] = $value;
+ }
+
+ /**
+ * Get the value of a setting
+ *
+ * @param string $setting_name Name of the setting
+ * @return string Value of the setting
+ */
+ function get($setting_name){
+ if(gettype(self::$settings[$setting_name]) == 'object')
+ $value = call_user_func(self::$settings[$setting_name]);
+ else
+ $value = self::$settings[$setting_name];
+
+ return $value;
+ }
+ }
+
+?>
View
25 index.php
@@ -10,14 +10,18 @@ function hello($name){
configure(function(){
$test = 'test';
set(array('views' => dirname(__FILE__) . '/templates'));
+ set(array('extension' => function(){
+ return '.'.end(explode('.', __FILE__));
+ }));
});
after(function(){
- echo " AFTER!";
+ echo ". Good bye!";
});
get("/", function(){
- echo "Welcome to Frank.php";
+ echo "Welcome to Frank";
+ echo settings::get('extension');
});
get("/template", function(){
@@ -63,5 +67,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];
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.