Passed
Push — master ( b8029f...b45126 )
by 世昌
02:31
created

Template   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSourcePath() 0 9 3
A include() 0 6 1
A seekSourcePath() 0 16 4
A getPath() 0 6 2
1
<?php
2
3
namespace suda\application\template;
4
5
use Exception;
6
7
/**
8
 * 模板
9
 */
10
class Template extends ModuleTemplate
11
{
12
13
    /**
14
     * 获取模板源路径
15
     *
16
     * @return string|null
17
     */
18
    public function getSourcePath(): ?string
19
    {
20
        if (strlen($this->source) === 0) {
21
            $path = $this->seekSourcePath();
22
            if (count($path) === 2) {
23
                list($this->source, $this->raw) = $path;
24
            }
25
        }
26
        return $this->source;
27
    }
28
29
    /**
30
     * 包含模板
31
     *
32
     * @param string $name
33
     * @return void
34
     * @throws Exception
35
     */
36
    public function include(string $name)
37
    {
38
        $included = new self($name, $this->application, $this->request, $this->module);
39
        $included->parent = $this;
40
        $included->value = $this->value;
41
        echo $included->getRenderedString();
42
    }
43
44
    /**
45
     * @return array|null
46
     */
47
    private function seekSourcePath():?array
48
    {
49
        $extArray = [];
50
        if (array_key_exists('subfix', $this->config)) {
51
            $extArray[$this->config['subfix']] = $this->config['raw'] ?? false;
52
        }
53
        $extArray['.tpl.html'] = false;
54
        $extArray['.php'] = true;
55
        $resource = $this->getResource($this->module);
56
        foreach ($extArray as $ext => $isRaw) {
57
            $path = $resource->getResourcePath($this->getTemplatePath() . '/' . $this->name . $ext);
58
            if ($path !== null) {
59
                return [$path , $isRaw];
60
            }
61
        }
62
        return null;
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getPath()
69
    {
70
        if ($this->isRaw()) {
71
            return $this->source;
72
        }
73
        return parent::getPath();
74
    }
75
}
76