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

Commit

Permalink
Merge 1808073 into 0cb6770
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasvargiu committed Oct 15, 2018
2 parents 0cb6770 + 1808073 commit 4d037da
Show file tree
Hide file tree
Showing 29 changed files with 363 additions and 107 deletions.
5 changes: 5 additions & 0 deletions src/Controller/AbstractActionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public function notFoundAction()
{
$event = $this->getEvent();
$routeMatch = $event->getRouteMatch();

if (null === $routeMatch) {
throw new Exception\RuntimeException('Event does not have a RouteMatch');
}

$routeMatch->setParam('action', 'not-found');

$helper = $this->plugin('createHttpNotFoundModel');
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ abstract class AbstractController implements
protected $response;

/**
* @var Event
* @var MvcEvent
*/
protected $event;

Expand Down
118 changes: 93 additions & 25 deletions src/Controller/AbstractRestfulController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
*/
namespace Zend\Mvc\Controller;

use Zend\Http\AbstractMessage;
use Zend\Http\Request as HttpRequest;
use Zend\Http\Response as HttpResponse;
use Zend\Json\Json;
use Zend\Mvc\Exception;
use Zend\Mvc\MvcEvent;
use Zend\Router\RouteMatch;
use Zend\Stdlib\RequestInterface as Request;
use Zend\Stdlib\ResponseInterface as Response;

Expand Down Expand Up @@ -90,6 +93,24 @@ public function getIdentifierName()
return $this->identifierName;
}

/**
* @return HttpResponse
*/
private function getHttpResponse()
{
$response = $this->getResponse();

if (! $response instanceof HttpResponse) {
throw new Exception\UnexpectedValueException(sprintf(
'Response must be an instance of %s. %s given',
HttpResponse::class,
\is_object($response) ? \get_class($response) : \gettype($response)
));
}

return $response;
}

/**
* Create a new resource
*
Expand All @@ -98,7 +119,7 @@ public function getIdentifierName()
*/
public function create($data)
{
$this->response->setStatusCode(405);
$this->getHttpResponse()->setStatusCode(405);

return [
'content' => 'Method Not Allowed'
Expand All @@ -113,7 +134,7 @@ public function create($data)
*/
public function delete($id)
{
$this->response->setStatusCode(405);
$this->getHttpResponse()->setStatusCode(405);

return [
'content' => 'Method Not Allowed'
Expand All @@ -130,7 +151,7 @@ public function delete($id)
*/
public function deleteList($data)
{
$this->response->setStatusCode(405);
$this->getHttpResponse()->setStatusCode(405);

return [
'content' => 'Method Not Allowed'
Expand All @@ -145,7 +166,7 @@ public function deleteList($data)
*/
public function get($id)
{
$this->response->setStatusCode(405);
$this->getHttpResponse()->setStatusCode(405);

return [
'content' => 'Method Not Allowed'
Expand All @@ -159,7 +180,7 @@ public function get($id)
*/
public function getList()
{
$this->response->setStatusCode(405);
$this->getHttpResponse()->setStatusCode(405);

return [
'content' => 'Method Not Allowed'
Expand All @@ -177,7 +198,7 @@ public function getList()
*/
public function head($id = null)
{
$this->response->setStatusCode(405);
$this->getHttpResponse()->setStatusCode(405);

return [
'content' => 'Method Not Allowed'
Expand All @@ -197,7 +218,7 @@ public function head($id = null)
*/
public function options()
{
$this->response->setStatusCode(405);
$this->getHttpResponse()->setStatusCode(405);

return [
'content' => 'Method Not Allowed'
Expand All @@ -210,13 +231,13 @@ public function options()
* Not marked as abstract, as that would introduce a BC break
* (introduced in 2.1.0); instead, raises an exception if not implemented.
*
* @param $id
* @param $data
* @param mixed $id
* @param mixed $data
* @return array
*/
public function patch($id, $data)
{
$this->response->setStatusCode(405);
$this->getHttpResponse()->setStatusCode(405);

return [
'content' => 'Method Not Allowed'
Expand All @@ -234,7 +255,7 @@ public function patch($id, $data)
*/
public function replaceList($data)
{
$this->response->setStatusCode(405);
$this->getHttpResponse()->setStatusCode(405);

return [
'content' => 'Method Not Allowed'
Expand All @@ -252,7 +273,7 @@ public function replaceList($data)
*/
public function patchList($data)
{
$this->response->setStatusCode(405);
$this->getHttpResponse()->setStatusCode(405);

return [
'content' => 'Method Not Allowed'
Expand All @@ -268,7 +289,7 @@ public function patchList($data)
*/
public function update($id, $data)
{
$this->response->setStatusCode(405);
$this->getHttpResponse()->setStatusCode(405);

return [
'content' => 'Method Not Allowed'
Expand All @@ -282,7 +303,7 @@ public function update($id, $data)
*/
public function notFoundAction()
{
$this->response->setStatusCode(404);
$this->getHttpResponse()->setStatusCode(404);

return [
'content' => 'Page not found'
Expand Down Expand Up @@ -331,6 +352,23 @@ public function onDispatch(MvcEvent $e)
}

$request = $e->getRequest();
$response = $e->getResponse();

if (! $request instanceof HttpRequest) {
throw new Exception\UnexpectedValueException(sprintf(
'Request must be an instance of %s. %s given',
HttpRequest::class,
\is_object($request) ? \get_class($request) : \gettype($request)
));
}

if (! $response instanceof HttpResponse) {
throw new Exception\UnexpectedValueException(sprintf(
'Response must be an instance of %s. %s given',
HttpResponse::class,
\is_object($response) ? \get_class($response) : \gettype($response)
));
}

// Was an "action" requested?
$action = $routeMatch->getParam('action', false);
Expand All @@ -352,7 +390,7 @@ public function onDispatch(MvcEvent $e)
case (isset($this->customHttpMethodsMap[$method])):
$callable = $this->customHttpMethodsMap[$method];
$action = $method;
$return = call_user_func($callable, $e);
$return = $callable($e);
break;
// DELETE
case 'delete':
Expand Down Expand Up @@ -388,15 +426,15 @@ public function onDispatch(MvcEvent $e)
}
$action = 'head';
$headResult = $this->head($id);
$response = ($headResult instanceof Response) ? clone $headResult : $e->getResponse();
$response->setContent('');
$return = $response;
$resultResponse = ($headResult instanceof Response) ? clone $headResult : $response;
$resultResponse->setContent('');
$return = $resultResponse;
break;
// OPTIONS
case 'options':
$action = 'options';
$this->options();
$return = $e->getResponse();
$return = $response;
break;
// PATCH
case 'patch':
Expand All @@ -416,7 +454,6 @@ public function onDispatch(MvcEvent $e)
$action = 'patchList';
$return = $this->patchList($data);
} catch (Exception\RuntimeException $ex) {
$response = $e->getResponse();
$response->setStatusCode(405);
return $response;
}
Expand All @@ -442,7 +479,6 @@ public function onDispatch(MvcEvent $e)
break;
// All others...
default:
$response = $e->getResponse();
$response->setStatusCode(405);
return $response;
}
Expand All @@ -462,6 +498,14 @@ public function onDispatch(MvcEvent $e)
*/
public function processPostData(Request $request)
{
if (! $request instanceof HttpRequest) {
throw new Exception\InvalidArgumentException(sprintf(
'Request must be an instance of %s. %s given',
HttpRequest::class,
\is_object($request) ? \get_class($request) : \gettype($request)
));
}

if ($this->requestHasContentType($request, self::CONTENT_TYPE_JSON)) {
return $this->create($this->jsonDecode($request->getContent()));
}
Expand All @@ -478,7 +522,15 @@ public function processPostData(Request $request)
*/
public function requestHasContentType(Request $request, $contentType = '')
{
/** @var $headerContentType \Zend\Http\Header\ContentType */
if (! $request instanceof HttpRequest) {
throw new Exception\InvalidArgumentException(sprintf(
'Request must be an instance of %s. %s given',
HttpRequest::class,
\is_object($request) ? \get_class($request) : \gettype($request)
));
}

/** @var \Zend\Http\Header\ContentType $headerContentType */
$headerContentType = $request->getHeaders()->get('content-type');
if (! $headerContentType) {
return false;
Expand Down Expand Up @@ -546,12 +598,28 @@ public function addHttpMethodHandler($method, /* Callable */ $handler)
* Attempts to see if an identifier was passed in either the URI or the
* query string, returning it if found. Otherwise, returns a boolean false.
*
* @param \Zend\Router\RouteMatch $routeMatch
* @param Request $request
* @param RouteMatch $routeMatch
* @param HttpRequest $request
* @return false|mixed
*/
protected function getIdentifier($routeMatch, $request)
{
if (! $routeMatch instanceof RouteMatch) {
throw new Exception\InvalidArgumentException(sprintf(
'RouteMatch must be an instance of %s. %s given',
RouteMatch::class,
\is_object($routeMatch) ? \get_class($routeMatch) : \gettype($routeMatch)
));
}

if (! $request instanceof HttpRequest) {
throw new Exception\InvalidArgumentException(sprintf(
'Request must be an instance of %s. %s given',
HttpRequest::class,
\is_object($request) ? \get_class($request) : \gettype($request)
));
}

$identifier = $this->getIdentifierName();
$id = $routeMatch->getParam($identifier, false);
if ($id !== false) {
Expand Down Expand Up @@ -611,7 +679,7 @@ protected function processBodyContent($request)
*
* Marked protected to allow usage from extending classes.
*
* @param string
* @param string $string
* @return mixed
* @throws Exception\DomainException if no JSON decoding functionality is
* available.
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/ControllerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ControllerManager extends AbstractPluginManager
* Injects an initializer for injecting controllers with an
* event manager and plugin manager.
*
* @param ConfigInterface|ContainerInterface $container
* @param ConfigInterface|ContainerInterface $configOrContainerInstance
* @param array $config
*/
public function __construct($configOrContainerInstance, array $config = [])
Expand Down
Loading

0 comments on commit 4d037da

Please sign in to comment.