Skip to content

Commit

Permalink
Using Event Handlers instead of a fixed closure for the response hand…
Browse files Browse the repository at this point in the history
…lers.
  • Loading branch information
daniel-aranda committed Mar 15, 2015
1 parent 9ebff07 commit c73bf50
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 33 deletions.
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 7 additions & 10 deletions core/RESTful/Response.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
namespace RESTful;
use PHPRocks\EventHandler;
use RESTful\Exception\Response\CanNotSwapResponseType;
use RESTful\Exception\Response\InvalidResponseType;

Expand All @@ -10,12 +11,12 @@
*/
final class Response {

use EventHandler;

const JSON = 'application/json';
const TEXT = 'text/plain';
const HTML = 'text/html';



public static $types = [
self::JSON,
self::HTML,
Expand All @@ -28,9 +29,9 @@ final class Response {

private $response = null;

public $outputHandler = null;
const OUTPUT_EVENT = 'output_event';

public $addHeaderHandler = null;
const HEADER_ADDED_EVENT = 'added_event';

private function validateHeaders(){
if( $this->header_set ) {
Expand All @@ -49,9 +50,7 @@ private function validateOutputHeaders(){
}

public function addHeader($header){
if( is_callable($this->addHeaderHandler) ){
$this->addHeaderHandler->__invoke($this, $header);
}
$this->trigger(self::HEADER_ADDED_EVENT, [$this, $header]);
}

public function setResponse($response){
Expand All @@ -69,9 +68,7 @@ public function render(){
$this->response = json_encode($this->response);
}

if( is_callable($this->outputHandler) ){
$this->outputHandler->__invoke($this);
}
$this->trigger(self::OUTPUT_EVENT, [$this]);

}

Expand Down
10 changes: 5 additions & 5 deletions public/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@

$response = new \RESTful\Response();

$response->addHeaderHandler = function(\RESTful\Response $response, $header){
$response->addEventHandler(\RESTful\Response::HEADER_ADDED_EVENT, function(\RESTful\Response $response, $header){
header($header);
};
});

$response->outputHandler = function(\RESTful\Response $response){
$response->addEventHandler(\RESTful\Response::OUTPUT_EVENT, function(\RESTful\Response $response){
echo $response->getResponse();
};
});

$request = \RESTful\Request::factory('');
$request = \RESTful\Request::factory('test_service/add');

$server = new \RESTful\Server(
$response
Expand Down
28 changes: 14 additions & 14 deletions tests/RESTful/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ protected function setUp() {
public function testRenderResponse() {

$output = null;
$this->response->outputHandler = function(Response $response) use (&$output){
$this->response->addEventHandler(Response::OUTPUT_EVENT, function(Response $response) use (&$output){
$output = $response->getResponse();
};
});

$this->response->setResponse('Hello World');

Expand All @@ -38,9 +38,9 @@ public function testRenderResponse() {
public function testMultiRenderResponse() {

$output = '';
$this->response->outputHandler = function(Response $response) use (&$output){
$this->response->addEventHandler(Response::OUTPUT_EVENT, function(Response $response) use (&$output){
$output .= $response->getResponse();
};
});

$this->response->setResponseType(Response::TEXT);

Expand All @@ -59,9 +59,9 @@ public function testMultiRenderResponse() {
public function testReceiveHeaders() {

$received_header = null;
$this->response->addHeaderHandler = function(Response $response, $header) use (&$received_header){
$this->response->addEventHandler(Response::HEADER_ADDED_EVENT, function(Response $response, $header) use (&$received_header){
$received_header = $header;
};
});

$this->response->render();

Expand All @@ -74,9 +74,9 @@ public function testReceiveHeaders() {
public function testHtmlHeaders() {

$received_header = null;
$this->response->addHeaderHandler = function(Response $response, $header) use (&$received_header){
$this->response->addEventHandler(Response::HEADER_ADDED_EVENT, function(Response $response, $header) use (&$received_header){
$received_header = $header;
};
});

$this->response->setResponseType(Response::HTML);
$this->response->render();
Expand All @@ -89,9 +89,9 @@ public function testHtmlHeaders() {
public function testTextHeaders() {

$received_header = null;
$this->response->addHeaderHandler = function(Response $response, $header) use (&$received_header){
$this->response->addEventHandler(Response::HEADER_ADDED_EVENT, function(Response $response, $header) use (&$received_header){
$received_header = $header;
};
});

$this->response->setResponseType(Response::TEXT);
$this->response->render();
Expand All @@ -105,9 +105,9 @@ public function testSwapHeadersWithContentNotYetRendered() {


$received_header = null;
$this->response->addHeaderHandler = function(Response $response, $header) use (&$received_header){
$this->response->addEventHandler(Response::HEADER_ADDED_EVENT, function(Response $response, $header) use (&$received_header){
$received_header = $header;
};
});

$this->response->setResponseType(Response::HTML);

Expand All @@ -125,9 +125,9 @@ public function testSwapHeadersWithContentRendered() {
$this->setExpectedException('RESTful\Exception\Response\CanNotSwapResponseType');

$received_header = null;
$this->response->addHeaderHandler = function(Response $response, $header) use (&$received_header){
$this->response->addEventHandler(Response::HEADER_ADDED_EVENT, function(Response $response, $header) use (&$received_header){
$received_header = $header;
};
});

$this->response->setResponseType(Response::HTML);
$this->response->render();
Expand Down

0 comments on commit c73bf50

Please sign in to comment.