Completed
Pull Request — 6.0 (#2162)
by
unknown
11:18
created

View::__isset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 (\Exception $e) {
126
            ob_end_clean();
127
            throw $e;
128
        } catch (\Error $e) {
129
            ob_end_clean();
130
            throw $e;
131
        }
132
133
        // 获取并清空缓存
134 1
        $content = ob_get_clean();
135
136 1
        if ($this->filter) {
137 1
            $content = call_user_func_array($this->filter, [$content]);
138
        }
139
140 1
        return $content;
141
    }
142
143
    /**
144
     * 模板变量赋值
145
     * @access public
146
     * @param string $name  变量名
147
     * @param mixed  $value 变量值
148
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
149 1
    public function __set($name, $value)
150
    {
151 1
        $this->data[$name] = $value;
152 1
    }
153
154
    /**
155
     * 取得模板显示变量的值
156
     * @access protected
157
     * @param string $name 模板变量
158
     * @return mixed
159
     */
160 1
    public function __get($name)
161
    {
162 1
        return $this->data[$name];
163
    }
164
165
    /**
166
     * 检测模板变量是否设置
167
     * @access public
168
     * @param string $name 模板变量名
169
     * @return bool
170
     */
171 1
    public function __isset($name)
172
    {
173 1
        return isset($this->data[$name]);
174
    }
175
176 1
    protected function resolveConfig(string $name)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function resolveConfig()
Loading history...
177
    {
178 1
        $config = $this->app->config->get('view', []);
179 1
        Arr::forget($config, 'type');
180 1
        return $config;
181
    }
182
183
    /**
184
     * 默认驱动
185
     * @return string|null
186
     */
187 1
    public function getDefaultDriver()
188
    {
189 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...
190
    }
191
192
}
193