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