Passed
Pull Request — 6.0 (#2163)
by
unknown
05:50
created

View::getContent()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.9102

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 13
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 24
ccs 8
cts 13
cp 0.6153
crap 4.9102
rs 9.8333
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
0 ignored issues
show
Coding Style introduced by
Package name "think" is not valid; consider "Think" instead
Loading history...
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 1
    public function engine(string $type = null)
45
    {
46 1
        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 1
    public function assign($name, $value = null)
57
    {
58 1
        if (is_array($name)) {
59 1
            $this->data = array_merge($this->data, $name);
60
        } else {
61 1
            $this->data[$name] = $value;
62
        }
63
64 1
        return $this;
65
    }
66
67
    /**
68
     * 视图过滤
69
     * @access public
70
     * @param Callable $filter 过滤方法或闭包
71
     * @return $this
72
     */
73 1
    public function filter(callable $filter = null)
74
    {
75 1
        $this->filter = $filter;
76 1
        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 1
    public function fetch(string $template = '', array $vars = []): string
88
    {
89
        return $this->getContent(function () use ($vars, $template) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
90 1
            $this->engine()->fetch($template, array_merge($this->data, $vars));
91 1
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
92
    }
93
94
    /**
95
     * 渲染内容输出
96
     * @access public
97
     * @param string $content 内容
98
     * @param array  $vars    模板变量
99
     * @return string
100
     */
101 1
    public function display(string $content, array $vars = []): string
102
    {
103
        return $this->getContent(function () use ($vars, $content) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
104 1
            $this->engine()->display($content, array_merge($this->data, $vars));
105 1
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
106
    }
107
108
    /**
109
     * 获取模板引擎渲染内容
110
     * @param $callback
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
111
     * @return string
112
     * @throws \Exception
113
     */
114 1
    protected function getContent($callback): string
115
    {
116
        // 页面缓存
117 1
        ob_start();
118 1
        ob_implicit_flush(0);
119
120
        // 渲染输出
121
        try {
122 1
            $callback();
123
        } catch (\think\exception\HttpResponseException $e) {
124
            throw $e;
125
        } catch (\Throwable $e) {
126
            ob_end_clean();
127
            throw $e;
128
        }
129
130
        // 获取并清空缓存
131 1
        $content = ob_get_clean();
132
133 1
        if ($this->filter) {
134 1
            $content = call_user_func_array($this->filter, [$content]);
135
        }
136
137 1
        return $content;
138
    }
139
140
    /**
141
     * 模板变量赋值
142
     * @access public
143
     * @param string $name  变量名
144
     * @param mixed  $value 变量值
145
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
146 1
    public function __set($name, $value)
147
    {
148 1
        $this->data[$name] = $value;
149 1
    }
150
151
    /**
152
     * 取得模板显示变量的值
153
     * @access protected
154
     * @param string $name 模板变量
155
     * @return mixed
156
     */
157 1
    public function __get($name)
158
    {
159 1
        return $this->data[$name];
160
    }
161
162
    /**
163
     * 检测模板变量是否设置
164
     * @access public
165
     * @param string $name 模板变量名
166
     * @return bool
167
     */
168 1
    public function __isset($name)
169
    {
170 1
        return isset($this->data[$name]);
171
    }
172
173 1
    protected function resolveConfig(string $name)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function resolveConfig()
Loading history...
174
    {
175 1
        $config = $this->app->config->get('view', []);
176 1
        Arr::forget($config, 'type');
177 1
        return $config;
178
    }
179
180
    /**
181
     * 默认驱动
182
     * @return string|null
183
     */
184 1
    public function getDefaultDriver()
185
    {
186 1
        return $this->app->config->get('view.type', 'php');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->app->confi...get('view.type', 'php') also could return the type array which is incompatible with the documented return type null|string.
Loading history...
187
    }
188
189
}
190