Permalink
Browse files

Restructured the response and request process procedures. The middlew…

…are classes now have to return the same object as the action(Request and Response), instead of using pass by reference.
  • Loading branch information...
1 parent 42f74aa commit 902e05595749f41205fa1c85fe85e35082579a3d @tdolsen committed Sep 21, 2010
View
@@ -3,6 +3,7 @@
namespace regain;
use regain\HTTP\Response
+ , reagin\HTTP\Request
, regain\Settings
, regain\Exceptions\TypeException
, regain\Middleware\MiddlewareInterface
@@ -55,24 +56,6 @@ public function __construct(array $middleware) {
* is an instance of Response(and in that case goes staight
* to output). Returns nothing else.
*/
- private function process($action, &$request, &$response = null) {
- $action = 'process_' . $action;
-
- foreach($this->middleware as $mw) {
- // Only if the middleware has the action
- if(method_exists($mw, $action)) {
- $res = $mw->$action($request, $response);
-
- if($res != null) {
- if(!$res instanceof Response) {
- throw new TypeException('The middleware class "' . get_class($mw) . '" returned an unknown result for "process_' . $action . '". Must return an instance of HTTP\Response or null.');
- }
-
- return $res;
- }
- }
- }
- }
/**
* A simple wrapper method around {@link process()} for processing the request.
@@ -81,8 +64,23 @@ private function process($action, &$request, &$response = null) {
*
* @return Response|null {@see process()}
*/
- public function process_request(&$request) {
- return $this->process('request', $request);
+ public function process_request($request) {
+ foreach($this->middleware as $mw) {
+ if(method_exists($mw, 'process_request')) {
+ $res = $mw->process_request($request);
+
+ if(!$res instanceof Request) {
+ if($res instanceof Response) {
+ return $res;
+ } else {
+ throw new TypeException('The process_request method expects a Request or Response object in return. "' . get_class($mw) . '" return an object of type "' . gettype($res) . '"');
+ }
+ }
+
+ $request = $res;
+ }
+ }
+ return $request;
}
/**
@@ -93,7 +91,18 @@ public function process_request(&$request) {
*
* @return Response|null {@see process()}
*/
- public function process_response($request, &$response) {
- return $this->process('response', $request, $response);
+ public function process_response($request, $response) {
+ foreach($this->middleware as $mw) {
+ if(method_exists($mw, 'process_response')) {
+ $res = $mw->process_response($request);
+
+ if(!$res instanceof Response) {
+ throw new TypeException('The process_response method expects a Response object in return. "' . get_class($mw) . '" return an object of type "' . gettype($res) . '"');
+ }
+
+ $request = $res;
+ }
+ }
+ return $request;
}
}
@@ -25,7 +25,7 @@ class Common extends MiddlewareAbstract {
*
* @return mixed
*/
- public function process_response($request, &$response) {
+ public function process_response($request, $response) {
// Appends a forward slash if set in the settings
if(Settings::get('append_slash')) {
if($response->status == 404 and substr($request->path, -1, 1) != '/') {
@@ -39,5 +39,7 @@ public function process_response($request, &$response) {
return new ResponseRedirect($path);
}
}
+
+ return $response;
}
}
@@ -17,7 +17,7 @@
*
* @return mixed
*/
- public function process_request(&$request) {}
+ public function process_request($request) {}
/**
* Empty wrapper for process_response.
@@ -27,5 +27,5 @@ public function process_request(&$request) {}
*
* @return mixed
*/
- public function process_response($request, &$response) {}
+ public function process_response($request, $response) {}
}
@@ -20,7 +20,7 @@
* @return mixed Should return either null, or a regain\HTTP\Response object
* to be submitted directly to output
*/
- public function process_request(&$request);
+ public function process_request($request);
/**
* Processes the response object after the view is run. The $response parameter
@@ -34,5 +34,5 @@ public function process_request(&$request);
* @return mixed Should return either null, or a regain\HTTP\Response object
* to be submitted directly to output
*/
- public function process_response($request, &$response);
+ public function process_response($request, $response);
}

0 comments on commit 902e055

Please sign in to comment.