|
1
|
|
|
<?php /** MicroPhpView */ |
|
2
|
|
|
|
|
3
|
|
|
namespace Micro\Mvc\Views; |
|
4
|
|
|
|
|
5
|
|
|
use Micro\Base\Exception; |
|
6
|
|
|
use Micro\Web\Language; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class PhpView |
|
10
|
|
|
* |
|
11
|
|
|
* @author Oleg Lunegov <[email protected]> |
|
12
|
|
|
* @link https://github.com/lugnsk/micro |
|
13
|
|
|
* @copyright Copyright © 2013 Oleg Lunegov |
|
14
|
|
|
* @license /LICENSE |
|
15
|
|
|
* @package Micro |
|
16
|
|
|
* @subpackage Mvc/Views |
|
17
|
|
|
* @version 1.0 |
|
18
|
|
|
* @since 1.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
class PhpView extends View |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var string Layout to render */ |
|
23
|
|
|
public $layout; |
|
24
|
|
|
/** @var string $view View name */ |
|
25
|
|
|
public $view; |
|
26
|
|
|
/** @var string $path Path to view */ |
|
27
|
|
|
public $path; |
|
28
|
|
|
/** @var string $data Return data */ |
|
29
|
|
|
public $data = ''; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Render partial |
|
33
|
|
|
* |
|
34
|
|
|
* @access public |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $view view name |
|
37
|
|
|
* |
|
38
|
|
|
* @return string |
|
39
|
|
|
* @throws Exception |
|
40
|
|
|
*/ |
|
41
|
|
|
public function renderPartial($view) |
|
42
|
|
|
{ |
|
43
|
|
|
$lay = $this->layout; |
|
44
|
|
|
$wi = $this->view; |
|
45
|
|
|
|
|
46
|
|
|
$this->layout = null; |
|
47
|
|
|
$this->view = $view; |
|
48
|
|
|
$output = $this->render(); |
|
49
|
|
|
$this->layout = $lay; |
|
50
|
|
|
$this->view = $wi; |
|
51
|
|
|
|
|
52
|
|
|
return $output; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Render insert data into view |
|
57
|
|
|
* |
|
58
|
|
|
* @access protected |
|
59
|
|
|
* |
|
60
|
|
|
* @return string |
|
61
|
|
|
* @throws Exception |
|
62
|
|
|
*/ |
|
63
|
|
|
public function render() |
|
64
|
|
|
{ |
|
65
|
|
|
if (!$this->view) { |
|
66
|
|
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $this->renderRawData( |
|
70
|
|
|
($this->data) ?: $this->renderFile($this->getViewFile($this->view), $this->params) |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Render raw data in layout |
|
76
|
|
|
* |
|
77
|
|
|
* @access public |
|
78
|
|
|
* @global Micro |
|
79
|
|
|
* @global Container |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $data arguments array |
|
82
|
|
|
* |
|
83
|
|
|
* @return string |
|
84
|
|
|
* @throws Exception |
|
85
|
|
|
*/ |
|
86
|
|
|
public function renderRawData($data = '') |
|
87
|
|
|
{ |
|
88
|
|
|
$layoutPath = null; |
|
89
|
|
|
if ($this->layout) { |
|
90
|
|
|
$layoutPath = $this->getLayoutFile($this->container->kernel->getAppDir(), $this->module); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if ($layoutPath) { |
|
|
|
|
|
|
94
|
|
|
$data = $this->insertStyleScripts($this->renderFile($layoutPath, ['content' => $data])); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $data; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Get layout path |
|
102
|
|
|
* |
|
103
|
|
|
* @access protected |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $appDir path to base dir |
|
106
|
|
|
* @param string $module module name |
|
107
|
|
|
* |
|
108
|
|
|
* @return string |
|
109
|
|
|
* @throws Exception |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function getLayoutFile($appDir, $module) |
|
112
|
|
|
{ |
|
113
|
|
|
if ($module) { |
|
114
|
|
|
$module = str_replace('\\', '/', substr($module, 4)); |
|
115
|
|
|
$module = substr($module, 0, strrpos($module, '/')); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$layout = $appDir . '/' . (($module) ? $module . '/' : $module); |
|
119
|
|
|
$afterPath = 'views/layouts/' . ucfirst($this->layout) . '.php'; |
|
120
|
|
|
|
|
121
|
|
|
if (!file_exists($layout . $afterPath)) { |
|
122
|
|
|
if (file_exists($appDir . '/' . $afterPath)) { |
|
123
|
|
|
return $appDir . '/' . $afterPath; |
|
124
|
|
|
} |
|
125
|
|
|
throw new Exception('Layout ' . ucfirst($this->layout) . ' not found.'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $layout . $afterPath; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Render file by path |
|
133
|
|
|
* |
|
134
|
|
|
* @access protected |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $fileName file name |
|
137
|
|
|
* @param array $data arguments array |
|
138
|
|
|
* |
|
139
|
|
|
* @return string |
|
140
|
|
|
* @throws Exception widget not declared |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function renderFile($fileName, array $data = []) |
|
|
|
|
|
|
143
|
|
|
{ |
|
144
|
|
|
/** @noinspection OnlyWritesOnParameterInspection */ |
|
145
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */ |
|
146
|
|
|
$lang = new Language($this->container, $fileName); |
|
147
|
|
|
extract($data, EXTR_PREFIX_SAME || EXTR_REFS, 'data'); |
|
148
|
|
|
ob_start(); |
|
149
|
|
|
|
|
150
|
|
|
/** @noinspection PhpIncludeInspection */ |
|
151
|
|
|
include str_replace('\\', '/', $fileName); |
|
152
|
|
|
|
|
153
|
|
View Code Duplication |
if (!empty($GLOBALS['widgetStack'])) { |
|
|
|
|
|
|
154
|
|
|
throw new Exception(count($GLOBALS['widgetStack']) . ' widgets not endings.'); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return ob_get_clean(); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Get view file |
|
162
|
|
|
* |
|
163
|
|
|
* @access private |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $view view file name |
|
166
|
|
|
* |
|
167
|
|
|
* @return string |
|
168
|
|
|
* @throws Exception |
|
169
|
|
|
*/ |
|
170
|
|
|
private function getViewFile($view) |
|
171
|
|
|
{ |
|
172
|
|
|
$calledClass = $this->path; |
|
173
|
|
|
|
|
174
|
|
|
// Calculate path to view |
|
175
|
|
|
if (substr($calledClass, 0, strpos($calledClass, '\\')) === 'App') { |
|
176
|
|
|
$path = $this->container->kernel->getAppDir(); |
|
|
|
|
|
|
177
|
|
|
} else { |
|
178
|
|
|
$path = $this->container->kernel->getMicroDir(); |
|
|
|
|
|
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
$cl = strtolower(dirname(str_replace('\\', '/', $calledClass))); |
|
182
|
|
|
$cl = substr($cl, strpos($cl, '/')); |
|
183
|
|
|
|
|
184
|
|
|
if ($this->asWidget) { |
|
185
|
|
|
$path .= $cl . '/views/' . $view . '.php'; |
|
186
|
|
|
} else { |
|
187
|
|
|
$className = str_replace('controller', '', |
|
188
|
|
|
strtolower(basename(str_replace('\\', '/', '/' . $this->path)))); |
|
189
|
|
|
$path .= dirname($cl) . '/views/' . $className . '/' . $view . '.php'; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
$path = str_replace('//', '/', $path); |
|
193
|
|
|
|
|
194
|
|
|
if (!file_exists($path)) { |
|
195
|
|
|
throw new Exception('View path `' . $path . '` not exists.'); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
return $path; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: