Completed
Push — 6.0 ( 7d6f90...b04f47 )
by yun
02:12
created

View::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 6
ccs 4
cts 4
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\response;
14
15
use think\Cookie;
16
use think\Response;
17
use think\View as BaseView;
18
19
/**
20
 * View Response
21
 */
22
class View extends Response
23
{
24
    /**
25
     * 输出参数
26
     * @var array
27
     */
28
    protected $options = [];
29
30
    /**
31
     * 输出变量
32
     * @var array
33
     */
34
    protected $vars = [];
35
36
    /**
37
     * 输出过滤
38
     * @var mixed
39
     */
40
    protected $filter;
41
42
    /**
43
     * 输出type
44
     * @var string
45
     */
46
    protected $contentType = 'text/html';
47
48
    /**
49
     * View对象
50
     * @var BaseView
51
     */
52
    protected $view;
53
54
    /**
55
     * 是否内容渲染
56
     * @var bool
57
     */
58
    protected $isContent = false;
59
60 3
    public function __construct(Cookie $cookie, BaseView $view, $data = '', int $code = 200)
61
    {
62 3
        $this->init($data, $code);
63
64 3
        $this->cookie = $cookie;
65 3
        $this->view   = $view;
66 3
    }
67
68
    /**
69
     * 设置是否为内容渲染
70
     * @access public
71
     * @param  bool $content
72
     * @return $this
73
     */
74
    public function isContent(bool $content = true)
75
    {
76
        $this->isContent = $content;
77
        return $this;
78
    }
79
80
    /**
81
     * 处理数据
82
     * @access protected
83
     * @param  mixed $data 要处理的数据
84
     * @return string
85
     */
86
    protected function output($data): string
87
    {
88
        // 渲染模板输出
89
        return $this->view->filter($this->filter)
90
            ->fetch($data, $this->vars, $this->isContent);
0 ignored issues
show
Unused Code introduced by
The call to think\View::fetch() has too many arguments starting with $this->isContent. ( Ignorable by Annotation )

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

90
            ->/** @scrutinizer ignore-call */ fetch($data, $this->vars, $this->isContent);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
91
    }
92
93
    /**
94
     * 获取视图变量
95
     * @access public
96
     * @param  string $name 模板变量
97
     * @return mixed
98
     */
99 3
    public function getVars(string $name = null)
100
    {
101 3
        if (is_null($name)) {
102 3
            return $this->vars;
103
        } else {
104
            return $this->vars[$name] ?? null;
105
        }
106
    }
107
108
    /**
109
     * 模板变量赋值
110
     * @access public
111
     * @param  string|array $name  模板变量
112
     * @param  mixed        $value 变量值
113
     * @return $this
114
     */
115 3
    public function assign($name, $value = null)
116
    {
117 3
        if (is_array($name)) {
118 3
            $this->vars = array_merge($this->vars, $name);
119
        } else {
120
            $this->vars[$name] = $value;
121
        }
122
123 3
        return $this;
124
    }
125
126
    /**
127
     * 视图内容过滤
128
     * @access public
129
     * @param callable $filter
130
     * @return $this
131
     */
132
    public function filter(callable $filter = null)
133
    {
134
        $this->filter = $filter;
135
        return $this;
136
    }
137
138
    /**
139
     * 检查模板是否存在
140
     * @access public
141
     * @param  string  $name 模板名
142
     * @return bool
143
     */
144
    public function exists(string $name): bool
145
    {
146
        return $this->view->exists($name);
0 ignored issues
show
Bug introduced by
The method exists() does not exist on think\View. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

146
        return $this->view->/** @scrutinizer ignore-call */ exists($name);
Loading history...
147
    }
148
149
}
150