1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace suda\application\template; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use ReflectionException; |
7
|
|
|
use suda\framework\Config; |
8
|
|
|
use suda\framework\filesystem\FileSystem; |
9
|
|
|
use suda\application\template\compiler\Compiler; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* 模块模板 |
13
|
|
|
*/ |
14
|
|
|
class ModuleTemplate extends ModuleTemplateBase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* 获取模板源路径 |
18
|
|
|
* |
19
|
|
|
* @return string|null |
20
|
|
|
*/ |
21
|
|
|
public function getSourcePath(): ?string |
22
|
|
|
{ |
23
|
|
|
$subfix = $this->config['subfix'] ?? '.tpl.html'; |
24
|
|
|
$path = $this |
25
|
|
|
->getResource($this->module) |
26
|
|
|
->getResourcePath($this->getTemplatePath() . '/' . $this->name . $subfix); |
27
|
|
|
$this->loadDynamicTemplateConfig($path.'.ini'); |
28
|
|
|
return $path; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string|null $path |
33
|
|
|
*/ |
34
|
|
|
protected function loadDynamicTemplateConfig(?string $path) { |
35
|
|
|
if ($path !== null) { |
36
|
|
|
$config = Config::loadConfig($path); |
37
|
|
|
// 自定义编译标签 |
38
|
|
|
if ($config !== null) { |
39
|
|
|
$this->config['compile'] = $config; |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* 获取模板编译后的路径 |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getPath() |
50
|
|
|
{ |
51
|
|
|
$output = $this->config['output'] ?? $this->application->getDataPath() . '/template/' . $this->uriName; |
52
|
|
|
FileSystem::make($output); |
53
|
|
|
return $output . '/' . $this->name . '.php'; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* 包含模板 |
58
|
|
|
* |
59
|
|
|
* @param string $name |
60
|
|
|
* @return void |
61
|
|
|
* @throws Exception |
62
|
|
|
*/ |
63
|
|
|
public function include(string $name) |
64
|
|
|
{ |
65
|
|
|
$included = new self($name, $this->application, $this->request, $this->module); |
66
|
|
|
$included->parent = $this; |
67
|
|
|
$included->value = $this->value; |
68
|
|
|
echo $included->getRenderedString(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string |
73
|
|
|
* @throws Exception |
74
|
|
|
*/ |
75
|
|
|
public function getRenderedString() |
76
|
|
|
{ |
77
|
|
|
$this->application->debug()->time('render ' . $this->name); |
78
|
|
|
$code = parent::getRenderedString(); |
79
|
|
|
$this->application->debug()->timeEnd('render ' . $this->name); |
80
|
|
|
return $code; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return bool |
85
|
|
|
* @throws Exception |
86
|
|
|
*/ |
87
|
|
|
protected function compile() |
88
|
|
|
{ |
89
|
|
|
if ($this->isCompiled() === false) { |
90
|
|
|
$this->application->debug()->time('compile ' . $this->name); |
91
|
|
|
$result = parent::compile(); |
92
|
|
|
$this->application->debug()->timeEnd('compile ' . $this->name); |
93
|
|
|
return $result; |
94
|
|
|
} |
95
|
|
|
return true; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string|array|null $name |
101
|
|
|
* @param mixed $values |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function getUrl($name = null, $values = null) |
105
|
|
|
{ |
106
|
|
|
$defaultName = $this->request->getAttribute('route'); |
107
|
|
|
$query = $this->request->get() ?? []; |
108
|
|
|
if (is_string($name)) { |
109
|
|
|
if (!is_array($values)) { |
110
|
|
|
$args = func_get_args(); |
111
|
|
|
array_shift($args); |
112
|
|
|
$values = $args; |
113
|
|
|
} |
114
|
|
|
return $this->application->getUrl($this->request, $name, $values ?? [], true, $this->module, $this->group); |
115
|
|
|
} elseif (is_string($defaultName)) { |
116
|
|
|
$parameter = is_array($name)?array_merge($query, $name):$query; |
117
|
|
|
return $this->application->getUrl( |
118
|
|
|
$this->request, |
119
|
|
|
$defaultName, |
120
|
|
|
$parameter, |
121
|
|
|
true, |
122
|
|
|
$this->module, |
123
|
|
|
$this->group |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
return '#' . $defaultName; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* 判断是否是某路由 |
132
|
|
|
* |
133
|
|
|
* @param string $name |
134
|
|
|
* @param array $parameter |
135
|
|
|
* @return boolean |
136
|
|
|
*/ |
137
|
|
|
public function is(string $name, array $parameter = null) |
138
|
|
|
{ |
139
|
|
|
$full = $this->application->getRouteName($name, $this->module, $this->group); |
140
|
|
|
if ($this->request->getAttribute('route') === $full) { |
141
|
|
|
if (is_array($parameter)) { |
142
|
|
|
foreach ($parameter as $key => $value) { |
143
|
|
|
if ($this->request->getQuery($key) != $value) { |
144
|
|
|
return false; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
return true; |
149
|
|
|
} |
150
|
|
|
return false; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|