Completed
Push — 6.0 ( bb666d...c1d13c )
by yun
05:54
created

View::__make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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