Passed
Push — 5.2 ( ab77ce...f44aef )
by liu
02:47
created

View::fetch()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 6
nop 2
dl 0
loc 23
rs 9.8666
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
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
class View
1 ignored issue
show
Coding Style introduced by
Missing class doc comment
Loading history...
16
{
17
    /**
18
     * 模板引擎实例
19
     * @var object
20
     */
21
    public $engine;
22
23
    /**
24
     * 模板变量
25
     * @var array
26
     */
27
    protected $data = [];
28
29
    /**
30
     * 内容过滤
31
     * @var mixed
32
     */
33
    protected $filter;
34
35
    /**
36
     * 初始化
37
     * @access public
38
     * @param  array $options  模板引擎参数
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
39
     * @return $this
40
     */
41
    public function __construct(array $options = [])
42
    {
43
        // 初始化模板引擎
44
        $type = $options['type'] ?? 'think';
45
        unset($options['type']);
46
        $this->engine($type, $options);
47
48
        return $this;
49
    }
50
51
    public static function __make(Config $config)
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
Coding Style introduced by
Method name "View::__make" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
52
    {
53
        return new static($config->get('template'));
0 ignored issues
show
Bug introduced by
It seems like $config->get('template') can also be of type null; however, parameter $options of think\View::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

53
        return new static(/** @scrutinizer ignore-type */ $config->get('template'));
Loading history...
54
    }
55
56
    /**
57
     * 模板变量赋值
58
     * @access public
59
     * @param  string|array $name  模板变量
60
     * @param  mixed        $value 变量值
61
     * @return $this
62
     */
63
    public function assign($name, $value = null)
64
    {
65
        if (is_array($name)) {
66
            $this->data = array_merge($this->data, $name);
67
        } else {
68
            $this->data[$name] = $value;
69
        }
70
71
        return $this;
72
    }
73
74
    /**
75
     * 设置当前模板解析的引擎
76
     * @access public
77
     * @param  string       $type    模板引擎类型
78
     * @param  array|string $options 模板引擎参数
79
     * @return $this
80
     */
81
    public function engine(string $type, array $options = [])
82
    {
83
        $this->engine = App::factory($type, '\\think\\view\\driver\\', $options);
84
85
        return $this;
86
    }
87
88
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $config should have a doc-comment as per coding-style.
Loading history...
89
     * 配置模板引擎
90
     * @access public
91
     * @param  array  $name 模板参数
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter $name does not match actual variable name $config
Loading history...
92
     * @return $this
93
     */
94
    public function config(array $config)
95
    {
96
        $this->engine->config($config);
97
98
        return $this;
99
    }
100
101
    /**
102
     * 检查模板是否存在
103
     * @access public
104
     * @param  string  $name 参数名
105
     * @return bool
106
     */
107
    public function exists(string $name): bool
108
    {
109
        return $this->engine->exists($name);
110
    }
111
112
    /**
113
     * 视图过滤
114
     * @access public
115
     * @param Callable  $filter 过滤方法或闭包
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
116
     * @return $this
117
     */
118
    public function filter(callable $filter = null)
119
    {
120
        $this->filter = $filter;
121
        return $this;
122
    }
123
124
    /**
125
     * 解析和获取模板内容 用于输出
126
     * @access public
127
     * @param  string    $template 模板文件名或者内容
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter name; 1 found
Loading history...
128
     * @param  bool      $renderContent     是否渲染内容
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 5 found
Loading history...
129
     * @return string
130
     * @throws \Exception
131
     */
132
    public function fetch(string $template = '', bool $renderContent = false): string
133
    {
134
        // 页面缓存
135
        ob_start();
136
        ob_implicit_flush(0);
137
138
        // 渲染输出
139
        try {
140
            $method = $renderContent ? 'display' : 'fetch';
141
            $this->engine->$method($template, $this->data);
142
        } catch (\Exception $e) {
143
            ob_end_clean();
144
            throw $e;
145
        }
146
147
        // 获取并清空缓存
148
        $content = ob_get_clean();
149
150
        if ($this->filter) {
151
            $content = call_user_func_array($this->filter, [$content]);
152
        }
153
154
        return $content;
155
    }
156
157
    /**
158
     * 渲染内容输出
159
     * @access public
160
     * @param  string $content 内容
161
     * @return string
162
     */
163
    public function display(string $content): string
164
    {
165
        return $this->fetch($content, true);
166
    }
167
168
    /**
169
     * 模板变量赋值
170
     * @access public
171
     * @param  string    $name  变量名
172
     * @param  mixed     $value 变量值
173
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
174
    public function __set($name, $value)
175
    {
176
        $this->data[$name] = $value;
177
    }
178
179
    /**
180
     * 取得模板显示变量的值
181
     * @access protected
182
     * @param  string $name 模板变量
183
     * @return mixed
184
     */
185
    public function __get($name)
186
    {
187
        return $this->data[$name];
188
    }
189
190
    /**
191
     * 检测模板变量是否设置
192
     * @access public
193
     * @param  string $name 模板变量名
194
     * @return bool
195
     */
196
    public function __isset($name)
197
    {
198
        return isset($this->data[$name]);
199
    }
200
}
201