<?php<liu21st@gmail.com>declare (strict_types = 1);
namespace think;
abstract class Response
{
protected $data;
protected $contentType = 'text/html';
protected $charset = 'utf-8';
protected $code = 200;
protected $allowCache = true;
protected $options = [];
protected $header = [];
protected $content = null;
protected $cookie;
protected $session;
protected function init($data = '', int $code = 200)
{
$this->data($data);
$this->code = $code;
$this->contentType($this->contentType, $this->charset);
}
public static function create($data = '', string $type = 'html', int $code = 200): Response
{
$class = false !== strpos($type, '\\') ? $type : '\\think\\response\\' . ucfirst(strtolower($type));
return Container::getInstance()->invokeClass($class, [$data, $code]);
}
public function setSession(Session $session)
{
$this->session = $session;
return $this;
}
public function send(): void
{
$data = $this->getContent();
if (!headers_sent() && !empty($this->header)) {
http_response_code($this->code);
foreach ($this->header as $name => $val) {
header($name . (!is_null($val) ? ':' . $val : ''));
}
}
if ($this->cookie) {
$this->cookie->save();
}
$this->sendData($data);
if (function_exists('fastcgi_finish_request')) {
fastcgi_finish_request();
}
}
protected function output($data)
{
return $data;
}
protected function sendData(string $data): void
{
echo $data;
}
public function options(array $options = [])
{
$this->options = array_merge($this->options, $options);
return $this;
}
public function data($data)
{
$this->data = $data;
return $this;
}
public function allowCache(bool $cache)
{
$this->allowCache = $cache;
return $this;
}
public function isAllowCache()
{
return $this->allowCache;
}
public function cookie(string $name, string $value, $option = null)
{
$this->cookie->set($name, $value, $option);
return $this;
}
public function header(array $header = [])
{
$this->header = array_merge($this->header, $header);
return $this;
}
public function content($content)
{
if (null !== $content && !is_string($content) && !is_numeric($content) && !is_callable([
$content,
'__toString',
])
) {
throw new \InvalidArgumentException(sprintf('variable type error: %s', gettype($content)));
}
$this->content = (string) $content;
return $this;
}
public function code(int $code)
{
$this->code = $code;
return $this;
}
public function lastModified(string $time)
{
$this->header['Last-Modified'] = $time;
return $this;
}
public function expires(string $time)
{
$this->header['Expires'] = $time;
return $this;
}
public function eTag(string $eTag)
{
$this->header['ETag'] = $eTag;
return $this;
}
public function cacheControl(string $cache)
{
$this->header['Cache-control'] = $cache;
return $this;
}
public function contentType(string $contentType, string $charset = 'utf-8')
{
$this->header['Content-Type'] = $contentType . '; charset=' . $charset;
return $this;
}
public function getHeader(string $name = '')
{
if (!empty($name)) {
return $this->header[$name] ?? null;
}
return $this->header;
}
public function getData()
{
return $this->data;
}
public function getContent(): string
{
if (null == $this->content) {
$content = $this->output($this->data);
if (null !== $content && !is_string($content) && !is_numeric($content) && !is_callable([
$content,
'__toString',
])
) {
throw new \InvalidArgumentException(sprintf('variable type error: %s', gettype($content)));
}
$this->content = (string) $content;
}
return $this->content;
}
public function getCode(): int
{
return $this->code;
}
}