Completed
Push — 6.0 ( 954786...6b998a )
by yun
01:50
created

View   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Test Coverage

Coverage 90.48%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 168
ccs 38
cts 42
cp 0.9048
rs 10
wmc 15

11 Methods

Rating   Name   Duplication   Size   Complexity  
A filter() 0 4 1
A display() 0 4 1
A fetch() 0 4 1
A engine() 0 3 1
A assign() 0 9 2
A resolveConfig() 0 5 1
A getContent() 0 26 4
A __isset() 0 3 1
A __get() 0 3 1
A getDefaultDriver() 0 3 1
A __set() 0 3 1
1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think;
14
15
use think\helper\Arr;
16
17
/**
18
 * 视图类
19
 * @package think
20
 */
21
class View extends Manager
22
{
23
24
    protected $namespace = '\\think\\view\\driver\\';
25
26
    /**
27
     * 模板变量
28
     * @var array
29
     */
30
    protected $data = [];
31
32
    /**
33
     * 内容过滤
34
     * @var mixed
35
     */
36
    protected $filter;
37
38
    /**
39
     * 获取模板引擎
40
     * @access public
41
     * @param string $type 模板引擎类型
42
     * @return $this
43
     */
44 3
    public function engine(string $type = null)
45
    {
46 3
        return $this->driver($type);
47
    }
48
49
    /**
50
     * 模板变量赋值
51
     * @access public
52
     * @param string|array $name  模板变量
53
     * @param mixed        $value 变量值
54
     * @return $this
55
     */
56 3
    public function assign($name, $value = null)
57
    {
58 3
        if (is_array($name)) {
59 3
            $this->data = array_merge($this->data, $name);
60
        } else {
61 3
            $this->data[$name] = $value;
62
        }
63
64 3
        return $this;
65
    }
66
67
    /**
68
     * 视图过滤
69
     * @access public
70
     * @param Callable $filter 过滤方法或闭包
71
     * @return $this
72
     */
73 3
    public function filter(callable $filter = null)
74
    {
75 3
        $this->filter = $filter;
76 3
        return $this;
77
    }
78
79
    /**
80
     * 解析和获取模板内容 用于输出
81
     * @access public
82
     * @param string $template 模板文件名或者内容
83
     * @param array  $vars     模板变量
84
     * @return string
85
     * @throws \Exception
86
     */
87 3
    public function fetch(string $template = '', array $vars = []): string
88
    {
89
        return $this->getContent(function () use ($vars, $template) {
90 3
            $this->engine()->fetch($template, array_merge($this->data, $vars));
91 3
        });
92
    }
93
94
    /**
95
     * 渲染内容输出
96
     * @access public
97
     * @param string $content 内容
98
     * @param array  $vars    模板变量
99
     * @return string
100
     */
101 3
    public function display(string $content, array $vars = []): string
102
    {
103
        return $this->getContent(function () use ($vars, $content) {
104 3
            $this->engine()->display($content, array_merge($this->data, $vars));
105 3
        });
106
    }
107
108
    /**
109
     * 获取模板引擎渲染内容
110
     * @param $callback
111
     * @return string
112
     * @throws \Exception
113
     */
114 3
    protected function getContent($callback): string
115
    {
116
        // 页面缓存
117 3
        ob_start();
118 3
        if (PHP_VERSION > 8.0) {
119
            ob_implicit_flush(false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type integer expected by parameter $flag of ob_implicit_flush(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

119
            ob_implicit_flush(/** @scrutinizer ignore-type */ false);
Loading history...
120
        } else {
121 3
            ob_implicit_flush(0);
122
        }
123
124
        // 渲染输出
125
        try {
126 3
            $callback();
127
        } catch (\Exception $e) {
128
            ob_end_clean();
129
            throw $e;
130
        }
131
132
        // 获取并清空缓存
133 3
        $content = ob_get_clean();
134
135 3
        if ($this->filter) {
136 3
            $content = call_user_func_array($this->filter, [$content]);
137
        }
138
139 3
        return $content;
140
    }
141
142
    /**
143
     * 模板变量赋值
144
     * @access public
145
     * @param string $name  变量名
146
     * @param mixed  $value 变量值
147
     */
148 3
    public function __set($name, $value)
149
    {
150 3
        $this->data[$name] = $value;
151 3
    }
152
153
    /**
154
     * 取得模板显示变量的值
155
     * @access protected
156
     * @param string $name 模板变量
157
     * @return mixed
158
     */
159 3
    public function __get($name)
160
    {
161 3
        return $this->data[$name];
162
    }
163
164
    /**
165
     * 检测模板变量是否设置
166
     * @access public
167
     * @param string $name 模板变量名
168
     * @return bool
169
     */
170 3
    public function __isset($name)
171
    {
172 3
        return isset($this->data[$name]);
173
    }
174
175 3
    protected function resolveConfig(string $name)
176
    {
177 3
        $config = $this->app->config->get('view', []);
178 3
        Arr::forget($config, 'type');
179 3
        return $config;
180
    }
181
182
    /**
183
     * 默认驱动
184
     * @return string|null
185
     */
186 3
    public function getDefaultDriver()
187
    {
188 3
        return $this->app->config->get('view.type', 'php');
189
    }
190
191
}
192