1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\View; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* DynamicContentAwareTrait implements common methods for classes |
7
|
|
|
* which support a [[View]] dynamic content feature. |
8
|
|
|
*/ |
9
|
|
|
trait DynamicContentAwareTrait |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string[] a list of placeholders for dynamic content |
13
|
|
|
*/ |
14
|
|
|
private $dynamicPlaceholders; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Returns the view object that can be used to render views or view files using dynamic contents. |
18
|
|
|
* |
19
|
|
|
* @return View the view object that can be used to render views or view files. |
20
|
|
|
*/ |
21
|
|
|
abstract protected function getView(): View; |
22
|
|
|
|
23
|
|
|
public function getDynamicPlaceholders(): array |
24
|
|
|
{ |
25
|
|
|
return $this->dynamicPlaceholders; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function setDynamicPlaceholders(array $placeholders): void |
29
|
|
|
{ |
30
|
|
|
$this->dynamicPlaceholders = $placeholders; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function addDynamicPlaceholder(string $name, string $statements): void |
34
|
|
|
{ |
35
|
|
|
$this->dynamicPlaceholders[$name] = $statements; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Replaces placeholders in $content with results of evaluated dynamic statements. |
40
|
|
|
* |
41
|
|
|
* @param string $content content to be parsed. |
42
|
|
|
* @param string[] $placeholders placeholders and their values. |
43
|
|
|
* @param bool $isRestoredFromCache whether content is going to be restored from cache. |
44
|
|
|
* |
45
|
|
|
* @return string final content. |
46
|
|
|
*/ |
47
|
|
|
protected function updateDynamicContent(string $content, array $placeholders, bool $isRestoredFromCache = false): string |
48
|
|
|
{ |
49
|
|
|
if ($placeholders === []) { |
50
|
|
|
return $content; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (count($this->getView()->getDynamicContents()) === 0) { |
54
|
|
|
// outermost cache: replace placeholder with dynamic content |
55
|
|
|
foreach ($placeholders as $name => $statements) { |
56
|
|
|
$placeholders[$name] = $this->getView()->evaluateDynamicContent($statements); |
57
|
|
|
} |
58
|
|
|
$content = strtr($content, $placeholders); |
59
|
|
|
} |
60
|
|
|
if ($isRestoredFromCache) { |
61
|
|
|
$view = $this->getView(); |
62
|
|
|
foreach ($placeholders as $name => $statements) { |
63
|
|
|
$view->addDynamicPlaceholder($name, $statements); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $content; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|