-
-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add default response header support #211
Merged
philipobenito
merged 1 commit into
thephpleague:master
from
shadowhand:feature/extend-strategies
Oct 16, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace League\Route\Strategy; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
|
||
abstract class AbstractStrategy implements StrategyInterface | ||
{ | ||
/** @var array */ | ||
protected $defaultResponseHeaders = []; | ||
|
||
/** | ||
* Get current default response headers | ||
* | ||
* @return array | ||
*/ | ||
public function getDefaultResponseHeaders() : array | ||
{ | ||
return $this->defaultResponseHeaders; | ||
} | ||
|
||
/** | ||
* Add or replace a default response header | ||
* | ||
* @param string $name | ||
* @param string $value | ||
* @return static | ||
*/ | ||
public function addDefaultResponseHeader(string $name, string $value) : AbstractStrategy | ||
{ | ||
$this->defaultResponseHeaders[strtolower($name)] = $value; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Add multiple default response headers | ||
* | ||
* @param array $headers | ||
* @return static | ||
*/ | ||
public function addDefaultResponseHeaders(array $headers) : AbstractStrategy | ||
{ | ||
foreach ($headers as $name => $value) { | ||
$this->addDefaultResponseHeader($name, $value); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Apply default response headers | ||
* | ||
* Headers that already exist on the response will NOT be replaced. | ||
* | ||
* @param \Psr\Http\Message\ResponseInterface $response | ||
* @return \Psr\Http\Message\ResponseInterface | ||
*/ | ||
protected function applyDefaultResponseHeaders(ResponseInterface $response) : ResponseInterface | ||
{ | ||
foreach ($this->defaultResponseHeaders as $name => $value) { | ||
if (! $response->hasHeader($name)) { | ||
$response = $response->withHeader($name, $value); | ||
} | ||
} | ||
|
||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
use Psr\Http\Message\{ResponseFactoryInterface, ResponseInterface, ServerRequestInterface}; | ||
use Psr\Http\Server\{MiddlewareInterface, RequestHandlerInterface}; | ||
|
||
class JsonStrategy implements ContainerAwareInterface, StrategyInterface | ||
class JsonStrategy extends AbstractStrategy implements ContainerAwareInterface | ||
{ | ||
use ContainerAwareTrait; | ||
|
||
|
@@ -27,6 +27,8 @@ class JsonStrategy implements ContainerAwareInterface, StrategyInterface | |
public function __construct(ResponseFactoryInterface $responseFactory) | ||
{ | ||
$this->responseFactory = $responseFactory; | ||
|
||
$this->addDefaultResponseHeader('content-type', 'application/json'); | ||
} | ||
|
||
/** | ||
|
@@ -39,14 +41,11 @@ public function invokeRouteCallable(Route $route, ServerRequestInterface $reques | |
|
||
if ($this->isJsonEncodable($response)) { | ||
$body = json_encode($response); | ||
$response = $this->responseFactory->createResponse(); | ||
$response = $response->withStatus(200); | ||
$response = $this->responseFactory->createResponse(200); | ||
$response->getBody()->write($body); | ||
} | ||
|
||
if ($response instanceof ResponseInterface && ! $response->hasHeader('content-type')) { | ||
$response = $response->withAddedHeader('content-type', 'application/json'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used |
||
} | ||
$response = $this->applyDefaultResponseHeaders($response); | ||
|
||
return $response; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for this, as the default for
createResponse
is a response with HTTP 200 status. I still made the status explicit for clarity.Also, I did not modify
JsonStrategy::getExceptionHandler
response handling, as there is no assurance that the body would conform to a custom JSON format like JSON-API or HAL. Since the body defined for an exception is explicit, it would likely cause issues to apply the default headers.