Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
aec1b817
Commit
aec1b817
authored
Apr 08, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added `HtmlResponseFormatter` and `JsonResponseFormatter`
parent
f6eb5e69
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
144 additions
and
30 deletions
+144
-30
CHANGELOG.md
framework/CHANGELOG.md
+1
-0
HtmlResponseFormatter.php
framework/web/HtmlResponseFormatter.php
+39
-0
JsonResponseFormatter.php
framework/web/JsonResponseFormatter.php
+68
-0
Response.php
framework/web/Response.php
+31
-29
XmlResponseFormatter.php
framework/web/XmlResponseFormatter.php
+5
-1
No files found.
framework/CHANGELOG.md
View file @
aec1b817
...
...
@@ -295,6 +295,7 @@ Yii Framework 2 Change Log
-
New: Added
`yii\web\GroupUrlRule`
(qiangxue)
-
New: Added
`yii\filters\RateLimiter`
(qiangxue)
-
New: Added various authentication methods, including
`HttpBasicAuth`
,
`HttpBearerAuth`
,
`QueryParamAuth`
, and
`CompositeAuth`
(qiangxue)
-
New: Added
`HtmlResponseFormatter`
and
`JsonResponseFormatter`
(qiangxue)
2.
0.0-alpha, December 1, 2013
-----------------------------
...
...
framework/web/HtmlResponseFormatter.php
0 → 100644
View file @
aec1b817
<?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\Component
;
/**
* HtmlResponseFormatter formats the given data into an HTML response content.
*
* It is used by [[Response]] to format response data.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
HtmlResponseFormatter
extends
Component
implements
ResponseFormatterInterface
{
/**
* @var string the Content-Type header for the response
*/
public
$contentType
=
'text/html'
;
/**
* Formats the specified response.
* @param Response $response the response to be formatted.
*/
public
function
format
(
$response
)
{
if
(
stripos
(
$this
->
contentType
,
'charset'
)
===
false
)
{
$this
->
contentType
.=
'; charset='
.
$response
->
charset
;
}
$response
->
getHeaders
()
->
set
(
'Content-Type'
,
$this
->
contentType
);
$response
->
content
=
$response
->
data
;
}
}
framework/web/JsonResponseFormatter.php
0 → 100644
View file @
aec1b817
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\web
;
use
Yii
;
use
yii\base\Component
;
use
yii\helpers\Json
;
/**
* JsonResponseFormatter formats the given data into a JSON or JSONP response content.
*
* It is used by [[Response]] to format response data.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
JsonResponseFormatter
extends
Component
implements
ResponseFormatterInterface
{
/**
* @var boolean whether to use JSONP response format. When this is true, the [[Response::data|response data]]
* must be an array consisting of `data` and `callback` members. The latter should be a JavaScript
* function name while the former will be passed to this function as a parameter.
*/
public
$useJsonp
=
false
;
/**
* Formats the specified response.
* @param Response $response the response to be formatted.
*/
public
function
format
(
$response
)
{
if
(
$this
->
useJsonp
)
{
$this
->
formatJsonp
(
$response
);
}
else
{
$this
->
formatJson
(
$response
);
}
}
/**
* Formats response data in JSON format.
* @param Response $response
*/
protected
function
formatJson
(
$response
)
{
$response
->
getHeaders
()
->
set
(
'Content-Type'
,
'application/json; charset=UTF-8'
);
$response
->
content
=
Json
::
encode
(
$response
->
data
);
}
/**
* Formats response data in JSONP format.
* @param Response $response
*/
protected
function
formatJsonp
(
$response
)
{
$response
->
getHeaders
()
->
set
(
'Content-Type'
,
'application/javascript; charset=UTF-8'
);
if
(
is_array
(
$response
->
data
)
&&
isset
(
$response
->
data
[
'data'
],
$response
->
data
[
'callback'
]))
{
$response
->
content
=
sprintf
(
'%s(%s);'
,
$response
->
data
[
'callback'
],
Json
::
encode
(
$response
->
data
[
'data'
]));
}
else
{
$response
->
content
=
''
;
Yii
::
warning
(
"The 'jsonp' response requires that the data be an array consisting of both 'data' and 'callback' elements."
,
__METHOD__
);
}
}
}
framework/web/Response.php
View file @
aec1b817
...
...
@@ -12,7 +12,6 @@ use yii\base\InvalidConfigException;
use
yii\base\InvalidParamException
;
use
yii\helpers\Url
;
use
yii\helpers\FileHelper
;
use
yii\helpers\Json
;
use
yii\helpers\Security
;
use
yii\helpers\StringHelper
;
...
...
@@ -82,7 +81,8 @@ class Response extends \yii\base\Response
/**
* @var string the response format. This determines how to convert [[data]] into [[content]]
* when the latter is not set. By default, the following formats are supported:
* when the latter is not set. The value of this property must be one of the keys declared in the [[formatters] array.
* By default, the following formats are supported:
*
* - [[FORMAT_RAW]]: the data will be treated as the response content without any conversion.
* No extra HTTP header will be added.
...
...
@@ -102,12 +102,38 @@ class Response extends \yii\base\Response
*/
public
$format
=
self
::
FORMAT_HTML
;
/**
* @var array a list of supported response formats. The keys are MIME types (e.g. `application/json`)
* while the values are the corresponding formats (e.g. `html`, `json`) which must be supported by [[formatters]].
* When this property is set, a content type negotiation process will be conducted to determine
* the value of [[format]] and the corresponding [[mimeType]] and [[acceptParams]] values.
*/
public
$supportedFormats
;
/**
* @var string the MIME type (e.g. `application/json`) chosen for this response after content type negotiation.
* This property will be set by the content type negotiation process.
*/
public
$mimeType
;
/**
* @var array the parameters (e.g. `['q' => 1, 'version' => '1.0']`) for the MIME type chosen
* by the content type negotiation. This is a list of name-value pairs associated with [[mimeType]]
* from the ACCEPT HTTP header. This property will be set by the content type negotiation process.
*/
public
$acceptParams
;
/**
* @var array the formatters for converting data into the response content of the specified [[format]].
* The array keys are the format names, and the array values are the corresponding configurations
* for creating the formatter objects.
* @see format
*/
public
$formatters
;
public
$formatters
=
[
self
::
FORMAT_HTML
=>
'yii\web\HtmlResponseFormatter'
,
self
::
FORMAT_XML
=>
'yii\web\XmlResponseFormatter'
,
self
::
FORMAT_JSON
=>
'yii\web\JsonResponseFormatter'
,
self
::
FORMAT_JSONP
=>
[
'class'
=>
'yii\web\HtmlResponseFormatter'
,
'useJsonp'
=>
true
,
],
];
/**
* @var mixed the original response data. When this is not null, it will be converted into [[content]]
* according to [[format]] when the response is being sent out.
...
...
@@ -835,42 +861,18 @@ class Response extends \yii\base\Response
if
(
isset
(
$this
->
formatters
[
$this
->
format
]))
{
$formatter
=
$this
->
formatters
[
$this
->
format
];
if
(
!
is_object
(
$formatter
))
{
$formatter
=
Yii
::
createObject
(
$formatter
);
$
this
->
formatters
[
$this
->
format
]
=
$
formatter
=
Yii
::
createObject
(
$formatter
);
}
if
(
$formatter
instanceof
ResponseFormatterInterface
)
{
$formatter
->
format
(
$this
);
}
else
{
throw
new
InvalidConfigException
(
"The '
{
$this
->
format
}
' response formatter is invalid. It must implement the ResponseFormatterInterface."
);
}
}
else
{
switch
(
$this
->
format
)
{
case
self
::
FORMAT_HTML
:
$this
->
getHeaders
()
->
setDefault
(
'Content-Type'
,
'text/html; charset='
.
$this
->
charset
);
}
elseif
(
$this
->
format
===
self
::
FORMAT_RAW
)
{
$this
->
content
=
$this
->
data
;
break
;
case
self
::
FORMAT_RAW
:
$this
->
content
=
$this
->
data
;
break
;
case
self
::
FORMAT_JSON
:
$this
->
getHeaders
()
->
set
(
'Content-Type'
,
'application/json; charset=UTF-8'
);
$this
->
content
=
Json
::
encode
(
$this
->
data
);
break
;
case
self
::
FORMAT_JSONP
:
$this
->
getHeaders
()
->
set
(
'Content-Type'
,
'text/javascript; charset='
.
$this
->
charset
);
if
(
is_array
(
$this
->
data
)
&&
isset
(
$this
->
data
[
'data'
],
$this
->
data
[
'callback'
]))
{
$this
->
content
=
sprintf
(
'%s(%s);'
,
$this
->
data
[
'callback'
],
Json
::
encode
(
$this
->
data
[
'data'
]));
}
else
{
$this
->
content
=
''
;
Yii
::
warning
(
"The 'jsonp' response requires that the data be an array consisting of both 'data' and 'callback' elements."
,
__METHOD__
);
}
break
;
case
self
::
FORMAT_XML
:
Yii
::
createObject
(
XmlResponseFormatter
::
className
())
->
format
(
$this
);
break
;
default
:
throw
new
InvalidConfigException
(
"Unsupported response format:
{
$this
->
format
}
"
);
}
}
if
(
is_array
(
$this
->
content
))
{
throw
new
InvalidParamException
(
"Response content must not be an array."
);
...
...
framework/web/XmlResponseFormatter.php
View file @
aec1b817
...
...
@@ -51,8 +51,12 @@ class XmlResponseFormatter extends Component implements ResponseFormatterInterfa
*/
public
function
format
(
$response
)
{
$charset
=
$this
->
encoding
===
null
?
$response
->
charset
:
$this
->
encoding
;
if
(
stripos
(
$this
->
contentType
,
'charset'
)
===
false
)
{
$this
->
contentType
.=
'; charset='
.
$charset
;
}
$response
->
getHeaders
()
->
set
(
'Content-Type'
,
$this
->
contentType
);
$dom
=
new
DOMDocument
(
$this
->
version
,
$
this
->
encoding
===
null
?
$response
->
charset
:
$this
->
encoding
);
$dom
=
new
DOMDocument
(
$this
->
version
,
$
charset
);
$root
=
new
DOMElement
(
$this
->
rootTag
);
$dom
->
appendChild
(
$root
);
$this
->
buildXml
(
$root
,
$response
->
data
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment