Add support for OPTIONS and generate Allow header from Handler. Close #4

This commit is contained in:
PJ Dietz 2013-05-27 14:13:16 -04:00
parent fafce16e9e
commit e693e138d0
1 changed files with 36 additions and 17 deletions

View File

@ -101,35 +101,29 @@ abstract class Handler implements HandlerInterface
protected function buildResponse() protected function buildResponse()
{ {
switch ($this->request->getMethod()) { switch ($this->request->getMethod()) {
case 'GET': case 'GET':
$this->get(); $this->get();
break; break;
case 'HEAD': case 'HEAD':
$this->head(); $this->head();
break; break;
case 'POST': case 'POST':
$this->post(); $this->post();
break; break;
case 'PUT': case 'PUT':
$this->put(); $this->put();
break; break;
case 'DELETE': case 'DELETE':
$this->delete(); $this->delete();
break; break;
case 'PATCH': case 'PATCH':
$this->patch(); $this->patch();
break; break;
case 'OPTIONS': case 'OPTIONS':
$this->options(); $this->options();
break; break;
default:
$this->respondWithMethodNotAllowed();
} }
} }
@ -152,14 +146,6 @@ abstract class Handler implements HandlerInterface
$this->respondWithMethodNotAllowed(); $this->respondWithMethodNotAllowed();
} }
/**
* Provide a default response for unsupported methods.
*/
protected function respondWithMethodNotAllowed()
{
$this->response->setStatusCode(405);
}
/** /**
* Method for handling HTTP HEAD requests. * Method for handling HTTP HEAD requests.
* *
@ -224,7 +210,40 @@ abstract class Handler implements HandlerInterface
*/ */
protected function options() protected function options()
{ {
$this->respondWithMethodNotAllowed(); if ($this->addAllowHeader()) {
$this->response->setStatusCode(200);
} else {
$this->response->setStatusCode(405);
}
}
/**
* Provide a default response for unsupported methods.
*/
protected function respondWithMethodNotAllowed()
{
$this->response->setStatusCode(405);
$this->addAllowHeader();
}
/** @return array of method names supported by the handler. */
protected function getAllowedMethods()
{
}
/**
* Add an Allow: header using the methods returned by getAllowedMethods()
*
* @return bool The header was added.
*/
protected function addAllowHeader()
{
$methods = $this->getAllowedMethods();
if ($methods) {
$this->response->setHeader('Allow', join($methods, ', '));
return true;
}
return false;
} }
} }