Passed
Push — master ( 3086d8...2c5405 )
by 世昌
02:04
created

ModuleTemplate   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 91
rs 10
c 0
b 0
f 0
wmc 16

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getUrl() 0 16 6
A getRenderedString() 0 6 1
A getSourcePath() 0 4 1
A is() 0 13 5
A getPath() 0 5 1
A include() 0 6 1
A __construct() 0 3 1
1
<?php
2
namespace suda\application\template;
3
4
use function constant;
5
use Exception;
6
use function is_array;
7
use function is_string;
8
use suda\framework\Request;
9
use suda\application\Application;
10
use suda\framework\filesystem\FileSystem;
11
12
/**
13
 * 模块模板
14
 */
15
class ModuleTemplate extends ModuleTemplateBase
16
{
17
    public function __construct(string $name, Application $application, Request $request, ?string $defaultModule = '')
18
    {
19
        parent::__construct($name, $application, $request, $defaultModule);
20
    }
21
22
    /**
23
     * 获取模板源路径
24
     *
25
     * @return string|null
26
     */
27
    public function getSourcePath():?string
28
    {
29
        $subfix = $this->config['subfix'] ?? '.tpl.html';
30
        return $this->getResource($this->module)->getResourcePath($this->getTemplatePath().'/'.$this->name.$subfix);
31
    }
32
33
    /**
34
     * 获取模板编译后的路径
35
     *
36
     * @return string
37
     */
38
    public function getPath()
39
    {
40
        $output = $this->config['output'] ?? constant('SUDA_DATA').'/template/'. $this->uriName;
41
        FileSystem::make($output);
42
        return $output .'/'. $this->name.'.php';
43
    }
44
45
    /**
46
     * 包含模板
47
     *
48
     * @param string $name
49
     * @return void
50
     * @throws Exception
51
     */
52
    public function include(string $name)
53
    {
54
        $included = new self($name, $this->application, $this->request, $this->module);
55
        $included->parent = $this;
56
        $included->value = $this->value;
57
        echo $included->getRenderedString();
58
    }
59
60
    public function getRenderedString()
61
    {
62
        $this->application->debug()->time('render '.$this->name);
63
        $code =  parent::getRenderedString();
64
        $this->application->debug()->timeEnd('render '.$this->name);
65
        return $code;
66
    }
67
68
    public function getUrl($name = null, $values = null)
69
    {
70
        $defaultName = $this->request->getAttribute('route');
71
        if (is_string($name)) {
72
            if (!is_array($values)) {
73
                $args = func_get_args();
74
                array_shift($args);
75
                $values = $args;
76
            }
77
            return $this->application->getUrl($this->request, $name, $values ?? [], true, $this->module, $this->group);
78
        } elseif (is_array($name) && is_string($defaultName)) {
79
            return $this->application->getUrl($this->request, $defaultName, array_merge($this->request->get() ?? [], $name) , true, $this->module, $this->group);
80
        } elseif (is_string($defaultName)) {
81
            return $this->application->getUrl($this->request, $defaultName, $this->request->get() ?? [], true, $this->module, $this->group);
82
        }
83
        return '#'.$defaultName;
84
    }
85
86
    /**
87
     * 判断是否是某路由
88
     *
89
     * @param string $name
90
     * @param array $parameter
91
     * @return boolean
92
     */
93
    public function is(string $name, array $parameter = null) {
94
        $full = $this->application->getRouteName($name, $this->module, $this->group);
95
        if ($this->request->getAttribute('route') === $full) {
96
            if (is_array($parameter)) {
97
                foreach ($parameter as $key => $value) {
98
                    if ($this->request->getQuery($key) != $value){
99
                        return false;
100
                    }
101
                }
102
            }
103
            return true;
104
        }
105
        return false;
106
    } 
107
}
108