Passed
Push — 5.2 ( f44aef...462ef8 )
by liu
02:32
created

Php::exists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 8
rs 10
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\view\driver;
14
15
use think\App;
16
use think\template\exception\TemplateNotFoundException;
0 ignored issues
show
Bug introduced by
The type think\template\exception\TemplateNotFoundException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
18
class Php
1 ignored issue
show
Coding Style introduced by
Missing class doc comment
Loading history...
19
{
20
    protected $template;
21
    protected $content;
22
    protected $app;
23
24
    // 模板引擎参数
25
    protected $config = [
26
        // 默认模板渲染规则 1 解析为小写+下划线 2 全部转换小写
27
        'auto_rule'   => 1,
28
        // 视图基础目录(集中式)
29
        'view_base'   => '',
30
        // 模板起始路径
31
        'view_path'   => '',
32
        // 模板文件后缀
33
        'view_suffix' => 'php',
34
        // 模板文件名分隔符
35
        'view_depr'   => DIRECTORY_SEPARATOR,
36
    ];
37
38
    public function __construct(App $app, array $config = [])
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
39
    {
40
        $this->app    = $app;
41
        $this->config = array_merge($this->config, (array) $config);
42
    }
43
44
    /**
45
     * 检测是否存在模板文件
46
     * @access public
47
     * @param  string $template 模板文件或者模板规则
48
     * @return bool
49
     */
50
    public function exists(string $template): bool
51
    {
52
        if ('' == pathinfo($template, PATHINFO_EXTENSION)) {
53
            // 获取模板文件名
54
            $template = $this->parseTemplate($template);
55
        }
56
57
        return is_file($template);
58
    }
59
60
    /**
61
     * 渲染模板文件
62
     * @access public
63
     * @param  string    $template 模板文件
64
     * @param  array     $data 模板变量
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
65
     * @return void
66
     */
67
    public function fetch(string $template, array $data = []): void
68
    {
69
        if ('' == pathinfo($template, PATHINFO_EXTENSION)) {
70
            // 获取模板文件名
71
            $template = $this->parseTemplate($template);
72
        }
73
74
        // 模板不存在 抛出异常
75
        if (!is_file($template)) {
76
            throw new TemplateNotFoundException('template not exists:' . $template, $template);
77
        }
78
79
        $this->template = $template;
80
81
        // 记录视图信息
82
        $this->app['log']
83
            ->record('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]');
84
85
        extract($data, EXTR_OVERWRITE);
86
87
        include $this->template;
88
    }
89
90
    /**
91
     * 渲染模板内容
92
     * @access public
93
     * @param  string    $content 模板内容
94
     * @param  array     $data 模板变量
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
95
     * @return void
96
     */
97
    public function display(string $content, array $data = []): void
98
    {
99
        $this->content = $content;
100
101
        extract($data, EXTR_OVERWRITE);
102
        eval('?>' . $this->content);
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
103
    }
104
105
    /**
106
     * 自动定位模板文件
107
     * @access private
108
     * @param  string $template 模板文件规则
109
     * @return string
110
     */
111
    private function parseTemplate(string $template): string
0 ignored issues
show
Coding Style introduced by
Private method name "Php::parseTemplate" must be prefixed with an underscore
Loading history...
112
    {
113
        if (empty($this->config['view_path'])) {
114
            $this->config['view_path'] = $this->app->getAppPath() . 'view' . DIRECTORY_SEPARATOR;
115
        }
116
117
        $request = $this->app['request'];
118
119
        // 获取视图根目录
120
        if (strpos($template, '@')) {
121
            // 跨模块调用
122
            list($app, $template) = explode('@', $template);
123
        }
124
125
        if ($this->config['view_base']) {
126
            // 基础视图目录
127
            $app  = isset($app) ? $app : $request->app();
128
            $path = $this->config['view_base'] . ($app ? $app . DIRECTORY_SEPARATOR : '');
129
        } else {
130
            $path = isset($app) ? $this->app->getBasePath() . $app . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR : $this->config['view_path'];
131
        }
132
133
        $depr = $this->config['view_depr'];
134
135
        if (0 !== strpos($template, '/')) {
136
            $template   = str_replace(['/', ':'], $depr, $template);
137
            $controller = App::parseName($request->controller());
138
139
            if ($controller) {
140
                if ('' == $template) {
141
                    // 如果模板文件名为空 按照默认规则定位
142
                    $template = str_replace('.', DIRECTORY_SEPARATOR, $controller) . $depr . (1 == $this->config['auto_rule'] ? App::parseName($request->action(true)) : $request->action());
143
                } elseif (false === strpos($template, $depr)) {
144
                    $template = str_replace('.', DIRECTORY_SEPARATOR, $controller) . $depr . $template;
145
                }
146
            }
147
        } else {
148
            $template = str_replace(['/', ':'], $depr, substr($template, 1));
149
        }
150
151
        return $path . ltrim($template, '/') . '.' . ltrim($this->config['view_suffix'], '.');
152
    }
153
154
    /**
155
     * 配置模板引擎
156
     * @access private
157
     * @param  array  $config 参数
158
     * @return void
159
     */
160
    public function config(array $config): void
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed. ( Ignorable by Annotation )

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

160
    public function config(/** @scrutinizer ignore-unused */ array $config): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
161
    {
162
        $this->config = array_merge($this->config, $name);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $name seems to be never defined.
Loading history...
163
    }
164
165
    /**
166
     * 获取模板引擎配置
167
     * @access public
168
     * @param  string  $name 参数名
169
     * @return void
170
     */
171
    public function getConfig(string $name)
172
    {
173
        return $this->config[$name] ?? null;
174
    }
175
}
176