Skip to content

Commit

Permalink
route extra headers support added
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan.trnka committed Nov 17, 2017
1 parent 9e8efe0 commit 3281720
Showing 1 changed file with 88 additions and 66 deletions.
154 changes: 88 additions & 66 deletions src/SmartHTTPRestAPI/JSON/JSONRestAPIRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ class JSONRestAPIRoute extends HTTPRouteDefaultAbstract implements HTTPRoute
*/
protected $allowedURLRegexp;

/** @var JSONRestAPIController[] */
protected $controllerHashMap = [];

/** @var JSONRestAPIController Controller instance initialized by self::bootstrap(). */
protected $controller;

Expand All @@ -26,67 +23,33 @@ class JSONRestAPIRoute extends HTTPRouteDefaultAbstract implements HTTPRoute
/** @var array Arguments for action in controller. */
protected $controllerActionArgs;

/**
* Determine whether Route accepts mime type from Request headers.
*
* @return bool
*/
public function checkAcceptRequestHeader() : bool
{
foreach ($this->request->getAccept() AS $mime) {
if (strcasecmp('application/json', $mime) === 0) {
return true;
}
}
return false;
}

/**
* Determine whether Route supports content type from Request headers.
*
* @return bool
*/
public function checkContentTypeRequestHeader() : bool
{
if ($this->request->typeof('get')) {
return true;
}
if (strcasecmp('application/json', $this->request->getContentType()) === 0) {
return true;
}
return false;
}
/** @var JSONRestAPIController[] */
protected $controllerHashMap = [];

/**
* Add controller to route.
*
* @param string $URIControllerId
* @param JSONRestAPIController $controller
*/
public function addController(string $URIControllerId, JSONRestAPIController $controller): void
{
$this->controllerHashMap[$URIControllerId] = $controller;
}
/** @var string[] Extra headers for every response, e.g. ORIGIN headers, etc. */
protected $extraHeaders = [];

/**
* Determine whether this route is compatible with URL. If false, router skips this route.
*
* @return bool
*/
public function URLIsCompatible() : bool
public function URLIsCompatible(): bool
{
// if regexp is not defined
if (!isset($this->allowedURLRegexp)) {
return false;
}

// test URL and required parts
$fullURL = $this->request->getUrl()->scheme . '://' . $this->request->getUrl()->host . $this->request->getUrl()->path;
$fullURL = $this->request->getUrl()->scheme . '://' . $this->request->getUrl()->host . $this->request->getUrl(
)->path;
if (!preg_match($this->allowedURLRegexp, $fullURL, $matches)) {
return false;
}
#ddd($matches);
if (!isset($matches['controller']) || !isset($matches['id']) || !isset($matches['project']) || !isset($this->controllerHashMap["{$matches['project']}-{$matches['controller']}"])) {
if (!isset($matches['controller']) || !isset($matches['id']) || !isset($matches['project'])
|| !isset($this->controllerHashMap["{$matches['project']}-{$matches['controller']}"])
) {
return false;
}

Expand All @@ -113,7 +76,8 @@ public function URLIsCompatible() : bool
if ($this->request->typeof('DELETE')) {
$this->controllerAction = 'deleteItem';
}
} else {
}
else {
$this->allowedRequestMethods = ['GET', 'POST', 'PUT', 'DELETE'];
$this->controllerActionArgs = [$project];
if ($this->request->typeof('get')) {
Expand All @@ -133,11 +97,64 @@ public function URLIsCompatible() : bool
return true;
}

/**
* Determine whether Route accepts mime type from Request headers.
* @return bool
*/
public function checkAcceptRequestHeader(): bool
{
foreach ($this->request->getAccept() AS $mime) {
if (strcasecmp('application/json', $mime) === 0) {
return true;
}
}
return false;
}

/**
* Determine whether Route supports content type from Request headers.
* @return bool
*/
public function checkContentTypeRequestHeader(): bool
{
if ($this->request->typeof('get')) {
return true;
}
if (strcasecmp('application/json', $this->request->getContentType()) === 0) {
return true;
}
return false;
}

/**
* Determine whether Route supports request method.
* @return bool
*/
public function checkRequestMethod(): bool
{
foreach ($this->getAllowedRequestMethods() AS $method) {
if ($this->request->typeof($method)) {
return true;
break;
}
unset($method);
}
return false;
}

/**
* Get collection of allowed request methods for this route URL. It is very important for handling HTTP405 error.
* @return array
*/
public function getAllowedRequestMethods(): array
{
return $this->allowedRequestMethods;
}

/**
* Setup response headers and body by your controller or other logic.
*
*/
public function prepareResponse() : void
public function prepareResponse(): void
{
try {
call_user_func_array([$this->controller, $this->controllerAction], $this->controllerActionArgs);
Expand All @@ -156,34 +173,39 @@ public function prepareResponse() : void
];
$this->response->setBody(json_encode($data));
}
foreach ($this->extraHeaders AS $header) {
$this->response->addHeader($header);
}
}

/**
* Determine whether Route supports request method.
* Add controller to route.
*
* @return bool
* @param string $URIControllerId
* @param JSONRestAPIController $controller
*/
public function checkRequestMethod() : bool
public function addController(string $URIControllerId, JSONRestAPIController $controller): void
{
foreach ($this->getAllowedRequestMethods() AS $method) {
if ($this->request->typeof($method)) {
return true;
break;
}
unset($method);
}
return false;
$this->controllerHashMap[$URIControllerId] = $controller;
}


/**
* Get collection of allowed request methods for this route URL. It is very important for handling HTTP405 error.
* Set extra headers for every request, e.g. ORIGIN headers, etc.
*
* @return array
* @param string[] $headers
*
* @throws \TypeError
*/
public function getAllowedRequestMethods() : array
public function setExtraHeaders(array $headers): void
{
return $this->allowedRequestMethods;
foreach ($headers AS $header) {
if (is_string($header)) {
$this->extraHeaders[] = $header;
}
else {
throw new \TypeError("Header must be s string value");
}
}
}

}

0 comments on commit 3281720

Please sign in to comment.