Merge pull request #8783 from owncloud/remove-serializers
Remove controller serializers
This commit is contained in:
commit
3a7b30795c
|
@ -29,7 +29,6 @@ namespace OCP\AppFramework;
|
||||||
|
|
||||||
use OCP\AppFramework\Http\TemplateResponse;
|
use OCP\AppFramework\Http\TemplateResponse;
|
||||||
use OCP\AppFramework\Http\JSONResponse;
|
use OCP\AppFramework\Http\JSONResponse;
|
||||||
use OCP\AppFramework\Http\IResponseSerializer;
|
|
||||||
use OCP\IRequest;
|
use OCP\IRequest;
|
||||||
|
|
||||||
|
|
||||||
|
@ -50,7 +49,6 @@ abstract class Controller {
|
||||||
*/
|
*/
|
||||||
protected $request;
|
protected $request;
|
||||||
|
|
||||||
private $serializer;
|
|
||||||
private $responders;
|
private $responders;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -58,7 +56,7 @@ abstract class Controller {
|
||||||
* @param string $appName the name of the app
|
* @param string $appName the name of the app
|
||||||
* @param IRequest $request an instance of the request
|
* @param IRequest $request an instance of the request
|
||||||
*/
|
*/
|
||||||
public function __construct($appName,
|
public function __construct($appName,
|
||||||
IRequest $request){
|
IRequest $request){
|
||||||
$this->appName = $appName;
|
$this->appName = $appName;
|
||||||
$this->request = $request;
|
$this->request = $request;
|
||||||
|
@ -72,17 +70,6 @@ abstract class Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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
|
* Registers a formatter for a type
|
||||||
* @param string $format
|
* @param string $format
|
||||||
|
@ -104,16 +91,12 @@ abstract class Controller {
|
||||||
public function buildResponse($response, $format='json') {
|
public function buildResponse($response, $format='json') {
|
||||||
if(array_key_exists($format, $this->responders)) {
|
if(array_key_exists($format, $this->responders)) {
|
||||||
|
|
||||||
if ($this->serializer) {
|
|
||||||
$response = $this->serializer->serialize($response);
|
|
||||||
}
|
|
||||||
|
|
||||||
$responder = $this->responders[$format];
|
$responder = $this->responders[$format];
|
||||||
|
|
||||||
return $responder($response);
|
return $responder($response);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw new \DomainException('No responder registered for format ' .
|
throw new \DomainException('No responder registered for format ' .
|
||||||
$format . '!');
|
$format . '!');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,27 +0,0 @@
|
||||||
<?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);
|
|
||||||
}
|
|
|
@ -27,15 +27,8 @@ namespace OCP\AppFramework;
|
||||||
use OC\AppFramework\Http\Request;
|
use OC\AppFramework\Http\Request;
|
||||||
use OCP\AppFramework\Http\TemplateResponse;
|
use OCP\AppFramework\Http\TemplateResponse;
|
||||||
use OCP\AppFramework\Http\JSONResponse;
|
use OCP\AppFramework\Http\JSONResponse;
|
||||||
use OCP\AppFramework\Http\IResponseSerializer;
|
|
||||||
|
|
||||||
|
|
||||||
class ToUpperCaseSerializer implements IResponseSerializer {
|
|
||||||
public function serialize($response) {
|
|
||||||
return array(strtoupper($response));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class ChildController extends Controller {
|
class ChildController extends Controller {
|
||||||
public function custom($in) {
|
public function custom($in) {
|
||||||
$this->registerResponder('json', function ($response) {
|
$this->registerResponder('json', function ($response) {
|
||||||
|
@ -44,12 +37,6 @@ class ChildController extends Controller {
|
||||||
|
|
||||||
return $in;
|
return $in;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function serializer($in) {
|
|
||||||
$this->registerSerializer(new ToUpperCaseSerializer());
|
|
||||||
|
|
||||||
return $in;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class ControllerTest extends \PHPUnit_Framework_TestCase {
|
class ControllerTest extends \PHPUnit_Framework_TestCase {
|
||||||
|
@ -170,16 +157,9 @@ class ControllerTest extends \PHPUnit_Framework_TestCase {
|
||||||
$response = $this->controller->custom('hi');
|
$response = $this->controller->custom('hi');
|
||||||
$response = $this->controller->buildResponse($response, 'json');
|
$response = $this->controller->buildResponse($response, 'json');
|
||||||
|
|
||||||
$this->assertEquals(array(2), $response->getData());
|
$this->assertEquals(array(2), $response->getData());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function testCustomSerializer() {
|
|
||||||
$response = $this->controller->serializer('hi');
|
|
||||||
$response = $this->controller->buildResponse($response, 'json');
|
|
||||||
|
|
||||||
$this->assertEquals(array('HI'), $response->getData());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue