Skip to content

Commit

Permalink
Merge pull request #49 from bravo-kernel/cake36
Browse files Browse the repository at this point in the history
Upgrade to Cake 3.6+
  • Loading branch information
bravo-kernel committed May 15, 2018
2 parents 447760f + 1371033 commit 0dd6c60
Show file tree
Hide file tree
Showing 23 changed files with 342 additions and 606 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ before_script:

- if [[ $DB = 'pgsql' ]]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi

- if [[ $PHPCS = 1 ]]; then composer require cakephp/cakephp-codesniffer:"2.*"; fi
- if [[ $PHPCS = 1 ]]; then composer require cakephp/cakephp-codesniffer:"^3.0"; fi

script:
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.0 ]]; then vendor/bin/phpunit; fi
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
}
],
"require":{
"cakephp/cakephp": "<3.6.0",
"cakephp/cakephp": "^3.6",
"friendsofcake/crud": "*",
"neomerx/json-api": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "<6.0",
"phpunit/phpunit": "^5.7.14|^6.0",
"friendsofcake/cakephp-test-utilities": "dev-master",
"friendsofcake/search": "^4.2"
"friendsofcake/search": "^4.4"
},
"autoload": {
"psr-4": {
Expand Down
34 changes: 23 additions & 11 deletions src/Error/JsonApiExceptionRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Neomerx\JsonApi\Document\Error;
use Neomerx\JsonApi\Encoder\Encoder;
use Neomerx\JsonApi\Exceptions\ErrorCollection;
use Zend\Diactoros\Stream;

/**
* Exception renderer for the JsonApiListener
Expand All @@ -24,7 +25,7 @@ class JsonApiExceptionRenderer extends ExceptionRenderer
* Method used for all non-validation errors.
*
* @param string $template Name of template to use (ignored for jsonapi)
* @return \Cake\Network\Response
* @return \Cake\Http\Response
*/
protected function _outputMessage($template)
{
Expand All @@ -35,7 +36,8 @@ protected function _outputMessage($template)
$viewVars = $this->controller->viewVars;

$code = $viewVars['code']; // e.g. 404
$title = $this->controller->response->httpCodes($code)[$code]; // e,g. Not Found

$title = $this->controller->response->getReasonPhrase($code); // e,g. Not Found

// Only set JSON API `detail` field if `message` viewVar field is not
// identical to the CakePHP HTTP Status Code description.
Expand Down Expand Up @@ -64,9 +66,14 @@ protected function _outputMessage($template)
$json = $this->_addQueryLogsNode($json);
}

// send response
$this->controller->response->type('jsonapi');
$this->controller->response->body($json);
# create stream as required by `response->withBody()`
$stream = new Stream('php://memory', 'wb+');
$stream->write($json);

// set up the response
$this->controller->response = $this->controller->response
->withType('jsonapi')
->withBody($stream);

return $this->controller->response;
}
Expand All @@ -76,7 +83,7 @@ protected function _outputMessage($template)
* validation errors and JSON API (request data) documents.
*
* @param \Crud\Error\Exception\ValidationException $exception Exception
* @return \Cake\Network\Response
* @return \Cake\Http\Response
*/
public function validation($exception)
{
Expand All @@ -87,10 +94,10 @@ public function validation($exception)
$status = $exception->getCode();

try {
$this->controller->response->statusCode($status);
$this->controller->response = $this->controller->response->withStatus($status);
} catch (Exception $e) {
$status = 422;
$this->controller->response->statusCode($status);
$this->controller->response = $this->controller->response->withStatus($status);
}

$errorCollection = $this->_getNeoMerxErrorCollection($exception->getValidationErrors());
Expand All @@ -103,9 +110,14 @@ public function validation($exception)
$json = $this->_addQueryLogsNode($json);
}

// set data and send response
$this->controller->response->type('jsonapi');
$this->controller->response->body($json);
# create stream as required by `response->withBody()`
$stream = new Stream('php://memory', 'wb+');
$stream->write($json);

// set up the response
$this->controller->response = $this->controller->response
->withType('jsonapi')
->withBody($stream);

return $this->controller->response;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Listener/JsonApi/DocumentValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,10 @@ protected function _getErrorCollectionEntity()
{
$entity = new Entity();

$entity->errors('CrudJsonApiListener', [
'NeoMerxErrorCollection' => $this->_errorCollection
$entity->setErrors([
'CrudJsonApiListener' => [
'NeoMerxErrorCollection' => $this->_errorCollection
]
]);

return $entity;
Expand Down

0 comments on commit 0dd6c60

Please sign in to comment.