Skip to content

Commit

Permalink
Add setting of headers in "Response" (if any)
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Feb 18, 2016
1 parent 9162a1d commit 96c5062
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
25 changes: 15 additions & 10 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,35 @@ public function run()

list($request, $response) = $this->components->getHttp();

$route = $this->getRoute($request);
$result = $this->getResponse($request);

if ($route instanceof ResponseInterface) {
$response = $route;
if ($result instanceof ResponseInterface) {
$response = $result;
}

// Sets the HTTP response code.
http_response_code($response->getStatusCode());

// Gets the selected route and sets it as the content.
if ( ! $response->getBody() || $response->getBody() == '') {
$response->getBody()->write($route);
$response->getBody()->write($result);
}

// Sets the specified headers, if any.
foreach ($response->getHeaders() as $name => $value) {
header($name . ': ' . implode(',', $value));
}

// Sets the HTTP response code.
http_response_code($response->getStatusCode());

echo $response->getBody();
}

/**
* Gets the route result from the dispatcher.
* Gets the response from the dispatcher.
*
* @param \Psr\Http\Message\RequestInterface $request
* @return string
* @return mixed
*/
private function getRoute(RequestInterface $request)
private function getResponse(RequestInterface $request)
{
// Gets the requested route
$route = $this->components->getDispatcher()->dispatch(
Expand Down
7 changes: 4 additions & 3 deletions tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@ public function testRunMethod()

$application = new Application($this->components);

echo $application->run();
$application->run();
}

/**
* Tests the run() method with a response as result.
*
* @runInSeparateProcess
* @return void
*/
public function testRunMethodWithResponse()
Expand All @@ -134,7 +135,7 @@ public function testRunMethodWithResponse()

$application = new Application($this->components);

echo $application->run();
$application->run();
}

/**
Expand All @@ -150,7 +151,7 @@ public function testRunMethodWithCallback()

$application = new Application($this->components);

echo $application->run();
$application->run();
}

/**
Expand Down
9 changes: 7 additions & 2 deletions tests/Fixture/TestClassWithResponseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ public function __construct(ResponseInterface $response)

public function index()
{
$this->response->getBody()->write('Hello');
$response = $this->response
->withStatus(200)
->withHeader('Content-Type', 'application/json')
->withHeader('Access-Control-Allow-Credentials', 'true');

return $this->response;
$response->getBody()->write('Hello');

return $response;
}
}

0 comments on commit 96c5062

Please sign in to comment.