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