1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Yiisoft\View; |
5
|
|
|
|
6
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use Yiisoft\I18n\Locale; |
9
|
|
|
use Yiisoft\View\Event\AfterRender; |
10
|
|
|
use Yiisoft\View\Event\BeforeRender; |
11
|
|
|
use Yiisoft\View\Event\PageBegin; |
12
|
|
|
use Yiisoft\View\Event\PageEnd; |
13
|
|
|
use Yiisoft\Widget\Block; |
14
|
|
|
use Yiisoft\Widget\ContentDecorator; |
15
|
|
|
use Yiisoft\Widget\FragmentCache; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* View represents a view object in the MVC pattern. |
19
|
|
|
* |
20
|
|
|
* View provides a set of methods (e.g. {@see render()}) for rendering purpose. |
21
|
|
|
* |
22
|
|
|
* For more details and usage information on View, see the [guide article on views](guide:structure-views). |
23
|
|
|
*/ |
24
|
|
|
class View implements DynamicContentAwareInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string $basePath view path |
28
|
|
|
*/ |
29
|
|
|
private $basePath; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array a list of named output blocks. The keys are the block names and the values are the corresponding block |
33
|
|
|
* content. You can call {@see beginBlock()} and {@see endBlock()} to capture small fragments of a view. |
34
|
|
|
* They can be later accessed somewhere else through this property. |
35
|
|
|
*/ |
36
|
|
|
public $blocks; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ViewContextInterface the context under which the {@see {renderFile()} method is being invoked. |
40
|
|
|
*/ |
41
|
|
|
public $context; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string the default view file extension. This will be appended to view file names if they don't have file |
45
|
|
|
* extensions. |
46
|
|
|
*/ |
47
|
|
|
public $defaultExtension = 'php'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var mixed custom parameters that are shared among view templates. |
51
|
|
|
*/ |
52
|
|
|
public $params = []; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var EventDispatcherInterface $eventDispatcher |
56
|
|
|
*/ |
57
|
|
|
protected $eventDispatcher; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var array a list of available renderers indexed by their corresponding supported file extensions. Each renderer |
61
|
|
|
* may be a view renderer object or the configuration for creating the renderer object. For example, the |
62
|
|
|
* following configuration enables the Twig view renderer: |
63
|
|
|
* |
64
|
|
|
* ```php |
65
|
|
|
* [ |
66
|
|
|
* 'twig' => ['__class' => \Yiisoft\Yii\Twig\ViewRenderer::class], |
67
|
|
|
* ] |
68
|
|
|
* ``` |
69
|
|
|
* |
70
|
|
|
* If no renderer is available for the given view file, the view file will be treated as a normal PHP and rendered |
71
|
|
|
* via PhpTemplateRenderer. |
72
|
|
|
*/ |
73
|
|
|
protected $renderers = []; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var Theme the theme object. |
77
|
|
|
*/ |
78
|
|
|
protected $theme; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var DynamicContentAwareInterface[] a list of currently active dynamic content class instances. |
82
|
|
|
*/ |
83
|
|
|
private $cacheStack = []; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var array a list of placeholders for embedding dynamic contents. |
87
|
|
|
*/ |
88
|
|
|
private $dynamicPlaceholders = []; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var string $language |
92
|
|
|
*/ |
93
|
|
|
private $language = 'en'; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @var LoggerInterface $logger |
97
|
|
|
*/ |
98
|
|
|
private $logger; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @var string $sourceLanguage |
102
|
|
|
*/ |
103
|
|
|
private $sourceLanguage = 'en'; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @var Locale source locale used to find localized view file. |
107
|
|
|
*/ |
108
|
|
|
private $sourceLocale; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @var array the view files currently being rendered. There may be multiple view files being |
112
|
|
|
* rendered at a moment because one view may be rendered within another. |
113
|
|
|
*/ |
114
|
|
|
private $viewFiles = []; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* View constructor. |
118
|
|
|
* |
119
|
|
|
* @param string $basePath |
120
|
|
|
* @param Theme $theme |
121
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
122
|
|
|
* @param LoggerInterface $logger |
123
|
|
|
*/ |
124
|
51 |
|
public function __construct(string $basePath, Theme $theme, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger) |
125
|
|
|
{ |
126
|
51 |
|
$this->basePath = $basePath; |
127
|
51 |
|
$this->theme = $theme; |
128
|
51 |
|
$this->eventDispatcher = $eventDispatcher; |
129
|
51 |
|
$this->logger = $logger; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get basePath. |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
public function getBasePath(): string |
138
|
|
|
{ |
139
|
|
|
return $this->basePath; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Set renderers. |
144
|
|
|
* |
145
|
|
|
* @param array $renderers |
146
|
|
|
* @return void |
147
|
|
|
*/ |
148
|
|
|
public function setRenderers(array $renderers): void |
149
|
|
|
{ |
150
|
|
|
$this->renderers = $renderers; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Set source language. |
155
|
|
|
* |
156
|
|
|
* @param string $language |
157
|
|
|
* @return void |
158
|
|
|
*/ |
159
|
|
|
public function setSourceLanguage(string $language): void |
160
|
|
|
{ |
161
|
|
|
$this->sourceLanguage = $language; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Set language. |
166
|
|
|
* |
167
|
|
|
* @param string $language |
168
|
|
|
* @return void |
169
|
|
|
*/ |
170
|
|
|
public function setLanguage(string $language): void |
171
|
|
|
{ |
172
|
|
|
$this->language = $language; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Renders a view. |
177
|
|
|
* |
178
|
|
|
* The view to be rendered can be specified in one of the following formats: |
179
|
|
|
* |
180
|
|
|
* - [path alias](guide:concept-aliases) (e.g. "@app/views/site/index"); |
181
|
|
|
* - absolute path within application (e.g. "//site/index"): the view name starts with double slashes. The actual |
182
|
|
|
* view file will be looked for under the [[Application::viewPath|view path]] of the application. |
183
|
|
|
* - absolute path within current module (e.g. "/site/index"): the view name starts with a single slash. The actual |
184
|
|
|
* view file will be looked for under the [[Module::viewPath|view path]] of the [[Controller::module|current module]]. |
185
|
|
|
* - relative view (e.g. "index"): the view name does not start with `@` or `/`. The corresponding view file will be |
186
|
|
|
* looked for under the {@see ViewContextInterface::getViewPath()|view path} of the view `$context`. |
187
|
|
|
* If `$context` is not given, it will be looked for under the directory containing the view currently |
188
|
|
|
* being rendered (i.e., this happens when rendering a view within another view). |
189
|
|
|
* |
190
|
|
|
* @param string $view the view name. |
191
|
|
|
* @param array $params the parameters (name-value pairs) that will be extracted and made available in the view |
192
|
|
|
* file. |
193
|
|
|
* @param object $context the context to be assigned to the view and can later be accessed via [[context]] in the |
194
|
|
|
* view. If the context implements {@see ViewContextInterface}, it may also be used to locate |
195
|
|
|
* the view file corresponding to a relative view name. |
196
|
|
|
* |
197
|
|
|
* @return string the rendering result |
198
|
|
|
* |
199
|
|
|
* @throws InvalidCallException if the view cannot be resolved. |
200
|
|
|
* @throws ViewNotFoundException if the view file does not exist. |
201
|
|
|
* |
202
|
|
|
* {@see renderFile()} |
203
|
|
|
*/ |
204
|
2 |
|
public function render($view, $params = [], $context = null) |
205
|
|
|
{ |
206
|
2 |
|
$viewFile = $this->findTemplateFile($view, $context); |
207
|
2 |
|
return $this->renderFile($viewFile, $params, $context); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Finds the view file based on the given view name. |
212
|
|
|
* |
213
|
|
|
* @param string $view the view name or the [path alias](guide:concept-aliases) of the view file. Please refer to |
214
|
|
|
* {@see render()} on how to specify this parameter. |
215
|
|
|
* @param object $context the context to be assigned to the view and can later be accessed via [[context]] in the |
216
|
|
|
* view. If the context implements [[ViewContextInterface]], it may also be used to locate the view |
217
|
|
|
* file corresponding to a relative view name. |
218
|
|
|
* |
219
|
|
|
* @throws InvalidCallException if a relative view name is given while there is no active context to determine the |
220
|
|
|
* corresponding view file. |
221
|
|
|
* |
222
|
|
|
* @return string the view file path. Note that the file may not exist. |
223
|
|
|
*/ |
224
|
2 |
|
protected function findTemplateFile(string $view, $context = null): string |
225
|
|
|
{ |
226
|
2 |
|
if (strncmp($view, '//', 2) === 0) { |
227
|
|
|
// path relative to basePath e.g. "//layouts/main" |
228
|
2 |
|
$file = $this->basePath . '/' . ltrim($view, '/'); |
229
|
1 |
|
} elseif ($context instanceof ViewContextInterface) { |
230
|
|
|
// path provided by context |
231
|
|
|
$file = $context->getViewPath() . '/' . $view; |
232
|
1 |
|
} elseif (($currentViewFile = $this->getRequestedViewFile()) !== false) { |
233
|
|
|
// path relative to currently rendered view |
234
|
1 |
|
$file = dirname($currentViewFile) . '/' . $view; |
|
|
|
|
235
|
|
|
} else { |
236
|
|
|
throw new \RuntimeException("Unable to resolve view file for view '$view': no active view context."); |
237
|
|
|
} |
238
|
|
|
|
239
|
2 |
|
if (pathinfo($file, PATHINFO_EXTENSION) !== '') { |
240
|
1 |
|
return $file; |
241
|
|
|
} |
242
|
|
|
|
243
|
1 |
|
$path = $file . '.' . $this->defaultExtension; |
244
|
|
|
|
245
|
1 |
|
if ($this->defaultExtension !== 'php' && !is_file($path)) { |
246
|
|
|
$path = $file . '.php'; |
247
|
|
|
} |
248
|
|
|
|
249
|
1 |
|
return $path; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Renders a view file. |
254
|
|
|
* |
255
|
|
|
* If {@see theme} is enabled (not null), it will try to render the themed version of the view file as long as it |
256
|
|
|
* is available. |
257
|
|
|
* |
258
|
|
|
* If {@see renderers|renderer} is enabled (not null), the method will use it to render the view file. Otherwise, |
259
|
|
|
* it will simply include the view file as a normal PHP file, capture its output and |
260
|
|
|
* return it as a string. |
261
|
|
|
* |
262
|
|
|
* @param string $viewFile the view file. This can be either an absolute file path or an alias of it. |
263
|
|
|
* @param array $params the parameters (name-value pairs) that will be extracted and made available in the view |
264
|
|
|
* file. |
265
|
|
|
* @param object $context the context that the view should use for rendering the view. If null, existing [[context]] |
266
|
|
|
* will be used. |
267
|
|
|
* |
268
|
|
|
* @throws ViewNotFoundException if the view file does not exist |
269
|
|
|
* |
270
|
|
|
* @return string the rendering result |
271
|
|
|
*/ |
272
|
33 |
|
public function renderFile(string $viewFile, array $params = [], object $context = null): string |
273
|
|
|
{ |
274
|
|
|
// TODO: these two match now |
275
|
33 |
|
$requestedFile = $viewFile; |
276
|
|
|
|
277
|
33 |
|
if ($this->theme !== null) { |
278
|
33 |
|
$viewFile = $this->theme->applyTo($viewFile); |
279
|
|
|
} |
280
|
33 |
|
if (is_file($viewFile)) { |
281
|
33 |
|
$viewFile = $this->localize($viewFile); |
282
|
|
|
} else { |
283
|
|
|
throw new ViewNotFoundException("The view file does not exist: $viewFile"); |
284
|
|
|
} |
285
|
|
|
|
286
|
33 |
|
$oldContext = $this->context; |
287
|
33 |
|
if ($context !== null) { |
288
|
|
|
$this->context = $context; |
289
|
|
|
} |
290
|
33 |
|
$output = ''; |
291
|
33 |
|
$this->viewFiles[] = [ |
292
|
33 |
|
'resolved' => $viewFile, |
293
|
33 |
|
'requested' => $requestedFile, |
294
|
|
|
]; |
295
|
|
|
|
296
|
33 |
|
if ($this->beforeRender($viewFile, $params)) { |
297
|
33 |
|
$this->logger->debug("Rendering view file: $viewFile"); |
298
|
33 |
|
$ext = pathinfo($viewFile, PATHINFO_EXTENSION); |
299
|
33 |
|
$renderer = $this->renderers[$ext] ?? new PhpTemplateRenderer(); |
300
|
33 |
|
$output = $renderer->render($this, $viewFile, $params); |
301
|
|
|
|
302
|
33 |
|
$this->afterRender($viewFile, $params, $output); |
303
|
|
|
} |
304
|
|
|
|
305
|
33 |
|
array_pop($this->viewFiles); |
306
|
33 |
|
$this->context = $oldContext; |
307
|
|
|
|
308
|
33 |
|
return $output; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Returns the localized version of a specified file. |
313
|
|
|
* |
314
|
|
|
* The searching is based on the specified language code. In particular, a file with the same name will be looked |
315
|
|
|
* for under the subdirectory whose name is the same as the language code. For example, given the file |
316
|
|
|
* "path/to/view.php" and language code "zh-CN", the localized file will be looked for as path/to/zh-CN/view.php". |
317
|
|
|
* If the file is not found, it will try a fallback with just a language code that is "zh" i.e. "path/to/zh/view.php". |
318
|
|
|
* If it is not found as well the original file will be returned. |
319
|
|
|
* |
320
|
|
|
* If the target and the source language codes are the same, the original file will be returned. |
321
|
|
|
* |
322
|
|
|
* @param string $file the original file |
323
|
|
|
* @param string|null $language the target language that the file should be localized to. |
324
|
|
|
* @param string|null $sourceLanguage the language that the original file is in. |
325
|
|
|
* |
326
|
|
|
* @return string the matching localized file, or the original file if the localized version is not found. |
327
|
|
|
* If the target and the source language codes are the same, the original file will be returned. |
328
|
|
|
*/ |
329
|
34 |
|
public function localize(string $file, ?string $language = null, ?string $sourceLanguage = null): string |
330
|
|
|
{ |
331
|
34 |
|
$language = $language ?? $this->language; |
332
|
34 |
|
$sourceLanguage = $sourceLanguage ?? $this->sourceLanguage; |
333
|
|
|
|
334
|
34 |
|
if ($language === $sourceLanguage) { |
335
|
34 |
|
return $file; |
336
|
|
|
} |
337
|
1 |
|
$desiredFile = dirname($file) . DIRECTORY_SEPARATOR . $language . DIRECTORY_SEPARATOR . basename($file); |
338
|
1 |
|
if (is_file($desiredFile)) { |
339
|
1 |
|
return $desiredFile; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
$language = substr($language, 0, 2); |
343
|
|
|
if ($language === $sourceLanguage) { |
344
|
|
|
return $file; |
345
|
|
|
} |
346
|
|
|
$desiredFile = dirname($file) . DIRECTORY_SEPARATOR . $language . DIRECTORY_SEPARATOR . basename($file); |
347
|
|
|
|
348
|
|
|
return is_file($desiredFile) ? $desiredFile : $file; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @return string|bool the view file currently being rendered. False if no view file is being rendered. |
353
|
|
|
*/ |
354
|
31 |
|
public function getViewFile() |
355
|
|
|
{ |
356
|
31 |
|
return empty($this->viewFiles) ? false : end($this->viewFiles)['resolved']; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* @return string|bool the requested view currently being rendered. False if no view file is being rendered. |
361
|
|
|
*/ |
362
|
1 |
|
protected function getRequestedViewFile() |
363
|
|
|
{ |
364
|
1 |
|
return empty($this->viewFiles) ? false : end($this->viewFiles)['requested']; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* This method is invoked right before {@see renderFile()} renders a view file. |
369
|
|
|
* |
370
|
|
|
* The default implementation will trigger the {@see BeforeRender()} event. If you override this method, make sure |
371
|
|
|
* you call the parent implementation first. |
372
|
|
|
* |
373
|
|
|
* @param string $viewFile the view file to be rendered. |
374
|
|
|
* @param array $params the parameter array passed to the {@see render()} method. |
375
|
|
|
* |
376
|
|
|
* @return bool whether to continue rendering the view file. |
377
|
|
|
*/ |
378
|
33 |
|
public function beforeRender(string $viewFile, array $params): bool |
379
|
|
|
{ |
380
|
33 |
|
$event = new BeforeRender($viewFile, $params); |
381
|
33 |
|
$this->eventDispatcher->dispatch($event); |
382
|
|
|
|
383
|
33 |
|
return !$event->isPropagationStopped(); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* This method is invoked right after {@see renderFile()} renders a view file. |
388
|
|
|
* |
389
|
|
|
* The default implementation will trigger the {@see AfterRender} event. If you override this method, make sure you |
390
|
|
|
* call the parent implementation first. |
391
|
|
|
* |
392
|
|
|
* @param string $viewFile the view file being rendered. |
393
|
|
|
* @param array $params the parameter array passed to the {@see render()} method. |
394
|
|
|
* @param string $output the rendering result of the view file. Updates to this parameter |
395
|
|
|
* will be passed back and returned by {@see renderFile()}. |
396
|
|
|
*/ |
397
|
33 |
|
public function afterRender(string $viewFile, array $params, &$output): string |
398
|
|
|
{ |
399
|
33 |
|
$event = new AfterRender($viewFile, $params, $output); |
400
|
33 |
|
$this->eventDispatcher->dispatch($event); |
401
|
|
|
|
402
|
33 |
|
return $event->getResult(); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* Renders dynamic content returned by the given PHP statements. |
407
|
|
|
* |
408
|
|
|
* This method is mainly used together with content caching (fragment caching and page caching) when some portions |
409
|
|
|
* of the content (called *dynamic content*) should not be cached. The dynamic content must be returned by some PHP |
410
|
|
|
* statements. You can optionally pass additional parameters that will be available as variables in the PHP |
411
|
|
|
* statement:. |
412
|
|
|
* |
413
|
|
|
* ```php |
414
|
|
|
* <?= $this->renderDynamic('return foo($myVar);', [ |
415
|
|
|
* 'myVar' => $model->getMyComplexVar(), |
416
|
|
|
* ]) ?> |
417
|
|
|
* ``` |
418
|
|
|
* |
419
|
|
|
* @param string $statements the PHP statements for generating the dynamic content. |
420
|
|
|
* @param array $params the parameters (name-value pairs) that will be extracted and made |
421
|
|
|
* available in the $statement context. The parameters will be stored in the cache and be reused |
422
|
|
|
* each time $statement is executed. You should make sure, that these are safely serializable. |
423
|
|
|
* |
424
|
|
|
* @return string the placeholder of the dynamic content, or the dynamic content if there is no active content |
425
|
|
|
* cache currently. |
426
|
|
|
*/ |
427
|
|
|
public function renderDynamic(string $statements, array $params = []): string |
428
|
|
|
{ |
429
|
|
|
if (!empty($params)) { |
430
|
|
|
$statements = 'extract(unserialize(\'' . str_replace(['\\', '\''], ['\\\\', '\\\''], serialize($params)) . '\'));' . $statements; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
if (!empty($this->cacheStack)) { |
434
|
|
|
$n = count($this->dynamicPlaceholders); |
435
|
|
|
$placeholder = "<![CDATA[YII-DYNAMIC-$n]]>"; |
436
|
|
|
$this->addDynamicPlaceholder($placeholder, $statements); |
437
|
|
|
|
438
|
|
|
return $placeholder; |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
return $this->evaluateDynamicContent($statements); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* Get source locale. |
446
|
|
|
* |
447
|
|
|
* @return Locale |
448
|
|
|
*/ |
449
|
|
|
public function getSourceLocale(): Locale |
450
|
|
|
{ |
451
|
|
|
if ($this->sourceLocale === null) { |
452
|
|
|
$this->sourceLocale = Locale::create('en-US'); |
|
|
|
|
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
return $this->sourceLocale; |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* Set source locale. |
460
|
|
|
* |
461
|
|
|
* @param string $locale |
462
|
|
|
* @return self |
463
|
|
|
*/ |
464
|
|
|
public function setSourceLocale(string $locale): self |
465
|
|
|
{ |
466
|
|
|
$this->sourceLocale = Locale::create($locale); |
467
|
|
|
|
468
|
|
|
return $this; |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
public function getDynamicPlaceholders(): array |
472
|
|
|
{ |
473
|
|
|
return $this->dynamicPlaceholders; |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
public function setDynamicPlaceholders(array $placeholders): void |
477
|
|
|
{ |
478
|
|
|
$this->dynamicPlaceholders = $placeholders; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
public function addDynamicPlaceholder(string $placeholder, string $statements): void |
482
|
|
|
{ |
483
|
|
|
foreach ($this->cacheStack as $cache) { |
484
|
|
|
$cache->addDynamicPlaceholder($placeholder, $statements); |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
$this->dynamicPlaceholders[$placeholder] = $statements; |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
/** |
491
|
|
|
* Evaluates the given PHP statements. |
492
|
|
|
* |
493
|
|
|
* This method is mainly used internally to implement dynamic content feature. |
494
|
|
|
* |
495
|
|
|
* @param string $statements the PHP statements to be evaluated. |
496
|
|
|
* |
497
|
|
|
* @return mixed the return value of the PHP statements. |
498
|
|
|
*/ |
499
|
|
|
public function evaluateDynamicContent(string $statements) |
500
|
|
|
{ |
501
|
|
|
return eval($statements); |
|
|
|
|
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
/** |
505
|
|
|
* Returns a list of currently active dynamic content class instances. |
506
|
|
|
* |
507
|
|
|
* @return DynamicContentAwareInterface[] class instances supporting dynamic contents. |
508
|
|
|
*/ |
509
|
|
|
public function getDynamicContents() |
510
|
|
|
{ |
511
|
|
|
return $this->cacheStack; |
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
/** |
515
|
|
|
* Adds a class instance supporting dynamic contents to the end of a list of currently active dynamic content class |
516
|
|
|
* instances. |
517
|
|
|
* |
518
|
|
|
* @param DynamicContentAwareInterface $instance class instance supporting dynamic contents. |
519
|
|
|
* |
520
|
|
|
* @return void |
521
|
|
|
*/ |
522
|
|
|
public function pushDynamicContent(DynamicContentAwareInterface $instance): void |
523
|
|
|
{ |
524
|
|
|
$this->cacheStack[] = $instance; |
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
/** |
528
|
|
|
* Removes a last class instance supporting dynamic contents from a list of currently active dynamic content class |
529
|
|
|
* instances. |
530
|
|
|
* |
531
|
|
|
* @return void |
532
|
|
|
*/ |
533
|
|
|
public function popDynamicContent(): void |
534
|
|
|
{ |
535
|
|
|
array_pop($this->cacheStack); |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
/** |
539
|
|
|
* Begins recording a block. |
540
|
|
|
* |
541
|
|
|
* This method is a shortcut to beginning {@see Block}. |
542
|
|
|
* |
543
|
|
|
* @param string $id the block ID. |
544
|
|
|
* @param bool $renderInPlace whether to render the block content in place. |
545
|
|
|
* Defaults to false, meaning the captured block will not be displayed. |
546
|
|
|
* |
547
|
|
|
* @return Block the Block widget instance |
548
|
|
|
*/ |
549
|
|
|
public function beginBlock($id, $renderInPlace = false): Block |
550
|
|
|
{ |
551
|
|
|
return Block::begin([ |
|
|
|
|
552
|
|
|
'id' => $id, |
553
|
|
|
'renderInPlace' => $renderInPlace, |
554
|
|
|
'view' => $this, |
555
|
|
|
]); |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
/** |
559
|
|
|
* Ends recording a block. |
560
|
|
|
* |
561
|
|
|
* @return void |
562
|
|
|
*/ |
563
|
|
|
public function endBlock(): void |
564
|
|
|
{ |
565
|
|
|
Block::end(); |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* Begins the rendering of content that is to be decorated by the specified view. |
570
|
|
|
* |
571
|
|
|
* This method can be used to implement nested layout. For example, a layout can be embedded in another layout file |
572
|
|
|
* specified as '@app/views/layouts/base.php' like the following: |
573
|
|
|
* |
574
|
|
|
* ```php |
575
|
|
|
* <?php $this->beginContent('@app/views/layouts/base.php'); ?> |
576
|
|
|
* //...layout content here... |
577
|
|
|
* <?php $this->endContent(); ?> |
578
|
|
|
* ``` |
579
|
|
|
* |
580
|
|
|
* @param string $viewFile the view file that will be used to decorate the content enclosed by this widget. This can |
581
|
|
|
* be specified as either the view file path or [path alias](guide:concept-aliases). |
582
|
|
|
* @param array $params the variables (name => value) to be extracted and made available in the decorative view. |
583
|
|
|
* |
584
|
|
|
* @return ContentDecorator the ContentDecorator widget instance |
585
|
|
|
* |
586
|
|
|
* {@see ContentDecorator} |
587
|
|
|
*/ |
588
|
|
|
public function beginContent(string $viewFile, array $params = []): ContentDecorator |
589
|
|
|
{ |
590
|
|
|
return ContentDecorator::begin([ |
|
|
|
|
591
|
|
|
'viewFile' => $viewFile, |
592
|
|
|
'params' => $params, |
593
|
|
|
'view' => $this, |
594
|
|
|
]); |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
/** |
598
|
|
|
* Ends the rendering of content. |
599
|
|
|
* |
600
|
|
|
* @return void |
601
|
|
|
*/ |
602
|
|
|
public function endContent(): void |
603
|
|
|
{ |
604
|
|
|
ContentDecorator::end(); |
605
|
|
|
} |
606
|
|
|
|
607
|
|
|
/** |
608
|
|
|
* Begins fragment caching. |
609
|
|
|
* |
610
|
|
|
* This method will display cached content if it is available. If not, it will start caching and would expect an |
611
|
|
|
* {@see endCache()} call to end the cache and save the content into cache. A typical usage of fragment caching is |
612
|
|
|
* as follows, |
613
|
|
|
* |
614
|
|
|
* ```php |
615
|
|
|
* if ($this->beginCache($id)) { |
616
|
|
|
* // ...generate content here |
617
|
|
|
* $this->endCache(); |
618
|
|
|
* } |
619
|
|
|
* ``` |
620
|
|
|
* |
621
|
|
|
* @param string $id a unique ID identifying the fragment to be cached. |
622
|
|
|
* @param array $properties initial property values for {@see FragmentCache} |
623
|
|
|
* |
624
|
|
|
* @return bool whether you should generate the content for caching. |
625
|
|
|
* False if the cached version is available. |
626
|
|
|
*/ |
627
|
|
|
public function beginCache(string $id, array $properties = []): bool |
628
|
|
|
{ |
629
|
|
|
$properties['id'] = $id; |
630
|
|
|
$properties['view'] = $this; |
631
|
|
|
$cache = FragmentCache::begin($properties); |
632
|
|
|
if ($cache->getCachedContent() !== false) { |
|
|
|
|
633
|
|
|
$this->endCache(); |
634
|
|
|
|
635
|
|
|
return false; |
636
|
|
|
} |
637
|
|
|
|
638
|
|
|
return true; |
639
|
|
|
} |
640
|
|
|
|
641
|
|
|
/** |
642
|
|
|
* Ends fragment caching. |
643
|
|
|
* |
644
|
|
|
* @return void |
645
|
|
|
*/ |
646
|
|
|
public function endCache(): void |
647
|
|
|
{ |
648
|
|
|
FragmentCache::end(); |
649
|
|
|
} |
650
|
|
|
|
651
|
|
|
/** |
652
|
|
|
* Marks the beginning of a page. |
653
|
|
|
* |
654
|
|
|
* @return void |
655
|
|
|
*/ |
656
|
31 |
|
public function beginPage(): void |
657
|
|
|
{ |
658
|
31 |
|
ob_start(); |
659
|
31 |
|
ob_implicit_flush(0); |
660
|
|
|
|
661
|
31 |
|
$this->eventDispatcher->dispatch(new PageBegin($this->getViewFile())); |
|
|
|
|
662
|
|
|
} |
663
|
|
|
|
664
|
|
|
/** |
665
|
|
|
* Marks the ending of a page. |
666
|
|
|
* |
667
|
|
|
* @return void |
668
|
|
|
*/ |
669
|
|
|
public function endPage(): void |
670
|
|
|
{ |
671
|
|
|
$this->eventDispatcher->dispatch(new PageEnd($this->getViewFile())); |
|
|
|
|
672
|
|
|
ob_end_flush(); |
673
|
|
|
} |
674
|
|
|
} |
675
|
|
|
|