<?php
namespace Yesf\Http;
use Yesf\Yesf;
use Yesf\Config;
use Yesf\DI\Container;
use Yesf\Http\Vars as HttpVars;
use Yesf\Exception\InvalidClassException;
class Response {
protected static $tpl_auto_config = false;
protected static $tpl_extension = 'phtml';
protected static $tpl_engine = null;
protected $tpl_path;
protected $sw_response = null;
protected $tpl_auto = null;
protected $tpl_default = '';
protected $tpl_engine_obj = null;
protected $is_end = false;
protected static $cookie = [
'expire' => -1,
'path' => '/',
'domain' => ''
];
public static function init() {
$view_config = Yesf::app()->getConfig('view', Yesf::CONF_PROJECT);
self::$tpl_auto_config = ($view_config['auto'] == 1) ? true : false;
self::$tpl_extension = ($view_config['extension'] ? $view_config['extension'] : 'phtml');
self::$tpl_engine = Template::class;
Container::getInstance()->setMulti(Template::class, Container::MULTI_CLONE);
}
public static function initInWorker() {
if (Yesf::app()->getConfig('cookie.expire')) {
self::$cookie['expire'] = Yesf::app()->getConfig('cookie.expire');
}
if (Yesf::app()->getConfig('cookie.path')) {
self::$cookie['path'] = Yesf::app()->getConfig('cookie.path');
}
if (Yesf::app()->getConfig('cookie.domain')) {
self::$cookie['domain'] = Yesf::app()->getConfig('cookie.domain');
}
}
public static function setTemplateEngine(string $id) {
Container::getInstance()->setMulti($id, Container::MULTI_CLONE);
$clazz = Container::getInstance()->get($id);
if (!is_subclass_of($clazz, __NAMESPACE__ . '\\TemplateInterface')) {
throw new InvalidClassException("$clazz not implemented TemplateInterface");
}
self::$tpl_engine = $classId;
}
public function setCurrentTemplateEngine(string $id) {
Container::getInstance()->setMulti($id, Container::MULTI_CLONE);
$clazz = Container::getInstance()->get($id);
if (!is_subclass_of($clazz, __NAMESPACE__ . '\\TemplateInterface')) {
throw new InvalidClassException("$clazz not implemented TemplateInterface");
}
$this->tpl_engine_obj = $clazz;
}
public function __construct($response) {
$this->sw_response = $response;
$this->tpl_path = APP_PATH . 'View/';
$this->tpl_engine_obj = Container::getInstance()->get(self::$tpl_engine);
}
public function setTemplatePath($path) {
$this->tpl_path = $path;
}
public function setTemplate($name) {
$this->tpl_default = $name;
}
public function disableView() {
$this->tpl_auto = false;
}
public function display($tpl = null, $is_abs_path = false) {
if ($tpl === null) {
$tpl = $this->tpl_default;
}
$data = $this->render($tpl, $is_abs_path);
if (!empty($data)) $this->write($data);
}
public function getTemplate() {
return $this->tpl_engine_obj;
}
public function render($tpl, $is_abs_path = false) {
if ($is_abs_path) {
$_tpl_full_path = $tpl;
} else {
$_tpl_full_path = $this->tpl_path . $tpl . '.' . self::$tpl_extension;
}
return $this->tpl_engine_obj->render($_tpl_full_path);
}
public function assign($k, $v) {
$this->tpl_engine_obj->assign($k, $v);
}
public function clearAssign() {
$this->tpl_engine_obj->clearAssign();
}
public function write($content) {
$this->sw_response->write($content);
}
public function sendfile($filepath, $offset, $length) {
$this->sw_response->sendfile($filepath, $offset, $length);
$this->is_end = true;
$this->sw_response = null;
$this->tpl_engine_obj = null;
}
public function header($k, $v) {
$this->sw_response->header($k, $v);
}
public function status($code) {
$this->sw_response->status($code);
}
public function cookie($param) {
$name = $param['name'];
if (!isset($param['expire'])) {
$expire = self::$cookie['expire'] === -1 ? 0 : time() + self::$cookie['expire'];
} elseif ($param['expire'] === -1) {
$expire = time() - 3600;
} elseif ($param['expire'] === 0) {
$expire = 0;
} else {
$expire = time() + $param['expire'];
}
!isset($param['path']) && $param['path'] = self::$cookie['path'];
!isset($param['domain']) && $param['domain'] = self::$cookie['domain'];
!isset($param['httponly']) && $param['httponly'] = false;
if (!isset($param['https'])) {
$param['https'] = false;
}
$this->sw_response->cookie($name, $param['value'], $expire, $param['path'], $param['domain'], $param['https'], $param['httponly']);
}
public function mimeType($extension) {
$this->header('Content-Type', HttpVars::mimeType($extension));
}
public function end() {
if ($this->is_end) {
return;
}
$this->is_end = true;
if ($this->sw_response) {
if (($this->tpl_auto === null && self::$tpl_auto_config) || $this->tpl_auto) {
$this->display();
}
$this->sw_response->end();
$this->sw_response = null;
}
$this->tpl_engine_obj = null;
}
public function __destruct() {
$this->end();
}
}