function __construct($sender = null)
{
if ($sender !== null) {
$this->sender = $sender;
}
if ($this->sender === null) {
// TODO 严格检查方法的参数数量
// 输出时按照下面数组中的顺序输出
$this->sender = array('header' => function ($_ = null) {
call_user_func_array('header', func_get_args());
}, 'status' => function ($var, $replace = true) {
header($_SERVER["SERVER_PROTOCOL"] . ' ' . $var, $replace);
}, 'cookie' => function ($name, $value, $expire = null, $path = '/', $domain = null, $secure = null) {
if (is_string($expire)) {
$expire = strtotime($expire);
}
setcookie($name, $value, $expire, $path, $domain, $secure);
}, 'body' => function ($var) {
if (is_array($var) || is_object($var)) {
header("Content-Type: application/json; charset=UTF-8");
echo json_encode($var);
// TODO 自定义适配方法
} else {
echo $var;
}
}, 'break' => function ($_ = null) {
//do nothing
}, 'res' => function ($status, $body) {
header($_SERVER["SERVER_PROTOCOL"] . ' ' . $status, true);
if (is_array($body)) {
header("Content-Type: application/json; charset=UTF-8");
echo json_encode($body);
// TODO 自定义适配方法
} else {
echo $body;
}
});
}
}