Commit 8a94eb1f by Carsten Brandt

Merge PR #2066 branch '2043-add-support-for-custom-request-parsers' of…

Merge PR #2066 branch '2043-add-support-for-custom-request-parsers' of https://github.com/danschmidt5189/yii2 * '2043-add-support-for-custom-request-parsers' of https://github.com/danschmidt5189/yii2: Fixed type in JsonParser Tabs vs. spaces Added support for custom web\Request parsers (#2043) Adds customizable parsing functionality to the web\Request class (Issue #2043)
parents b44d0eb6 7c98ef59
...@@ -4,6 +4,7 @@ Yii Framework 2 Change Log ...@@ -4,6 +4,7 @@ Yii Framework 2 Change Log
2.0.0 beta under development 2.0.0 beta under development
---------------------------- ----------------------------
- Enh #2043: Added support for custom web\Request parsers (danschmidt5189)
- Bug #1326: The `visible` setting for `DetailView` doesn't work as expected (qiangxue) - Bug #1326: The `visible` setting for `DetailView` doesn't work as expected (qiangxue)
- Bug #1446: Logging while logs are processed causes infinite loop (qiangxue) - Bug #1446: Logging while logs are processed causes infinite loop (qiangxue)
- Bug #1497: Localized view files are not correctly returned (mintao) - Bug #1497: Localized view files are not correctly returned (mintao)
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\web;
use yii\base\InvalidParamException;
use yii\helpers\Json;
/**
* Parses a raw HTTP request using Json::encode()
*
* @author Dan Schmidt <danschmidt5189@gmail.com>
* @since 2.0
*/
class JsonParser implements RequestParserInterface
{
/**
* @var boolean whether to return objects in terms of associative arrays.
*/
public $asArray = true;
/**
* @var boolean whether to throw an exception if the body is invalid json
*/
public $throwException = false;
/**
* @param string $rawBody the raw HTTP request body
* @return array parameters parsed from the request body
*/
public function parse($rawBody)
{
try {
return Json::decode($rawBody, $this->asArray);
} catch (InvalidParamException $e) {
if ($this->throwException) {
throw $e;
}
return null;
}
}
}
...@@ -128,6 +128,13 @@ class Request extends \yii\base\Request ...@@ -128,6 +128,13 @@ class Request extends \yii\base\Request
* @see getRestParams() * @see getRestParams()
*/ */
public $restVar = '_method'; public $restVar = '_method';
/**
* @var array the parsers for converting the raw request body into [[restParams]]
* The array keys are the request content-types, and the array values are the
* corresponding configurations for creating the parser objects.
* @see getRestParams()
*/
public $parsers = [];
private $_cookies; private $_cookies;
...@@ -257,8 +264,12 @@ class Request extends \yii\base\Request ...@@ -257,8 +264,12 @@ class Request extends \yii\base\Request
if ($this->_restParams === null) { if ($this->_restParams === null) {
if (isset($_POST[$this->restVar])) { if (isset($_POST[$this->restVar])) {
$this->_restParams = $_POST; $this->_restParams = $_POST;
} elseif(strncmp($this->getContentType(), 'application/json', 16) === 0) { } else if (isset($this->parsers[$this->getContentType()])) {
$this->_restParams = Json::decode($this->getRawBody(), true); $parser = Yii::createObject($this->parsers[$this->getContentType()]);
if (! $parser instanceof RequestParserInterface) {
throw new InvalidConfigException("The '{$this->contentType}' request parser is invalid. It must implement the RequestParserInterface.");
}
$this->_restParams = $parser->parse($this->getRawBody());
} else { } else {
$this->_restParams = []; $this->_restParams = [];
mb_parse_str($this->getRawBody(), $this->_restParams); mb_parse_str($this->getRawBody(), $this->_restParams);
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\web;
/**
* Interface for objects that parse the raw request body into a parameters array
*
* @author Dan Schmidt <danschmidt5189@gmail.com>
* @since 2.0
*/
interface RequestParserInterface
{
/**
* @param string $rawBody the raw HTTP request body
* @return array parameters parsed from the request body
*/
public function parse($rawBody);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment