Skip to content

Commit

Permalink
handle request directly to determine content type
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Nov 5, 2015
1 parent e80c116 commit 7dcad1d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
9 changes: 5 additions & 4 deletions Slim/Handlers/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function __construct($displayErrorDetails = false)
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Exception $exception)
{
$contentType = $this->determineContentType($request->getHeaderLine('Accept'));
$contentType = $this->determineContentType($request);
switch ($contentType) {
case 'application/json':
$output = $this->renderJsonErrorMessage($exception);
Expand Down Expand Up @@ -221,14 +221,15 @@ private function createCdataSection($content)

/**
* Determine which content type we know about is wanted using Accept header
* content.
*
* @param string $acceptHeader Accept header from request
* @param ServerRequestInterface $request
* @return string
*/
private function determineContentType($acceptHeader)
private function determineContentType(ServerRequestInterface $request)
{
$acceptHeader = $request->getHeaderLine('Accept');
$selectedContentTypes = array_intersect(explode(',', $acceptHeader), $this->knownContentTypes);

if (count($selectedContentTypes)) {
return $selectedContentTypes[0];
}
Expand Down
9 changes: 5 additions & 4 deletions Slim/Handlers/NotAllowed.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
$output = 'Allowed methods: ' . $allow;
} else {
$status = 405;
$contentType = $this->determineContentType($request->getHeaderLine('Accept'));
$contentType = $this->determineContentType($request);
switch ($contentType) {
case 'application/json':
$output = '{"message":"Method not allowed. Must be one of: ' . $allow . '"}';
Expand Down Expand Up @@ -104,14 +104,15 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res

/**
* Determine which content type we know about is wanted using Accept header
* content.
*
* @param string $acceptHeader Accept header from request
* @param ServerRequestInterface $request
* @return string
*/
private function determineContentType($acceptHeader)
private function determineContentType(ServerRequestInterface $request)
{
$acceptHeader = $request->getHeaderLine('Accept');
$selectedContentTypes = array_intersect(explode(',', $acceptHeader), $this->knownContentTypes);

if (count($selectedContentTypes)) {
return $selectedContentTypes[0];
}
Expand Down
9 changes: 5 additions & 4 deletions Slim/Handlers/NotFound.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class NotFound
public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
{

$contentType = $this->determineContentType($request->getHeaderLine('Accept'));
$contentType = $this->determineContentType($request);
switch ($contentType) {
case 'application/json':
$output = '{"message":"Not found"}';
Expand Down Expand Up @@ -103,14 +103,15 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res

/**
* Determine which content type we know about is wanted using Accept header
* content.
*
* @param string $acceptHeader Accept header from request
* @param ServerRequestInterface $request
* @return string
*/
private function determineContentType($acceptHeader)
private function determineContentType(ServerRequestInterface $request)
{
$acceptHeader = $request->getHeaderLine('Accept');
$selectedContentTypes = array_intersect(explode(',', $acceptHeader), $this->knownContentTypes);

if (count($selectedContentTypes)) {
return $selectedContentTypes[0];
}
Expand Down

0 comments on commit 7dcad1d

Please sign in to comment.