Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Updated requestHasContentType to use the match method of the Accept
Browse files Browse the repository at this point in the history
header object.

Change-Id: I7e43851c88692b5ea675393719f91d3a4ef379dd
  • Loading branch information
Jurgen Van de Moere committed Jan 3, 2013
1 parent 66ffac0 commit b69296a
Showing 1 changed file with 68 additions and 35 deletions.
103 changes: 68 additions & 35 deletions library/Zend/Mvc/Controller/AbstractRestfulController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Mvc
*/

namespace Zend\Mvc\Controller;

use Zend\Http\Request as HttpRequest;
Expand All @@ -26,33 +25,46 @@
*/
abstract class AbstractRestfulController extends AbstractController
{

const CONTENT_TYPE_JSON = 'json';

/**
* @var string
*/
protected $eventIdentifier = __CLASS__;

/**
* @var array
*/
protected $contentTypes = array(
self::CONTENT_TYPE_JSON => array(
'application/application/hal+json',
'application/json'
)
);

/**
* Return list of resources
*
* @return mixed
*/
abstract public function getList();
abstract public function getList ();

/**
* Return single resource
*
* @param mixed $id
* @return mixed
*/
abstract public function get($id);
abstract public function get ($id);

/**
* Create a new resource
*
* @param mixed $data
* @return mixed
*/
abstract public function create($data);
abstract public function create ($data);

/**
* Update an existing resource
Expand All @@ -61,26 +73,28 @@ abstract public function create($data);
* @param mixed $data
* @return mixed
*/
abstract public function update($id, $data);
abstract public function update ($id, $data);

/**
* Delete an existing resource
*
* @param mixed $id
* @return mixed
*/
abstract public function delete($id);
abstract public function delete ($id);

/**
* Basic functionality for when a page is not available
*
* @return array
*/
public function notFoundAction()
public function notFoundAction ()
{
$this->response->setStatusCode(404);

return array('content' => 'Page not found');

return array(
'content' => 'Page not found'
);
}

/**
Expand All @@ -96,12 +110,13 @@ public function notFoundAction()
* @return mixed|Response
* @throws Exception\InvalidArgumentException
*/
public function dispatch(Request $request, Response $response = null)
public function dispatch (Request $request, Response $response = null)
{
if (!$request instanceof HttpRequest) {
throw new Exception\InvalidArgumentException('Expected an HTTP request');
if (! $request instanceof HttpRequest) {
throw new Exception\InvalidArgumentException(
'Expected an HTTP request');
}

return parent::dispatch($request, $response);
}

Expand All @@ -112,23 +127,24 @@ public function dispatch(Request $request, Response $response = null)
* @return mixed
* @throws Exception\DomainException if no route matches in event or invalid HTTP method
*/
public function onDispatch(MvcEvent $e)
public function onDispatch (MvcEvent $e)
{
$routeMatch = $e->getRouteMatch();
if (!$routeMatch) {
if (! $routeMatch) {
/**
* @todo Determine requirements for when route match is missing.
* Potentially allow pulling directly from request metadata?
*/
throw new Exception\DomainException('Missing route matches; unsure how to retrieve action');
throw new Exception\DomainException(
'Missing route matches; unsure how to retrieve action');
}

$request = $e->getRequest();
$action = $routeMatch->getParam('action', false);
$action = $routeMatch->getParam('action', false);
if ($action) {
// Handle arbitrary methods, ending in Action
$method = static::getMethodFromAction($action);
if (!method_exists($this, $method)) {
if (! method_exists($this, $method)) {
$method = 'notFoundAction';
}
$return = $this->$method();
Expand Down Expand Up @@ -159,8 +175,9 @@ public function onDispatch(MvcEvent $e)
break;
case 'delete':
if (null === $id = $routeMatch->getParam('id')) {
if (!($id = $request->getQuery()->get('id', false))) {
throw new Exception\DomainException('Missing identifier');
if (! ($id = $request->getQuery()->get('id', false))) {
throw new Exception\DomainException(
'Missing identifier');
}
}
$action = 'delete';
Expand All @@ -169,15 +186,15 @@ public function onDispatch(MvcEvent $e)
default:
throw new Exception\DomainException('Invalid HTTP method!');
}

$routeMatch->setParam('action', $action);
}

// Emit post-dispatch signal, passing:
// - return from method, request, response
// If a listener returns a response object, return it immediately
$e->setResult($return);

return $return;
}

Expand All @@ -189,13 +206,12 @@ public function onDispatch(MvcEvent $e)
*/
public function processPostData (Request $request)
{
$contentType = strtolower( $request->getHeaders()->get('Content-Type')->getFieldValue());

if (strpos($contentType, 'application/json') !== false) {
if ($this->requestHasContentType($request, self::CONTENT_TYPE_JSON)) {
return $this->create(Json::decode($request->getContent()));
}

return $this->create($request->getPost()->toArray());

return $this->create($request->getPost()
->toArray());
}

/**
Expand All @@ -206,24 +222,41 @@ public function processPostData (Request $request)
* @return mixed
* @throws Exception\DomainException
*/
public function processPutData(Request $request, $routeMatch)
public function processPutData (Request $request, $routeMatch)
{
if (null === $id = $routeMatch->getParam('id')) {
if (!($id = $request->getQuery()->get('id', false))) {
if (! ($id = $request->getQuery()->get('id', false))) {
throw new Exception\DomainException('Missing identifier');
}
}

$contentType = strtolower( $request->getHeaders()->get('Content-Type')->getFieldValue());

if (strpos($contentType, 'application/json') !== false) {
if ($this->requestHasContentType($request, self::CONTENT_TYPE_JSON)) {
return $this->update($id, Json::decode($request->getContent()));
}

$content = $request->getContent();
parse_str($content, $parsedParams);

return $this->update($id, $parsedParams);
}

/**
* Check if request has certain content type
*
* @return boolean
*/
public function requestHasContentType (Request $request, $contentType = '')
{
$acceptHeaders = $request->getHeaders()->get('Accept');

if (array_key_exists($contentType, $this->contentTypes)) {
foreach ($this->contentTypes[$contentType] as $contentTypeValue) {
if ($acceptHeaders->match($contentTypeValue) !== false) {
return true;
}
}
}

return false;
}
}

0 comments on commit b69296a

Please sign in to comment.