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