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
|
|
|
|