Completed
Push — master ( c6ea3f...5e16c6 )
by Dmitry
12:35
created

DynamicContentAwareTrait::updateDynamicContent()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 12
cts 12
cp 1
rs 6.9811
c 0
b 0
f 0
cc 7
eloc 12
nc 5
nop 3
crap 7
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\base;
9
10
/**
11
 * DynamicContentAwareTrait implements common methods for classes
12
 * which support a [[View]] dynamic content feature.
13
 *
14
 * @author Sergey Makinen <[email protected]>
15
 * @since 2.0.14
16
 */
17
trait DynamicContentAwareTrait
18
{
19
    /**
20
     * @var string[] a list of placeholders for dynamic content
21
     */
22
    private $_dynamicPlaceholders;
23
24
    /**
25
     * Returns the view object that can be used to render views or view files using dynamic contents.
26
     * @return View the view object that can be used to render views or view files.
27
     */
28
    abstract protected function getView();
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 21
    public function getDynamicPlaceholders()
34
    {
35 21
        return $this->_dynamicPlaceholders;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function setDynamicPlaceholders($placeholders)
42
    {
43
        $this->_dynamicPlaceholders = $placeholders;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 16
    public function addDynamicPlaceholder($name, $statements)
50
    {
51 16
        $this->_dynamicPlaceholders[$name] = $statements;
52 16
    }
53
54
    /**
55
     * Replaces placeholders in $content with results of evaluated dynamic statements.
56
     * @param string $content content to be parsed.
57
     * @param string[] $placeholders placeholders and their values.
58
     * @param bool $isRestoredFromCache whether content is going to be restored from cache.
59
     * @return string final content.
60
     */
61 21
    protected function updateDynamicContent($content, $placeholders, $isRestoredFromCache = false)
62
    {
63 21
        if (empty($placeholders) || !is_array($placeholders)) {
64 5
            return $content;
65
        }
66
67 16
        if (count($this->getView()->getDynamicContents()) === 0) {
68
            // outermost cache: replace placeholder with dynamic content
69 16
            foreach ($placeholders as $name => $statements) {
70 16
                $placeholders[$name] = $this->getView()->evaluateDynamicContent($statements);
71
            }
72 16
            $content = strtr($content, $placeholders);
73
        }
74 16
        if ($isRestoredFromCache) {
75 15
            $view = $this->getView();
76 15
            foreach ($placeholders as $name => $statements) {
77 15
                $view->addDynamicPlaceholder($name, $statements);
78
            }
79
        }
80
81 16
        return $content;
82
    }
83
}
84