-
-
Notifications
You must be signed in to change notification settings - Fork 239
Expand file tree
/
Copy pathViewMaker.php
More file actions
322 lines (273 loc) · 9.42 KB
/
ViewMaker.php
File metadata and controls
322 lines (273 loc) · 9.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php namespace System\Traits;
use File;
use Lang;
use Block;
use SystemException;
use Throwable;
use Config;
/**
* View Maker Trait
* Adds view based methods to a class
*
* @package winter\wn-system-module
* @author Alexey Bobkov, Samuel Georges
*/
trait ViewMaker
{
/**
* @var array A list of variables to pass to the page.
*/
public $vars = [];
/**
* @var string|array Specifies a path to the views directory.
*/
protected $viewPath;
/**
* @var string Specifies a path to the layout directory.
*/
protected $layoutPath;
/**
* @var string Layout to use for the view.
*/
public $layout;
/**
* @var bool Prevents the use of a layout.
*/
public $suppressLayout = false;
/**
* Prepends a path on the available view path locations.
*
* @param array|string $path
* @return void
*/
public function prependViewPath(array|string $path): void
{
$this->viewPath = (array) $this->viewPath;
if (is_array($path)) {
$this->viewPath = array_merge($path, $this->viewPath);
} else {
array_unshift($this->viewPath, $path);
}
}
/**
* Append a path on the available view path locations.
*
* @param array|string $path
* @return void
*/
public function appendViewPath(array|string $path): void
{
$this->viewPath = (array) $this->viewPath;
if (is_array($path)) {
$this->viewPath = array_merge($this->viewPath, $path);
} else {
$this->viewPath[] = $path;
}
}
/**
* Prepends a path on the available view path locations.
*
* @deprecated Use prependViewPath()
*/
public function addViewPath(string|array $path): void
{
$this->prependViewPath($path);
}
/**
* Returns the active view path locations.
*/
public function getViewPaths(): array
{
return (array) $this->viewPath;
}
/**
* Render a partial file contents located in the views folder.
* @return mixed Partial contents or false if not throwing an exception.
*/
public function makePartial(string $partial, array $params = [], bool $throwException = true)
{
$notRealPath = realpath($partial) === false || is_dir($partial) === true;
if (!File::isPathSymbol($partial) && $notRealPath) {
$folder = strpos($partial, '/') !== false ? dirname($partial) . '/' : '';
$partial = $folder . '_' . strtolower(basename($partial));
}
$partialPath = $this->getViewPath($partial);
if (!File::exists($partialPath)) {
if ($throwException) {
throw new SystemException(Lang::get('backend::lang.partial.not_found_name', ['name' => $partialPath]));
}
return false;
}
return $this->makeFileContents($partialPath, $params);
}
/**
* Loads the specified view. Applies the layout if one is set.
* The view file must have the .php extension (or ".htm" for historical reasons) and be located in the views directory
*/
public function makeView(string $view): string
{
$viewPath = $this->getViewPath(strtolower($view));
$contents = $this->makeFileContents($viewPath);
return $this->makeViewContent($contents);
}
/**
* Renders supplied contents inside a layout.
*/
public function makeViewContent(string $contents, string $layout = null): string
{
if ($this->suppressLayout || $this->layout == '') {
return $contents;
}
// Append any undefined block content to the body block
Block::set('undefinedBlock', $contents);
Block::append('body', Block::get('undefinedBlock'));
return $this->makeLayout($layout);
}
/**
* Render a layout, defaulting to the layout propery specified on the class
* @return string|bool The layout contents, or false.
*/
public function makeLayout(string $name = null, array $params = [], bool $throwException = true): string|bool
{
$layout = $name ?? $this->layout;
if ($layout == '') {
return '';
}
$layoutPath = $this->getViewPath($layout, $this->layoutPath);
if (!File::exists($layoutPath)) {
if ($throwException) {
throw new SystemException(Lang::get('cms::lang.layout.not_found_name', ['name' => $layoutPath]));
}
return false;
}
return $this->makeFileContents($layoutPath, $params);
}
/**
* Renders a layout partial
*/
public function makeLayoutPartial(string $partial, array $params = []): string
{
if (!File::isLocalPath($partial) && !File::isPathSymbol($partial)) {
$folder = strpos($partial, '/') !== false ? dirname($partial) . '/' : '';
$partial = $folder . '_' . strtolower(basename($partial));
}
return $this->makeLayout($partial, $params);
}
/**
* Locates a file based on its definition. The file name can be prefixed with a
* symbol (~|$) to return in context of the application or plugin base path,
* otherwise it will be returned in context of this object view path.
*
* If the fileName cannot be found it will be returned unmodified.
*/
public function getViewPath(string $fileName, string|array $viewPaths = null): string
{
$input = $fileName;
$allowedExtensions = ['php', 'htm'];
if (!isset($this->viewPath)) {
$this->viewPath = $this->guessViewPath();
}
if (!$viewPaths) {
$viewPaths = $this->viewPath;
}
if (!is_array($viewPaths)) {
$viewPaths = [$viewPaths];
}
// Check the path for an extension
$ext = pathinfo($fileName, PATHINFO_EXTENSION);
if (!empty($ext)) {
if (!in_array($ext, $allowedExtensions)) {
throw new SystemException("$ext is not a valid View extension");
}
// Remove the extension from the fileName
$fileName = substr($fileName, 0, strrpos($fileName, '.'));
}
// Check if this a path relative to the view paths
foreach ($viewPaths as $path) {
$absolutePath = File::symbolizePath($path);
foreach ($allowedExtensions as $ext) {
$viewPath = $absolutePath . DIRECTORY_SEPARATOR . $fileName . ".$ext";
if (File::isFile($viewPath)) {
return $viewPath;
}
}
}
// Next, check if this is a local path reference
$absolutePath = File::symbolizePath($fileName);
foreach ($allowedExtensions as $ext) {
$viewPath = $absolutePath . ".$ext";
if (
File::isLocalPath($viewPath)
|| (
!Config::get('cms.restrictBaseDir', true)
&& realpath($viewPath) !== false
)
) {
return $viewPath;
}
}
return $input;
}
/**
* Includes a file path using output buffering, making the provided vars available.
*/
public function makeFileContents(string $filePath, array $extraParams = []): string
{
if (!strlen($filePath) ||
!File::isFile($filePath) ||
(!File::isLocalPath($filePath) && Config::get('cms.restrictBaseDir', true))
) {
return '';
}
if (!is_array($extraParams)) {
$extraParams = [];
}
$vars = array_merge($this->vars, $extraParams);
$obLevel = ob_get_level();
ob_start();
extract($vars);
// We'll evaluate the contents of the view inside a try/catch block so we can
// flush out any stray output that might get out before an error occurs or
// an exception is thrown. This prevents any partial views from leaking.
try {
include $filePath;
}
catch (Throwable $e) {
$this->handleViewException($e, $obLevel);
}
return ob_get_clean();
}
/**
* Handle a view exception.
*/
protected function handleViewException(Throwable $e, int $obLevel): void
{
while (ob_get_level() > $obLevel) {
ob_end_clean();
}
throw $e;
}
/**
* Guess the package path for the called class.
* @param string $suffix An extra path to attach to the end
* @param bool $isPublic Returns public path instead of an absolute one
*/
public function guessViewPath(string $suffix = '', bool $isPublic = false): ?string
{
$class = get_called_class();
return $this->guessViewPathFrom($class, $suffix, $isPublic);
}
/**
* Guess the package path from a specified class.
* @param string $class Class to guess path from.
* @param string $suffix An extra path to attach to the end
* @param bool $isPublic Returns public path instead of an absolute one
*/
public function guessViewPathFrom(string $class, string $suffix = '', bool $isPublic = false): ?string
{
$classFolder = strtolower(class_basename($class));
$classFile = realpath(dirname(File::fromClass($class)));
$guessedPath = $classFile ? $classFile . DIRECTORY_SEPARATOR . $classFolder . $suffix : null;
return $isPublic ? File::localToPublic($guessedPath) : $guessedPath;
}
}