Skip to content

Commit

Permalink
Accept header is now parsed in Controller constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
zozlak committed Jan 2, 2019
1 parent dced2a6 commit eef013e
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/zozlak/rest/HttpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ class HttpController {
static public function reportError(Throwable $ex,
int $verbosity = self::ERR_HIDE) {
if (!headers_sent() && !self::$errorReported) {
$code = $ex->getCode();
$code = $code < 300 || $code >= 600 ? 500 : $code;
$msg = explode("\n", $ex->getMessage())[0];
$msg = empty($msg) ? 'Internal Server Error' : $msg;
$code = $ex->getCode();
$code = $code < 300 || $code >= 600 ? 500 : $code;
$msg = explode("\n", $ex->getMessage())[0];
$msg = empty($msg) ? 'Internal Server Error' : $msg;
header('HTTP/1.1 ' . $code . ' ' . $msg);
self::$errorReported = true;
}
Expand Down Expand Up @@ -197,6 +197,8 @@ public function __construct(string $namespace = '', string $baseUrl = '',
$this->baseUrl = parse_url($baseUrl);
$this->urlSource = $urlSource;
$this->headersFormatter = new HeadersFormatter();

$this->accept = self::parsePriorityList(filter_input(\INPUT_SERVER, 'HTTP_ACCEPT') ?? '');
}

/**
Expand Down Expand Up @@ -302,7 +304,7 @@ public function setFormatters(array $map): HttpController {
$this->formattersMap = $map;
return $this;
}

/**
*
* @return bool
Expand Down Expand Up @@ -401,8 +403,7 @@ private function handleException(Throwable $ex) {
*
*/
private function parseAccept() {
$this->accept = self::parsePriorityList(filter_input(\INPUT_SERVER, 'HTTP_ACCEPT') ?? '');
$format = $this->getAccept(array_keys($this->formattersMap));
$format = $this->getAccept(array_keys($this->formattersMap));
if (count($format) === 0) {
$class = $this->formattersMap['default'];
} else {
Expand Down

0 comments on commit eef013e

Please sign in to comment.