diff options
author | Bernhard Posselt <dev@bernhard-posselt.com> | 2014-05-06 16:29:19 +0200 |
---|---|---|
committer | Bernhard Posselt <dev@bernhard-posselt.com> | 2014-05-11 17:54:08 +0200 |
commit | 80648da43197c91ed52f36cee8bc818038b88eb6 (patch) | |
tree | ea604192691d7b74857ed639311185de0d93504c /lib/public/appframework | |
parent | a252f59cd436d2c005755955bc93ab44544df766 (diff) | |
download | nextcloud-server-80648da43197c91ed52f36cee8bc818038b88eb6.tar.gz nextcloud-server-80648da43197c91ed52f36cee8bc818038b88eb6.zip |
implement most of the basic stuff that was suggested in #8290
Diffstat (limited to 'lib/public/appframework')
-rw-r--r-- | lib/public/appframework/controller.php | 65 | ||||
-rw-r--r-- | lib/public/appframework/http/iresponseserializer.php | 27 | ||||
-rw-r--r-- | lib/public/appframework/http/templateresponse.php | 10 |
3 files changed, 99 insertions, 3 deletions
diff --git a/lib/public/appframework/controller.php b/lib/public/appframework/controller.php index f42eba172c7..f28a1d83a63 100644 --- a/lib/public/appframework/controller.php +++ b/lib/public/appframework/controller.php @@ -28,6 +28,8 @@ namespace OCP\AppFramework; use OCP\AppFramework\Http\TemplateResponse; +use OCP\AppFramework\Http\JSONResponse; +use OCP\AppFramework\Http\IResponseSerializer; use OCP\IRequest; @@ -48,6 +50,8 @@ abstract class Controller { */ protected $request; + private $serializer; + private $formatters; /** * constructor of the controller @@ -66,11 +70,66 @@ abstract class Controller { IRequest $request){ $this->appName = $appName; $this->request = $request; + + // default formatters + $this->formatters = array( + 'json' => function ($response) { + return new JSONResponse($response); + } + ); + } + + + /** + * Registers a serializer that is executed before a formatter is being + * called, useful for turning any data into PHP arrays that can be used + * by a JSONResponse for instance + * @param IResponseSerializer $serializer + */ + protected function registerSerializer(IResponseSerializer $serializer) { + $this->serializer = $serializer; + } + + + /** + * Registers a formatter for a type + * @param string $format + * @param \Closure $closure + */ + protected function registerFormatter($format, \Closure $formatter) { + $this->formatters[$format] = $formatter; + } + + + /** + * Serializes and formats a response + * @param mixed response the value that was returned from a controller and + * is not a Response instance + * @param string $format the format for which a formatter has been registered + * @throws \DomainException if format does not match a registered formatter + * @return Response + */ + public function formatResponse($response, $format='json') { + if(array_key_exists($format, $this->formatters)) { + + if ($this->serializer) { + $response = $this->serializer->serialize($response); + } + + $formatter = $this->formatters[$format]; + + return $formatter($response); + + } else { + throw new \DomainException('No formatter registered for format ' . + $format . '!'); + } } /** * Lets you access post and get parameters by the index + * @deprecated write your parameters as method arguments instead * @param string $key the key which you want to access in the URL Parameter * placeholder, $_POST or $_GET array. * The priority how they're returned is the following: @@ -88,6 +147,7 @@ abstract class Controller { /** * Returns all params that were received, be it from the request * (as GET or POST) or throuh the URL by the route + * @deprecated use $this->request instead * @return array the array with all parameters */ public function getParams() { @@ -97,6 +157,7 @@ abstract class Controller { /** * Returns the method of the request + * @deprecated use $this->request instead * @return string the method of the request (POST, GET, etc) */ public function method() { @@ -106,6 +167,7 @@ abstract class Controller { /** * Shortcut for accessing an uploaded file through the $_FILES array + * @deprecated use $this->request instead * @param string $key the key that will be taken from the $_FILES array * @return array the file in the $_FILES element */ @@ -116,6 +178,7 @@ abstract class Controller { /** * Shortcut for getting env variables + * @deprecated use $this->request instead * @param string $key the key that will be taken from the $_ENV array * @return array the value in the $_ENV element */ @@ -126,6 +189,7 @@ abstract class Controller { /** * Shortcut for getting cookie variables + * @deprecated use $this->request instead * @param string $key the key that will be taken from the $_COOKIE array * @return array the value in the $_COOKIE element */ @@ -136,6 +200,7 @@ abstract class Controller { /** * Shortcut for rendering a template + * @deprecated return a template response instead * @param string $templateName the name of the template * @param array $params the template parameters in key => value structure * @param string $renderAs user renders a full page, blank only your template diff --git a/lib/public/appframework/http/iresponseserializer.php b/lib/public/appframework/http/iresponseserializer.php new file mode 100644 index 00000000000..8ffdd451020 --- /dev/null +++ b/lib/public/appframework/http/iresponseserializer.php @@ -0,0 +1,27 @@ +<?php +/** + * ownCloud - App Framework + * + * @author Bernhard Posselt + * @copyright 2012 Bernhard Posselt nukeawhale@gmail.com + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU Affero General Public + * License along with this library. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCP\AppFramework\Http; + +interface IResponseSerializer { + function serialize($response); +}
\ No newline at end of file diff --git a/lib/public/appframework/http/templateresponse.php b/lib/public/appframework/http/templateresponse.php index f5baf788ada..52355f93cdd 100644 --- a/lib/public/appframework/http/templateresponse.php +++ b/lib/public/appframework/http/templateresponse.php @@ -61,12 +61,16 @@ class TemplateResponse extends Response { * constructor of TemplateResponse * @param string $appName the name of the app to load the template from * @param string $templateName the name of the template + * @param array $params an array of parameters which should be passed to the + * template + * @param string $renderAs how the page should be rendered, defaults to user */ - public function __construct($appName, $templateName) { + public function __construct($appName, $templateName, array $params=array(), + $renderAs='user') { $this->templateName = $templateName; $this->appName = $appName; - $this->params = array(); - $this->renderAs = 'user'; + $this->params = $params; + $this->renderAs = $renderAs; } |