diff --git a/recess/recess/framework/AbstractView.class.php b/recess/recess/framework/AbstractView.class.php index 3b50c2f..959767f 100644 --- a/recess/recess/framework/AbstractView.class.php +++ b/recess/recess/framework/AbstractView.class.php @@ -26,7 +26,12 @@ public final function respondWith(Response $response) { if(ResponseCodes::canHaveBody($response->code) && !$response instanceof ForwardingResponse) { $this->response = $response; - $this->render($response); + + while(($format = $response->request->accepts->nextFormat()) !== false) { + if($this->render($format, $response)) { + return true; + } + } } } @@ -100,7 +105,7 @@ protected final function sendHeadersFor(Response $response) { * @param Response $response * @abstract */ - protected abstract function render(Response $response); + protected abstract function render($format, Response $response); } ?> \ No newline at end of file diff --git a/recess/recess/framework/DefaultPolicy.class.php b/recess/recess/framework/DefaultPolicy.class.php index 6964d11..40ab1a0 100644 --- a/recess/recess/framework/DefaultPolicy.class.php +++ b/recess/recess/framework/DefaultPolicy.class.php @@ -3,6 +3,7 @@ Library::import('recess.framework.views.RecessView'); Library::import('recess.framework.views.NativeView'); Library::import('recess.framework.interfaces.IPolicy'); +Library::import('recess.framework.http.MimeTypes'); class DefaultPolicy implements IPolicy { protected $controller; @@ -16,14 +17,9 @@ class DefaultPolicy implements IPolicy { * @return Request The refined Request. */ public function preprocess(Request &$request) { - $this->getHttpMethodFromPost($request); - $this->getFormatFromResourceString($request); - - if($request->format != Formats::XHTML) { - $this->reparameterizeForFormat($request); - } + $this->forceFormatFromResourceString($request); return $request; } @@ -69,7 +65,7 @@ protected function getHttpMethodFromPost(Request &$request) { return $request; } - protected function getFormatFromResourceString(Request &$request) { + protected function forceFormatFromResourceString(Request &$request) { $lastPartIndex = count($request->resourceParts) - 1; if($lastPartIndex < 0) return $request; @@ -77,17 +73,18 @@ protected function getFormatFromResourceString(Request &$request) { $lastDotPosition = strrpos($lastPart, Library::dotSeparator); if($lastDotPosition !== false) { - $substring = substr($lastPart, $lastDotPosition + 1); - if(in_array($substring, Formats::$all)) { - $request->format = $substring; + $format = substr($lastPart, $lastDotPosition + 1); + if($format !== '') { + $request->accepts->forceFormat($format); $request->setResource(substr($request->resource, 0, strrpos($request->resource, Library::dotSeparator))); - } else { - $request->format = Formats::XHTML; } } + return $request; } + // @Todo: Worry about the "input" problem. This isn't based on the format + // but rather it is based on the content-type of the entity. protected function reparameterizeForFormat(Request &$request) { if($request->format == Formats::JSON) { $method = strtolower($request->method); diff --git a/recess/recess/framework/controllers/Controller.class.php b/recess/recess/framework/controllers/Controller.class.php index c978420..77feb93 100644 --- a/recess/recess/framework/controllers/Controller.class.php +++ b/recess/recess/framework/controllers/Controller.class.php @@ -26,10 +26,7 @@ abstract class Controller extends AbstractController { /** @var Application */ protected $application; - - /** The formats/content-types which a controller responds to. */ - protected $formats = array(Formats::XHTML); - + public function __construct($application = null) { $this->application = $application; } @@ -194,7 +191,6 @@ function wrappedServe(Request $request) { if(is_array($this->headers)) { foreach($this->headers as $header) $response->addHeader($header); } unset($response->data['request']); unset($response->data['headers']); - unset($response->data['formats']); return $response; } diff --git a/recess/recess/framework/views/NativeView.class.php b/recess/recess/framework/views/NativeView.class.php index a7f91b8..114f706 100644 --- a/recess/recess/framework/views/NativeView.class.php +++ b/recess/recess/framework/views/NativeView.class.php @@ -2,47 +2,41 @@ Library::import('recess.framework.AbstractView'); class NativeView extends AbstractView { - /** - * Realizes HTTP's body content based on the Response parameter. Responsible - * for returning content in the format desired. The render method likely uses - * inversion of control which delegates to another method within the view to - * realize the Response. - * - * @param Response $response - * @abstract - */ - protected function render(Response $response) { - $this->renderFormats($response); - extract($response->data); - $viewsDir = $response->meta->app->getViewsDir(); - - include($response->meta->viewDir . $response->meta->viewName . '.php'); - } - protected function renderFormats(Response $response) { - switch($response->request->format) { - case Formats::JSON: - $this->render_JSON($response); - default: + protected function render($format, Response $response) { + if($format == 'html') { + $extension = '.php'; + } else { + $extension = ".$format.php"; } - } - - protected function render_JSON(Response $response) { - foreach($response->data as $key => $value) { - if($value instanceof ModelSet) { - $response->data[$key] = $value->toArray(); - } - if($value instanceof Form) { - unset($response->data[$key]); - } - if(substr($key,0,1) == '_') { - unset($response->data[$key]); - } + + $viewTemplate = $response->meta->viewDir . $response->meta->viewName . $extension; + if(file_exists($viewTemplate)) { + extract($response->data); + $viewsDir = $response->meta->app->getViewsDir(); + include($viewTemplate); + return true; + } else { + return false; } - if(isset($response->data['application'])) unset ($response->data['application']); - if(isset($response->data['controller'])) unset ($response->data['controller']); - print json_encode($response->data); - exit; } + +// protected function render_JSON(Response $response) { +// foreach($response->data as $key => $value) { +// if($value instanceof ModelSet) { +// $response->data[$key] = $value->toArray(); +// } +// if($value instanceof Form) { +// unset($response->data[$key]); +// } +// if(substr($key,0,1) == '_') { +// unset($response->data[$key]); +// } +// } +// if(isset($response->data['application'])) unset ($response->data['application']); +// if(isset($response->data['controller'])) unset ($response->data['controller']); +// print json_encode($response->data); +// exit; +// } } ?> \ No newline at end of file diff --git a/recess/recess/framework/views/RecessView.class.php b/recess/recess/framework/views/RecessView.class.php index 9936eba..4a7f521 100644 --- a/recess/recess/framework/views/RecessView.class.php +++ b/recess/recess/framework/views/RecessView.class.php @@ -14,15 +14,18 @@ class RecessView extends NativeView { * @param Response $response * @abstract */ - protected function render(Response $response) { - $this->renderFormats($response); + protected function render($format, Response $response) { $this->loadHelper( 'recess.framework.helpers.Layout', 'recess.framework.helpers.Part', 'recess.framework.helpers.Url', 'recess.framework.helpers.Html'); - parent::render($response); - Layout::extendEnd(); + if(parent::render($format, $response)) { + Layout::extendEnd(); + return true; + } else { + return false; + } } } ?> \ No newline at end of file diff --git a/recess/recess/http/Accepts.class.php b/recess/recess/http/Accepts.class.php index 404b8bc..41dd10b 100644 --- a/recess/recess/http/Accepts.class.php +++ b/recess/recess/http/Accepts.class.php @@ -27,8 +27,13 @@ protected function initFormats() { $this->types = new AcceptsList($this->headers[self::TYPES]); } - public function forceFormat($mimeType) { - $this->headers[self::TYPES] = $mimeType; + public function forceFormat($format) { + $mimeType = MimeTypes::preferredMimeTypeFor($format); + if($mimeType != false) { + $this->headers[self::TYPES] = $mimeType; + } else { + $this->headers[self::TYPES] = ''; + } } public function nextFormat() { diff --git a/recess/recess/http/Environment.class.php b/recess/recess/http/Environment.class.php index fd43583..39d532d 100644 --- a/recess/recess/http/Environment.class.php +++ b/recess/recess/http/Environment.class.php @@ -1,6 +1,7 @@ diff --git a/recess/recess/http/MimeTypes.class.php b/recess/recess/http/MimeTypes.class.php index 9c923f6..97320f4 100644 --- a/recess/recess/http/MimeTypes.class.php +++ b/recess/recess/http/MimeTypes.class.php @@ -31,7 +31,7 @@ static function preferredMimeTypeFor($format) { if(isset(self::$byFormat[$format])) { return self::$byFormat[$format][0]; } else { - return '?/?'; + return false; } } diff --git a/recess/test/recess/http/AcceptsTest.php b/recess/test/recess/http/AcceptsTest.php index cca2b5a..52409b6 100644 --- a/recess/test/recess/http/AcceptsTest.php +++ b/recess/test/recess/http/AcceptsTest.php @@ -108,7 +108,7 @@ function testReset() { } function testOverrideContentTypes() { - $this->acceptOverride->forceFormat(MimeTypes::preferredMimeTypeFor('xml')); + $this->acceptOverride->forceFormat('xml'); $this->assertEquals('xml', $this->acceptOverride->nextFormat()); $this->acceptOverride->resetFormats(); $this->assertEquals('xml', $this->acceptOverride->nextFormat());