forked from yiisoft/yii2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicContentAwareTrait.php
83 lines (74 loc) · 2.37 KB
/
DynamicContentAwareTrait.php
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
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
/**
* DynamicContentAwareTrait implements common methods for classes
* which support a [[View]] dynamic content feature.
*
* @author Sergey Makinen <[email protected]>
* @since 2.0.14
*/
trait DynamicContentAwareTrait
{
/**
* @var string[] a list of placeholders for dynamic content
*/
private $_dynamicPlaceholders;
/**
* Returns the view object that can be used to render views or view files using dynamic contents.
* @return View the view object that can be used to render views or view files.
*/
abstract protected function getView();
/**
* {@inheritdoc}
*/
public function getDynamicPlaceholders()
{
return $this->_dynamicPlaceholders;
}
/**
* {@inheritdoc}
*/
public function setDynamicPlaceholders($placeholders)
{
$this->_dynamicPlaceholders = $placeholders;
}
/**
* {@inheritdoc}
*/
public function addDynamicPlaceholder($name, $statements)
{
$this->_dynamicPlaceholders[$name] = $statements;
}
/**
* Replaces placeholders in $content with results of evaluated dynamic statements.
* @param string $content content to be parsed.
* @param string[] $placeholders placeholders and their values.
* @param bool $isRestoredFromCache whether content is going to be restored from cache.
* @return string final content.
*/
protected function updateDynamicContent($content, $placeholders, $isRestoredFromCache = false)
{
if (empty($placeholders) || !is_array($placeholders)) {
return $content;
}
if (count($this->getView()->getDynamicContents()) === 0) {
// outermost cache: replace placeholder with dynamic content
foreach ($placeholders as $name => $statements) {
$placeholders[$name] = $this->getView()->evaluateDynamicContent($statements);
}
$content = strtr($content, $placeholders);
}
if ($isRestoredFromCache) {
$view = $this->getView();
foreach ($placeholders as $name => $statements) {
$view->addDynamicPlaceholder($name, $statements);
}
}
return $content;
}
}