|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Kotori.php |
|
4
|
|
|
* |
|
5
|
|
|
* A Tiny Model-View-Controller PHP Framework |
|
6
|
|
|
* |
|
7
|
|
|
* This content is released under the Apache 2 License |
|
8
|
|
|
* |
|
9
|
|
|
* Copyright (c) 2015-2017 Kotori Technology. All rights reserved. |
|
10
|
|
|
* |
|
11
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
12
|
|
|
* you may not use this file except in compliance with the License. |
|
13
|
|
|
* You may obtain a copy of the License at |
|
14
|
|
|
* |
|
15
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
16
|
|
|
* |
|
17
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
18
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
19
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
20
|
|
|
* See the License for the specific language governing permissions and |
|
21
|
|
|
* limitations under the License. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* View Class |
|
26
|
|
|
* |
|
27
|
|
|
* @package Kotori |
|
28
|
|
|
* @subpackage Core |
|
29
|
|
|
* @author Kokororin |
|
30
|
|
|
* @link https://kotori.love |
|
31
|
|
|
*/ |
|
32
|
|
|
namespace Kotori\Core; |
|
33
|
|
|
|
|
34
|
|
|
use Kotori\Debug\Hook; |
|
35
|
|
|
use Kotori\Exception\NotFoundException; |
|
36
|
|
|
use Kotori\Exception\ResponseException; |
|
37
|
|
|
use Kotori\Traits\ControllerMethodsTrait; |
|
38
|
|
|
|
|
39
|
|
|
class View |
|
40
|
|
|
{ |
|
41
|
|
|
use ControllerMethodsTrait; |
|
42
|
|
|
/** |
|
43
|
|
|
* Template Direcory |
|
44
|
|
|
* |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $tplDir; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* |
|
51
|
|
|
* Template Path |
|
52
|
|
|
* |
|
53
|
|
|
* @var string |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $viewPath; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Variable List |
|
59
|
|
|
* |
|
60
|
|
|
* @var array |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $data = []; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Variable List for TplInclude |
|
66
|
|
|
* |
|
67
|
|
|
* @var array |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $needData; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Class constructor |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $tplDir |
|
75
|
|
|
*/ |
|
76
|
3 |
|
public function __construct($tplDir = null) |
|
77
|
|
|
{ |
|
78
|
3 |
|
if (null == $tplDir) { |
|
|
|
|
|
|
79
|
3 |
|
$this->tplDir = Container::get('config')->get('app_full_path') . '/views/'; |
|
80
|
|
|
} else { |
|
81
|
|
|
$this->tplDir = $tplDir; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
3 |
|
Hook::listen(__CLASS__); |
|
85
|
3 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Set variables for Template |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $key |
|
91
|
|
|
* @param mixed $value |
|
92
|
|
|
* @return \Kotori\Core\View |
|
93
|
|
|
*/ |
|
94
|
|
|
public function assign($key, $value) |
|
95
|
|
|
{ |
|
96
|
|
|
$this->data[$key] = $value; |
|
97
|
|
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Display Output |
|
102
|
|
|
* |
|
103
|
|
|
* Processes and sends finalized output data to the browser along |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $tpl |
|
106
|
|
|
* @return void |
|
107
|
|
|
* |
|
108
|
|
|
* @throws \Kotori\Exception\NotFoundException |
|
109
|
|
|
* @throws \Kotori\Exception\ResponseException |
|
110
|
|
|
*/ |
|
111
|
|
|
public function display($tpl = '') |
|
112
|
|
|
{ |
|
113
|
|
|
if (Container::get('request')->isCli()) { |
|
114
|
|
|
throw new ResponseException('cannot render template in CLI mode'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
if ('' === $tpl) { |
|
118
|
|
|
$tpl = Container::get('route')->getController() . '/' |
|
119
|
|
|
. Container::get('route')->getAction(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$this->viewPath = $this->tplDir . $tpl . '.html'; |
|
123
|
|
|
if (!Helper::isFile($this->viewPath)) { |
|
124
|
|
|
throw new NotFoundException('Template is not existed.'); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
unset($tpl); |
|
128
|
|
|
ob_start(); |
|
129
|
|
|
extract($this->data, EXTR_OVERWRITE); |
|
130
|
|
|
include $this->viewPath; |
|
131
|
|
|
$buffer = ob_get_contents(); |
|
132
|
|
|
ob_get_clean(); |
|
133
|
|
|
$output = Helper::comment() . preg_replace('|</body>.*?</html>|is', '', $buffer, -1, $count) . Container::get('trace')->showTrace(); |
|
134
|
|
|
if ($count > 0) { |
|
135
|
|
|
$output .= '</body></html>'; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
echo $output; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Include Template |
|
143
|
|
|
* |
|
144
|
|
|
* @param string $path |
|
145
|
|
|
* @param array $data |
|
146
|
|
|
* @return void |
|
147
|
|
|
*/ |
|
148
|
|
|
public function need($path, array $data = []) |
|
149
|
|
|
{ |
|
150
|
|
|
$this->needData = [ |
|
151
|
|
|
'path' => Container::get('config')->get('app_full_path') . '/views/' . $path . '.html', |
|
152
|
|
|
'data' => $data, |
|
153
|
|
|
]; |
|
154
|
|
|
unset($path, $data); |
|
155
|
|
|
extract($this->needData['data']); |
|
156
|
|
|
include $this->needData['path']; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|