diff --git a/composer.json b/composer.json index baa24f2..1ea2953 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ }, "extra": { "think-config": { - "view": "src/config/view.php" + "template": "src/config/template.php" } } } \ No newline at end of file diff --git a/src/View.php b/src/View.php index 059ad1e..e0ab907 100644 --- a/src/View.php +++ b/src/View.php @@ -43,7 +43,7 @@ public function __construct(array $options = []) // 初始化模板引擎 $type = $options['type'] ?? 'think'; unset($options['type']); - $this->engine($type, $optons); + $this->engine($type, $options); return $this; } diff --git a/src/ViewController.php b/src/ViewController.php new file mode 100644 index 0000000..464023b --- /dev/null +++ b/src/ViewController.php @@ -0,0 +1,97 @@ + +// +---------------------------------------------------------------------- +declare (strict_types = 1); + +namespace think; + +use think\Controller; + +class ViewController extends Controller +{ + /** + * 视图类实例 + * @var \think\View + */ + protected $view; + + /** + * 构造方法 + * @access public + * @param App $app 应用对象 + */ + public function __construct(App $app) + { + parent::__construct($app); + $this->view = $this->app['view']; + } + + /** + * 加载模板输出 + * @access protected + * @param string $template 模板文件名 + * @return mixed + */ + protected function fetch(string $template = '') + { + return $this->view->fetch($template); + } + + /** + * 渲染内容输出 + * @access protected + * @param string $content 模板内容 + * @return mixed + */ + protected function display(string $content = '') + { + return $this->view->display($content); + } + + /** + * 模板变量赋值 + * @access protected + * @param array $vars 模板变量 + * @return $this + */ + protected function assign(array $vars) + { + $this->view->assign($vars); + + return $this; + } + + /** + * 视图过滤 + * @access protected + * @param Callable $filter 过滤方法或闭包 + * @return $this + */ + protected function filter(callable $filter) + { + $this->view->filter($filter); + + return $this; + } + + /** + * 初始化模板引擎 + * @access protected + * @param array|string $engine 引擎参数 + * @return $this + */ + protected function engine($engine) + { + $this->view->engine($engine); + + return $this; + } + +} diff --git a/src/config/template.php b/src/config/template.php new file mode 100644 index 0000000..299bd6f --- /dev/null +++ b/src/config/template.php @@ -0,0 +1,35 @@ + +// +---------------------------------------------------------------------- + +// +---------------------------------------------------------------------- +// | 模板设置 +// +---------------------------------------------------------------------- + +return [ + // 模板引擎类型 支持 php think 支持扩展 + 'type' => 'Think', + // 默认模板渲染规则 1 解析为小写+下划线 2 全部转换小写 3 保持操作方法 + 'auto_rule' => 1, + // 模板路径 + 'view_path' => '', + // 模板后缀 + 'view_suffix' => 'html', + // 模板文件名分隔符 + 'view_depr' => DIRECTORY_SEPARATOR, + // 模板引擎普通标签开始标记 + 'tpl_begin' => '{', + // 模板引擎普通标签结束标记 + 'tpl_end' => '}', + // 标签库标签开始标记 + 'taglib_begin' => '{', + // 标签库标签结束标记 + 'taglib_end' => '}', +]; diff --git a/src/response/Download.php b/src/response/Download.php deleted file mode 100644 index f3b75e2..0000000 --- a/src/response/Download.php +++ /dev/null @@ -1,137 +0,0 @@ - -// +---------------------------------------------------------------------- - -namespace think\response; - -use think\Exception; -use think\Response; - -class Download extends Response -{ - protected $expire = 360; - protected $name; - protected $mimeType; - protected $isContent = false; - - /** - * 处理数据 - * @access protected - * @param mixed $data 要处理的数据 - * @return mixed - * @throws \Exception - */ - protected function output($data) - { - if (!$this->isContent && !is_file($data)) { - throw new Exception('file not exists:' . $data); - } - - ob_end_clean(); - - if (!empty($this->name)) { - $name = $this->name; - } else { - $name = !$this->isContent ? pathinfo($data, PATHINFO_BASENAME) : ''; - } - - if ($this->isContent) { - $mimeType = $this->mimeType; - $size = strlen($data); - } else { - $mimeType = $this->getMimeType($data); - $size = filesize($data); - } - - $this->header['Pragma'] = 'public'; - $this->header['Content-Type'] = $mimeType ?: 'application/octet-stream'; - $this->header['Cache-control'] = 'max-age=' . $this->expire; - $this->header['Content-Disposition'] = 'attachment; filename="' . $name . '"'; - $this->header['Content-Length'] = $size; - $this->header['Content-Transfer-Encoding'] = 'binary'; - $this->header['Expires'] = gmdate("D, d M Y H:i:s", time() + $this->expire) . ' GMT'; - - $this->lastModified(gmdate('D, d M Y H:i:s', time()) . ' GMT'); - - $data = $this->isContent ? $data : file_get_contents($data); - return $data; - } - - /** - * 设置是否为内容 必须配合mimeType方法使用 - * @access public - * @param bool $content - * @return $this - */ - public function isContent(bool $content = true) - { - $this->isContent = $content; - return $this; - } - - /** - * 设置有效期 - * @access public - * @param integer $expire 有效期 - * @return $this - */ - public function expire(int $expire) - { - $this->expire = $expire; - return $this; - } - - /** - * 设置文件类型 - * @access public - * @param string $filename 文件名 - * @return $this - */ - public function mimeType(string $mimeType) - { - $this->mimeType = $mimeType; - return $this; - } - - /** - * 获取文件类型信息 - * @access public - * @param string $filename 文件名 - * @return string - */ - protected function getMimeType(string $filename): string - { - if (!empty($this->mimeType)) { - return $this->mimeType; - } - - $finfo = finfo_open(FILEINFO_MIME_TYPE); - - return finfo_file($finfo, $filename); - } - - /** - * 设置下载文件的显示名称 - * @access public - * @param string $filename 文件名 - * @param bool $extension 后缀自动识别 - * @return $this - */ - public function name(string $filename, bool $extension = true) - { - $this->name = $filename; - - if ($extension && false === strpos($filename, '.')) { - $this->name .= '.' . pathinfo($this->data, PATHINFO_EXTENSION); - } - - return $this; - } -} diff --git a/src/response/Jump.php b/src/response/Jump.php index f24ab2a..cd74322 100644 --- a/src/response/Jump.php +++ b/src/response/Jump.php @@ -27,9 +27,7 @@ class Jump extends Response */ protected function output($data): string { - $config = Container::pull('config'); return Container::pull('view') - ->init($config->pull('template')) ->assign($data) ->fetch($this->options['jump_template']); } diff --git a/src/response/View.php b/src/response/View.php index cd9bb44..236124c 100644 --- a/src/response/View.php +++ b/src/response/View.php @@ -31,9 +31,7 @@ class View extends Response protected function output($data): string { // 渲染模板输出 - $config = Container::pull('config'); return Container::pull('view') - ->init($config->pull('template')) ->filter($this->filter) ->assign($this->vars) ->fetch($data); @@ -87,9 +85,7 @@ public function filter(callable $filter = null) */ public function exists(string $name): bool { - return Container::pull('view') - ->init(Container::pull('config')->pull('template')) - ->exists($name); + return Container::pull('view')->exists($name); } } diff --git a/src/response/Xml.php b/src/response/Xml.php deleted file mode 100644 index c818076..0000000 --- a/src/response/Xml.php +++ /dev/null @@ -1,116 +0,0 @@ - -// +---------------------------------------------------------------------- - -namespace think\response; - -use think\Collection; -use think\Model; -use think\Response; - -class Xml extends Response -{ - // 输出参数 - protected $options = [ - // 根节点名 - 'root_node' => 'think', - // 根节点属性 - 'root_attr' => '', - //数字索引的子节点名 - 'item_node' => 'item', - // 数字索引子节点key转换的属性名 - 'item_key' => 'id', - // 数据编码 - 'encoding' => 'utf-8', - ]; - - protected $contentType = 'text/xml'; - - /** - * 处理数据 - * @access protected - * @param mixed $data 要处理的数据 - * @return mixed - */ - protected function output($data): string - { - if (is_string($data)) { - if (0 !== strpos($data, 'options['encoding']; - $xml = ""; - $data = $xml . $data; - } - return $data; - } - - // XML数据转换 - return $this->xmlEncode($data, $this->options['root_node'], $this->options['item_node'], $this->options['root_attr'], $this->options['item_key'], $this->options['encoding']); - } - - /** - * XML编码 - * @access protected - * @param mixed $data 数据 - * @param string $root 根节点名 - * @param string $item 数字索引的子节点名 - * @param string $attr 根节点属性 - * @param string $id 数字索引子节点key转换的属性名 - * @param string $encoding 数据编码 - * @return string - */ - protected function xmlEncode($data, string $root, string $item, string $attr, string $id, string $encoding): string - { - if (is_array($attr)) { - $array = []; - foreach ($attr as $key => $value) { - $array[] = "{$key}=\"{$value}\""; - } - $attr = implode(' ', $array); - } - - $attr = trim($attr); - $attr = empty($attr) ? '' : " {$attr}"; - $xml = ""; - $xml .= "<{$root}{$attr}>"; - $xml .= $this->dataToXml($data, $item, $id); - $xml .= ""; - - return $xml; - } - - /** - * 数据XML编码 - * @access protected - * @param mixed $data 数据 - * @param string $item 数字索引时的节点名称 - * @param string $id 数字索引key转换为的属性名 - * @return string - */ - protected function dataToXml($data, string $item, string $id): string - { - $xml = $attr = ''; - - if ($data instanceof Collection || $data instanceof Model) { - $data = $data->toArray(); - } - - foreach ($data as $key => $val) { - if (is_numeric($key)) { - $id && $attr = " {$id}=\"{$key}\""; - $key = $item; - } - $xml .= "<{$key}{$attr}>"; - $xml .= (is_array($val) || is_object($val)) ? $this->dataToXml($val, $item, $id) : $val; - $xml .= ""; - } - - return $xml; - } -}