Passed
Push — dev ( b8029f...5026e0 )
by 世昌
02:31
created

Template::seekSourcePath()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 6
nop 0
dl 0
loc 16
rs 9.9
c 0
b 0
f 0
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
    private function seekSourcePath():?array
45
    {
46
        $extArray = [];
47
        if (array_key_exists('subfix', $this->config)) {
48
            $extArray[$this->config['subfix']] = $this->config['raw'] ?? false;
49
        }
50
        $extArray['.tpl.html'] = false;
51
        $extArray['.php'] = true;
52
        $resource = $this->getResource($this->module);
53
        foreach ($extArray as $ext => $isRaw) {
54
            $path = $resource->getResourcePath($this->getTemplatePath() . '/' . $this->name . $ext);
55
            if ($path !== null) {
56
                return [$path , $isRaw];
57
            }
58
        }
59
        return null;
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    public function getPath()
66
    {
67
        if ($this->isRaw()) {
68
            return $this->source;
69
        }
70
        return parent::getPath();
71
    }
72
}
73