首页 > Web开发 > 详细

[李景山php]每天TP5-20170127|thinkphp5-View.php

时间:2016-12-29 11:30:28      阅读:215      评论:0      收藏:0      [点我收藏+]
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace think;

class View
{// 神秘的视图类,我来了
    // 视图实例
    protected static $instance;// 实例化
    // 模板引擎实例
    public $engine;// 引擎
    // 模板变量
    protected $data = [];// 变量
    // 视图输出替换
    protected $replace = [];// 视图 输出

    /**
     * 架构函数
     * @access public
     * @param array $engine  模板引擎参数
     * @param array $replace  字符串替换参数
     */
    public function __construct($engine = [], $replace = [])
    {// 构造函数
        // 初始化模板引擎
        $this->engine((array) $engine);// 设置引擎
        $this->replace = $replace;// 设置替换
    }

    /**
     * 初始化视图
     * @access public
     * @param array $engine  模板引擎参数
     * @param array $replace  字符串替换参数
     * @return object
     */
    public static function instance($engine = [], $replace = [])
    {
        if (is_null(self::$instance)) {// 初始化方案 标准的单例模式
            self::$instance = new self($engine, $replace);
        }
        return self::$instance;
    }

    /**
     * 模板变量赋值
     * @access public
     * @param mixed $name  变量名
     * @param mixed $value 变量值
     * @return $this
     */
    public function assign($name, $value = ‘‘)
    {
        if (is_array($name)) {// 如果是 数组
            $this->data = array_merge($this->data, $name);// 拼合全部数组
        } else {
            $this->data[$name] = $value;// 否则 就是数组 放到对应的数据里面
        }
        return $this;
    }

    /**
     * 设置当前模板解析的引擎
     * @access public
     * @param array|string $options 引擎参数
     * @return $this
     */
    public function engine($options = [])// 设置模版引擎
    {
        if (is_string($options)) {// 如果是字符串
            $type    = $options;// 转存一下先
            $options = [];
        } else {
            $type = !empty($options[‘type‘]) ? $options[‘type‘] : ‘Think‘;// 类型 想获取
        }

        $class = false !== strpos($type, ‘\\‘) ? $type : ‘\\think\\view\\driver\\‘ . ucfirst($type);
        if (isset($options[‘type‘])) {
            unset($options[‘type‘]);
        }// 获取类名
        $this->engine = new $class($options);// 设置引擎,就是 new 相应的类
        return $this;
    }

    /**
     * 配置模板引擎
     * @access private
     * @param string|array  $name 参数名
     * @param mixed         $value 参数值
     * @return void
     */
    public function config($name, $value = null)
    {// 设置模版引擎里面的配置项
        $this->engine->config($name, $value);
        return $this;
    }

    /**
     * 解析和获取模板内容 用于输出
     * @param string    $template 模板文件名或者内容
     * @param array     $vars     模板输出变量
     * @param array     $replace 替换内容
     * @param array     $config     模板参数
     * @param bool      $renderContent     是否渲染内容
     * @return string
     * @throws Exception
     */
    public function fetch($template = ‘‘, $vars = [], $replace = [], $config = [], $renderContent = false)
    {// 解析和获取模版内容,这个太重要了,太重要了
        // 模板变量
        $vars = array_merge($this->data, $vars);// 变量

        // 页面缓存
        ob_start();// 开启 缓存
        ob_implicit_flush(0);// 打开绝对刷新

        // 渲染输出
        $method = $renderContent ? ‘display‘ : ‘fetch‘; // 是否渲染内容
        $this->engine->$method($template, $vars, $config);// 调用相应引擎里面的不同的方法

        // 获取并清空缓存
        $content = ob_get_clean();// 获取内容
        // 内容过滤标签
        Hook::listen(‘view_filter‘, $content);// 启用,内容过滤标签
        // 允许用户自定义模板的字符串替换
        $replace = array_merge($this->replace, $replace);// 进行替换
        if (!empty($replace)) {// 不为空
            $content = strtr($content, $replace);// 执行内容替换
        }
        return $content;// 返回内容
        // 总结 就是 个 模版转内容
    }

    /**
     * 视图内容替换
     * @access public
     * @param string|array  $content 被替换内容(支持批量替换)
     * @param string        $replace    替换内容
     * @return $this
     */
    public function replace($content, $replace = ‘‘)
    {// 设置 替换 内容
        if (is_array($content)) {
            $this->replace = array_merge($this->replace, $content);
        } else {
            $this->replace[$content] = $replace;
        }
        return $this;
    }

    /**
     * 渲染内容输出
     * @access public
     * @param string $content 内容
     * @param array  $vars    模板输出变量
     * @param array  $replace 替换内容
     * @param array  $config     模板参数
     * @return mixed
     */
    public function display($content, $vars = [], $replace = [], $config = [])
    {// 渲染 内容输出
        return $this->fetch($content, $vars, $replace, $config, true);
    }

    /**
     * 模板变量赋值
     * @access public
     * @param string    $name  变量名
     * @param mixed     $value 变量值
     */
    public function __set($name, $value)
    {// 设置 ,变成了 值设置
        $this->data[$name] = $value;
    }

    /**
     * 取得模板显示变量的值
     * @access protected
     * @param string $name 模板变量
     * @return mixed
     */
    public function __get($name)
    {// 获取值
        return $this->data[$name];
    }

    /**
     * 检测模板变量是否设置
     * @access public
     * @param string $name 模板变量名
     * @return bool
     */
    public function __isset($name)
    {// 是否设置
        return isset($this->data[$name]);
    }
}


本文出自 “专注php 群号:414194301” 博客,请务必保留此出处http://jingshanls.blog.51cto.com/3357095/1887147

[李景山php]每天TP5-20170127|thinkphp5-View.php

原文:http://jingshanls.blog.51cto.com/3357095/1887147

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!