Permalink
Browse files

Middleware fleshed out and Json body parser built atop it

  • Loading branch information...
1 parent fc25b3a commit 56dc266fadb01f73830c1a56de7a2bd52c1666aa @beaucollins committed Apr 23, 2012
View
@@ -6,6 +6,7 @@
require_once 'Response.php';
require_once 'Settings.php';
require_once 'Exceptions.php';
+require_once 'Middleware.php';
class Phluid_App {
@@ -0,0 +1,7 @@
+<?php
+
+interface Phluid_Middleware {
+
+ public function __invoke( $request, $response, $next );
+
+}
@@ -0,0 +1,23 @@
+<?php
+
+class Phluid_Middleware_JsonBodyParser implements Phluid_Middleware {
+
+ private $array;
+
+ function __construct( $array = true ){
+ $this->array = $array;
+ }
+
+ //Just JSON
+ function __invoke( $request, $response, $next ){
+ if ( strpos( $request->getBody(), "{") === 0 || $request->getHeader('Content-Type') == 'application/json' ) {
+ try {
+ $request->setBody( json_decode( $request->getBody(), $this->array ) );
+ } catch (Exception $e) {
+
+ }
+ }
+ $next();
+ }
+
+}
@@ -7,6 +7,7 @@ class Phluid_Request {
var $headers;
var $params;
var $memo = array();
+ var $body;
public static function fromServer(){
@@ -19,14 +20,28 @@ public static function fromServer(){
}
$request = new Phluid_Request( $_SERVER['REQUEST_METHOD'], $path, $_SERVER );
+ $request->setBody( @file_get_contents('php://input') );
return $request;
}
- public function __construct( $method, $path, $headers = array() ){
+ public function __construct( $method, $path, $headers = array(), $body=null ){
$this->method = $method;
$this->path = $path;
$this->headers = $headers;
+ $this->body = $body;
+ }
+
+ public function getBody(){
+ return $this->body;
+ }
+
+ public function setBody( $body ){
+ $this->body = $body;
+ }
+
+ public function getHeader( $key ){
+ Phluid_Utils::array_val( $this->headers, strtoupper($key) );
}
public function __get( $key ){
@@ -95,7 +110,7 @@ public function compileRegex( $pattern ){
public function withPrefix( $prefix ){
if ( stripos( $this->path, $prefix ) === 0 ) {
$new_path = substr( $this->path, strlen( $prefix ) );
- return new Phluid_Request( $this->method, $new_path, $this->headers );
+ return new Phluid_Request( $this->method, $new_path, $this->headers, $this->body );
} else {
return $this;
}
@@ -51,12 +51,12 @@ public function statusMessage(){
public function redirect( $url ){
$this->setHeader( "Location", $url );
- $this->raw_body = null;
+ $this->raw_body = "Redirecting: $url";
}
public function renderString( $string, $content_type="text/plain", $status = 200 ){
$this->status_code = $status;
- $this->raw_body = $string;
+ $this->raw_body = (string) $string;
$this->setHeader( 'Content-Type', $content_type );
}
@@ -1,6 +1,8 @@
<?php
-class Phluid_Route {
+require_once 'Middleware.php';
+
+class Phluid_Route implements Phluid_Middleware {
private $closure;
private $methods;
@@ -20,7 +22,7 @@ public function matches( $request ){
return false;
}
- public function __invoke( $request, $response ){
+ public function __invoke( $request, $response, $next = null ){
$closure = $this->closure;
$closure( $request, $response);
@@ -102,9 +102,16 @@ public function testExceptionHandler(){
public function testPostRequest(){
$this->app->post( '/robot', function( $request, $response ){
-
+ $response->renderString( strlen( $request->getBody() ) );
} );
+ $request = new Phluid_Request( 'POST', '/robot' );
+ $request->setBody( "?something=awesome" );
+
+ $response = $this->app->serve( $request );
+
+ $this->assertSame( '18', $response->getBody() );
+
}
}
@@ -0,0 +1,26 @@
+<?php
+
+require_once 'lib/Phluid/Request.php';
+require_once 'lib/Phluid/Middleware/JsonBodyParser.php';
+
+class Phluid_Middleware_BodyParserTest extends PHPUnit_Framework_TestCase {
+
+ public function testJsonParsing(){
+
+ $thing = new stdClass();
+ $thing->awesome = "YES";
+
+ $request = new Phluid_Request( 'POST', '/', array( 'Content-Type' => 'application/json' ), json_encode( $thing ) );
+
+ $this->assertSame( json_encode( $thing ), $request->getBody() );
+
+ $parser = new Phluid_Middleware_JsonBodyParser( false );
+
+ $next = function() use( $request, $thing ) {
+ $this->assertSame( $thing->awesome, $request->getBody()->awesome );
+ };
+ $parser( $request, null, $next );
+
+ }
+
+}
@@ -42,7 +42,6 @@ public function testRegexCompiling(){
public function testSplatRoutes(){
$request = new Phluid_Request( 'GET', '/user/beau' );
-
$this->assertTrue( $request->parsePath( '/user/(.*)' ) );
$this->assertContains( 'beau', $request->params );
@@ -53,5 +52,11 @@ public function testSplatRoutes(){
$this->assertTrue( $request2->parsePath( '/user' ) );
}
+
+ public function testBody(){
+ $request = new Phluid_Request( 'POST', '/' );
+ $request->setBody( "Hello world" );
+ $this->assertSame( "Hello world", $request->getBody() );
+ }
}

0 comments on commit 56dc266

Please sign in to comment.