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
|
|
|
* @return void |
|
|
|
|
76
|
|
|
*/ |
77
|
3 |
|
public function __construct($tplDir = null) |
78
|
|
|
{ |
79
|
3 |
|
if (null == $tplDir) { |
|
|
|
|
80
|
3 |
|
$this->tplDir = Container::get('config')->get('app_full_path') . '/views/'; |
81
|
|
|
} else { |
82
|
|
|
$this->tplDir = $tplDir; |
83
|
|
|
} |
84
|
|
|
|
85
|
3 |
|
Hook::listen(__CLASS__); |
86
|
3 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set variables for Template |
90
|
|
|
* |
91
|
|
|
* @param string $key |
92
|
|
|
* @param mixed $value |
93
|
|
|
* @return \Kotori\Core\View |
94
|
|
|
*/ |
95
|
|
|
public function assign($key, $value) |
96
|
|
|
{ |
97
|
|
|
$this->data[$key] = $value; |
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Display Output |
103
|
|
|
* |
104
|
|
|
* Processes and sends finalized output data to the browser along |
105
|
|
|
* |
106
|
|
|
* @param string $tpl |
107
|
|
|
* @return void |
108
|
|
|
* |
109
|
|
|
* @throws \Kotori\Exception\NotFoundException |
110
|
|
|
* @throws \Kotori\Exception\ResponseException |
111
|
|
|
*/ |
112
|
|
|
public function display($tpl = '') |
113
|
|
|
{ |
114
|
|
|
if (Container::get('request')->isCli()) { |
115
|
|
|
throw new ResponseException('cannot render template in CLI mode'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if ('' === $tpl) { |
119
|
|
|
$tpl = Container::get('route')->getController() . '/' |
120
|
|
|
. Container::get('route')->getAction(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$this->viewPath = $this->tplDir . $tpl . '.html'; |
124
|
|
|
if (!Helper::isFile($this->viewPath)) { |
125
|
|
|
throw new NotFoundException('Template is not existed.'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
unset($tpl); |
129
|
|
|
ob_start(); |
130
|
|
|
extract($this->data, EXTR_OVERWRITE); |
131
|
|
|
include $this->viewPath; |
132
|
|
|
$buffer = ob_get_contents(); |
133
|
|
|
ob_get_clean(); |
134
|
|
|
$output = Helper::comment() . preg_replace('|</body>.*?</html>|is', '', $buffer, -1, $count) . Container::get('trace')->showTrace(); |
135
|
|
|
if ($count > 0) { |
136
|
|
|
$output .= '</body></html>'; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
echo $output; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Include Template |
144
|
|
|
* |
145
|
|
|
* @param string $path |
146
|
|
|
* @param array $data |
147
|
|
|
* @return void |
148
|
|
|
*/ |
149
|
|
|
public function need($path, $data = []) |
150
|
|
|
{ |
151
|
|
|
$this->needData = [ |
152
|
|
|
'path' => Container::get('config')->get('app_full_path') . '/views/' . $path . '.html', |
153
|
|
|
'data' => $data, |
154
|
|
|
]; |
155
|
|
|
unset($path, $data); |
156
|
|
|
extract($this->needData['data']); |
157
|
|
|
include $this->needData['path']; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.