function __construct($data = null, $url_begin = 0)
{
if ($data === null) {
$data = $GLOBALS;
$data['header'] = $this->getAllHeaders();
}
$this->url_begin = $url_begin;
//支持json请求(Content-Type: application/json)
$contentType = null;
if (isset($data['header']['Content-Type'])) {
$contentType = $data['header']['Content-Type'];
list($contentType, ) = explode(';', $contentType) + array(null, null);
if ($contentType == 'application/json') {
$post = file_get_contents('php://input');
if ($post != '') {
$post = json_decode($post, true);
Verify::isTrue(is_array($post), new BadRequest('post unjson data with application/json type'));
$data['_POST'] = $post;
if (!isset($data['_REQUEST'])) {
$data['_REQUEST'] = [];
}
$data['_REQUEST'] = array_merge($data['_POST'], $data['_REQUEST']);
}
} elseif ($contentType == 'text/plain') {
$data['_POST'] = file_get_contents('php://input');
}
}
//TODO: 支持put请求
if (isset($data['_SERVER']['REQUEST_METHOD']) && 'PUT' == $data['_SERVER']['REQUEST_METHOD']) {
if ($contentType == 'application/x-www-form-urlencoded') {
$queryString = file_get_contents('php://input');
$query = array();
parse_str($queryString, $query);
$data['_POST'] = $query;
}
}
$full_path = $data['_SERVER']['REQUEST_URI'];
Verify::isTrue($full_path, '$._SERVER.REQUEST_URI not found');
list($full_path, ) = explode('?', $full_path);
$paths = explode('/', $full_path);
$paths = array_filter($paths, function ($i) {
return $i !== '';
});
$paths = array_slice($paths, $this->url_begin);
$data['path'] = $paths;
$this->data = new JsonStore($data);
}