summaryrefslogtreecommitdiffstats
path: root/lib/public/appframework
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2014-05-12 01:07:49 +0200
committerMorris Jobke <hey@morrisjobke.de>2014-05-12 01:07:49 +0200
commit649999547411cef63c5488a2b859fdf4ae150dd8 (patch)
tree2aa3180155933054487f7ed05b05171f66d24b3d /lib/public/appframework
parent7a6ff56b138f1b7d5f1db49855e5069cc241b052 (diff)
parent63f2f16b852e126cbbf478f2d25232195c5a37e4 (diff)
downloadnextcloud-server-649999547411cef63c5488a2b859fdf4ae150dd8.tar.gz
nextcloud-server-649999547411cef63c5488a2b859fdf4ae150dd8.zip
Merge pull request #8477 from owncloud/better-controllers
Better appframework controllers
Diffstat (limited to 'lib/public/appframework')
-rw-r--r--lib/public/appframework/controller.php67
-rw-r--r--lib/public/appframework/http.php2
-rw-r--r--lib/public/appframework/http/downloadresponse.php2
-rw-r--r--lib/public/appframework/http/iresponseserializer.php27
-rw-r--r--lib/public/appframework/http/jsonresponse.php2
-rw-r--r--lib/public/appframework/http/redirectresponse.php2
-rw-r--r--lib/public/appframework/http/response.php2
-rw-r--r--lib/public/appframework/http/templateresponse.php12
-rw-r--r--lib/public/appframework/iapi.php2
-rw-r--r--lib/public/appframework/middleware.php2
10 files changed, 108 insertions, 12 deletions
diff --git a/lib/public/appframework/controller.php b/lib/public/appframework/controller.php
index f42eba172c7..50b5ed3a80d 100644
--- a/lib/public/appframework/controller.php
+++ b/lib/public/appframework/controller.php
@@ -3,7 +3,7 @@
* ownCloud - App Framework
*
* @author Bernhard Posselt
- * @copyright 2012 Bernhard Posselt nukeawhale@gmail.com
+ * @copyright 2012, 2014 Bernhard Posselt <dev@bernhard-posselt.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
@@ -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 $responders;
/**
* constructor of the controller
@@ -66,11 +70,66 @@ abstract class Controller {
IRequest $request){
$this->appName = $appName;
$this->request = $request;
+
+ // default responders
+ $this->responders = 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 $responder
+ */
+ protected function registerResponder($format, \Closure $responder) {
+ $this->responders[$format] = $responder;
+ }
+
+
+ /**
+ * 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 buildResponse($response, $format='json') {
+ if(array_key_exists($format, $this->responders)) {
+
+ if ($this->serializer) {
+ $response = $this->serializer->serialize($response);
+ }
+
+ $responder = $this->responders[$format];
+
+ return $responder($response);
+
+ } else {
+ throw new \DomainException('No responder 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.php b/lib/public/appframework/http.php
index 60f314202cc..c6e2ff8846f 100644
--- a/lib/public/appframework/http.php
+++ b/lib/public/appframework/http.php
@@ -3,7 +3,7 @@
* ownCloud - App Framework
*
* @author Bernhard Posselt, Thomas Tanghus, Bart Visscher
- * @copyright 2012 Bernhard Posselt nukeawhale@gmail.com
+ * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
diff --git a/lib/public/appframework/http/downloadresponse.php b/lib/public/appframework/http/downloadresponse.php
index d3c2818e828..6b61490341e 100644
--- a/lib/public/appframework/http/downloadresponse.php
+++ b/lib/public/appframework/http/downloadresponse.php
@@ -4,7 +4,7 @@
* ownCloud - App Framework
*
* @author Bernhard Posselt
- * @copyright 2012 Bernhard Posselt nukeawhale@gmail.com
+ * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
diff --git a/lib/public/appframework/http/iresponseserializer.php b/lib/public/appframework/http/iresponseserializer.php
new file mode 100644
index 00000000000..c16e106df34
--- /dev/null
+++ b/lib/public/appframework/http/iresponseserializer.php
@@ -0,0 +1,27 @@
+<?php
+/**
+ * ownCloud - App Framework
+ *
+ * @author Bernhard Posselt
+ * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.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/jsonresponse.php b/lib/public/appframework/http/jsonresponse.php
index 6d029b7464a..c6360e0a0f5 100644
--- a/lib/public/appframework/http/jsonresponse.php
+++ b/lib/public/appframework/http/jsonresponse.php
@@ -3,7 +3,7 @@
* ownCloud - App Framework
*
* @author Bernhard Posselt
- * @copyright 2012 Bernhard Posselt nukeawhale@gmail.com
+ * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
diff --git a/lib/public/appframework/http/redirectresponse.php b/lib/public/appframework/http/redirectresponse.php
index 416e1533635..a1b482c6b3b 100644
--- a/lib/public/appframework/http/redirectresponse.php
+++ b/lib/public/appframework/http/redirectresponse.php
@@ -4,7 +4,7 @@
* ownCloud - App Framework
*
* @author Bernhard Posselt
- * @copyright 2012 Bernhard Posselt nukeawhale@gmail.com
+ * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
diff --git a/lib/public/appframework/http/response.php b/lib/public/appframework/http/response.php
index 559d14dd7e7..20e936bb860 100644
--- a/lib/public/appframework/http/response.php
+++ b/lib/public/appframework/http/response.php
@@ -3,7 +3,7 @@
* ownCloud - App Framework
*
* @author Bernhard Posselt, Thomas Tanghus, Bart Visscher
- * @copyright 2012 Bernhard Posselt nukeawhale@gmail.com
+ * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
diff --git a/lib/public/appframework/http/templateresponse.php b/lib/public/appframework/http/templateresponse.php
index f5baf788ada..02589f4e2a4 100644
--- a/lib/public/appframework/http/templateresponse.php
+++ b/lib/public/appframework/http/templateresponse.php
@@ -3,7 +3,7 @@
* ownCloud - App Framework
*
* @author Bernhard Posselt
- * @copyright 2012 Bernhard Posselt nukeawhale@gmail.com
+ * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
@@ -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;
}
diff --git a/lib/public/appframework/iapi.php b/lib/public/appframework/iapi.php
index c4aeea2d4e5..9af251be850 100644
--- a/lib/public/appframework/iapi.php
+++ b/lib/public/appframework/iapi.php
@@ -3,7 +3,7 @@
* ownCloud - App Framework
*
* @author Bernhard Posselt
- * @copyright 2012 Bernhard Posselt nukeawhale@gmail.com
+ * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
diff --git a/lib/public/appframework/middleware.php b/lib/public/appframework/middleware.php
index 24f31939935..2e1111e9d5d 100644
--- a/lib/public/appframework/middleware.php
+++ b/lib/public/appframework/middleware.php
@@ -3,7 +3,7 @@
* ownCloud - App Framework
*
* @author Bernhard Posselt
- * @copyright 2012 Bernhard Posselt nukeawhale@gmail.com
+ * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE