Commit 3a75c69b by Paul Klimov

Merge branch 'master' of github.com:yiisoft/yii2

parents 1fd7dc66 65179b10
...@@ -73,6 +73,7 @@ Yii Framework 2 Change Log ...@@ -73,6 +73,7 @@ Yii Framework 2 Change Log
- Enh #1973: `yii message/extract` is now able to generate `.po` files (SergeiKutanov, samdark) - Enh #1973: `yii message/extract` is now able to generate `.po` files (SergeiKutanov, samdark)
- Enh #1984: ActionFilter will now mark event as handled when action run is aborted (cebe) - Enh #1984: ActionFilter will now mark event as handled when action run is aborted (cebe)
- Enh #2003: Added `filter` property to `ExistValidator` and `UniqueValidator` to support adding additional filtering conditions (qiangxue) - Enh #2003: Added `filter` property to `ExistValidator` and `UniqueValidator` to support adding additional filtering conditions (qiangxue)
- Enh #2043: Added support for custom request body parsers (danschmidt5189, cebe)
- Enh #2051: Do not save null data into database when using RBAC (qiangxue) - Enh #2051: Do not save null data into database when using RBAC (qiangxue)
- Enh: Added `favicon.ico` and `robots.txt` to default application templates (samdark) - Enh: Added `favicon.ico` and `robots.txt` to default application templates (samdark)
- Enh: Added `Widget::autoIdPrefix` to support prefixing automatically generated widget IDs (qiangxue) - Enh: Added `Widget::autoIdPrefix` to support prefixing automatically generated widget IDs (qiangxue)
......
...@@ -86,6 +86,8 @@ class Pagination extends Object ...@@ -86,6 +86,8 @@ class Pagination extends Object
* @var array parameters (name => value) that should be used to obtain the current page number * @var array parameters (name => value) that should be used to obtain the current page number
* and to create new pagination URLs. If not set, all parameters from $_GET will be used instead. * and to create new pagination URLs. If not set, all parameters from $_GET will be used instead.
* *
* In order to add hash to all links use `array_merge($_GET, ['#' => 'my-hash'])`.
*
* The array element indexed by [[pageVar]] is considered to be the current page number. * The array element indexed by [[pageVar]] is considered to be the current page number.
* If the element does not exist, the current page number is considered 0. * If the element does not exist, the current page number is considered 0.
*/ */
......
...@@ -168,6 +168,8 @@ class Sort extends Object ...@@ -168,6 +168,8 @@ class Sort extends Object
* @var array parameters (name => value) that should be used to obtain the current sort directions * @var array parameters (name => value) that should be used to obtain the current sort directions
* and to create new sort URLs. If not set, $_GET will be used instead. * and to create new sort URLs. If not set, $_GET will be used instead.
* *
* In order to add hash to all links use `array_merge($_GET, ['#' => 'my-hash'])`.
*
* The array element indexed by [[sortVar]] is considered to be the current sort directions. * The array element indexed by [[sortVar]] is considered to be the current sort directions.
* If the element does not exist, the [[defaults|default order]] will be used. * If the element does not exist, the [[defaults|default order]] will be used.
* *
......
...@@ -47,6 +47,7 @@ class ActiveFixture extends BaseActiveFixture ...@@ -47,6 +47,7 @@ class ActiveFixture extends BaseActiveFixture
*/ */
private $_table; private $_table;
/** /**
* @inheritdoc * @inheritdoc
*/ */
......
...@@ -28,6 +28,7 @@ abstract class DbFixture extends Fixture ...@@ -28,6 +28,7 @@ abstract class DbFixture extends Fixture
*/ */
public $db = 'db'; public $db = 'db';
/** /**
* @inheritdoc * @inheritdoc
*/ */
......
...@@ -35,6 +35,7 @@ class Fixture extends Component ...@@ -35,6 +35,7 @@ class Fixture extends Component
*/ */
public $depends = []; public $depends = [];
/** /**
* Loads the fixture. * Loads the fixture.
* This method is called before performing every test method. * This method is called before performing every test method.
......
...@@ -38,6 +38,7 @@ trait FixtureTrait ...@@ -38,6 +38,7 @@ trait FixtureTrait
*/ */
private $_fixtureAliases; private $_fixtureAliases;
/** /**
* Returns the value of an object property. * Returns the value of an object property.
* *
......
...@@ -34,7 +34,7 @@ class InitDbFixture extends DbFixture ...@@ -34,7 +34,7 @@ class InitDbFixture extends DbFixture
public $initScript = '@app/tests/fixtures/initdb.php'; public $initScript = '@app/tests/fixtures/initdb.php';
/** /**
* @var array list of database schemas that the test tables may reside in. Defaults to * @var array list of database schemas that the test tables may reside in. Defaults to
* [''], meaning using the default schema (an empty string refers to the * `['']`, meaning using the default schema (an empty string refers to the
* default schema). This property is mainly used when turning on and off integrity checks * default schema). This property is mainly used when turning on and off integrity checks
* so that fixture data can be populated into the database without causing problem. * so that fixture data can be populated into the database without causing problem.
*/ */
......
<?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 [[yii\helpers\Json::decode()]]
*
* @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 a [[BadRequestHttpException]] if the body is invalid json
*/
public $throwException = true;
/**
* Parses a HTTP request body.
* @param string $rawBody the raw HTTP request body.
* @param string $contentType the content type specified for the request body.
* @return array parameters parsed from the request body
* @throws BadRequestHttpException if the body contains invalid json and [[throwException]] is `true`.
*/
public function parse($rawBody, $contentType)
{
try {
return Json::decode($rawBody, $this->asArray);
} catch (InvalidParamException $e) {
if ($this->throwException) {
throw new BadRequestHttpException('Invalid JSON data in request body: ' . $e->getMessage(), 0, $e);
}
return null;
}
}
}
...@@ -128,6 +128,26 @@ class Request extends \yii\base\Request ...@@ -128,6 +128,26 @@ class Request extends \yii\base\Request
* @see getRestParams() * @see getRestParams()
*/ */
public $restVar = '_method'; public $restVar = '_method';
/**
* @var array the parsers for converting the raw HTTP request body into [[restParams]].
* The array keys are the request `Content-Types`, and the array values are the
* corresponding configurations for [[Yii::createObject|creating the parser objects]].
* A parser must implement the [[RequestParserInterface]].
*
* To enable parsing for JSON requests you can use the [[JsonParser]] class like in the following example:
*
* ```
* [
* 'application/json' => 'yii\web\JsonParser',
* ]
* ```
*
* To register a parser for parsing all request types you can use `'*'` as the array key.
* This one will be used as a fallback in case no other types match.
*
* @see getRestParams()
*/
public $parsers = [];
private $_cookies; private $_cookies;
...@@ -249,16 +269,33 @@ class Request extends \yii\base\Request ...@@ -249,16 +269,33 @@ class Request extends \yii\base\Request
/** /**
* Returns the request parameters for the RESTful request. * Returns the request parameters for the RESTful request.
*
* Request parameters are determined using the parsers configured in [[parsers]] property.
* If no parsers are configured for the current [[contentType]] it uses the PHP function [[mb_parse_str()]]
* to parse the [[rawBody|request body]].
* @return array the RESTful request parameters * @return array the RESTful request parameters
* @throws \yii\base\InvalidConfigException if a registered parser does not implement the [[RequestParserInterface]].
* @see getMethod() * @see getMethod()
*/ */
public function getRestParams() public function getRestParams()
{ {
if ($this->_restParams === null) { if ($this->_restParams === null) {
$contentType = $this->getContentType();
if (isset($_POST[$this->restVar])) { if (isset($_POST[$this->restVar])) {
$this->_restParams = $_POST; $this->_restParams = $_POST;
} elseif(strncmp($this->getContentType(), 'application/json', 16) === 0) { unset($this->_restParams[$this->restVar]);
$this->_restParams = Json::decode($this->getRawBody(), true); } elseif (isset($this->parsers[$contentType])) {
$parser = Yii::createObject($this->parsers[$contentType]);
if (!($parser instanceof RequestParserInterface)) {
throw new InvalidConfigException("The '$contentType' request parser is invalid. It must implement the yii\\web\\RequestParserInterface.");
}
$this->_restParams = $parser->parse($this->getRawBody(), $contentType);
} elseif (isset($this->parsers['*'])) {
$parser = Yii::createObject($this->parsers['*']);
if (!($parser instanceof RequestParserInterface)) {
throw new InvalidConfigException("The fallback request parser is invalid. It must implement the yii\\web\\RequestParserInterface.");
}
$this->_restParams = $parser->parse($this->getRawBody(), $contentType);
} else { } else {
$this->_restParams = []; $this->_restParams = [];
mb_parse_str($this->getRawBody(), $this->_restParams); mb_parse_str($this->getRawBody(), $this->_restParams);
...@@ -824,7 +861,7 @@ class Request extends \yii\base\Request ...@@ -824,7 +861,7 @@ class Request extends \yii\base\Request
} }
/** /**
* Returns request content-type * Returns request content-type
* The Content-Type header field indicates the MIME type of the data * The Content-Type header field indicates the MIME type of the data
* contained in [[getRawBody()]] or, in the case of the HEAD method, the * contained in [[getRawBody()]] or, in the case of the HEAD method, the
* media type that would have been sent had the request been a GET. * media type that would have been sent had the request been a GET.
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\web;
/**
* Interface for classes that parse the raw request body into a parameters array.
*
* @author Dan Schmidt <danschmidt5189@gmail.com>
* @since 2.0
*/
interface RequestParserInterface
{
/**
* Parses a HTTP request body.
* @param string $rawBody the raw HTTP request body.
* @param string $contentType the content type specified for the request body.
* @return array parameters parsed from the request body
*/
public function parse($rawBody, $contentType);
}
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