GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 533da6...33cff4 )
by Robert
08:57
created

FragmentCache   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 5
dl 0
loc 177
ccs 48
cts 52
cp 0.9231
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 12 4
C run() 0 23 8
D getCachedContent() 0 33 9
A updateDynamicContent() 0 8 2
A calculateKey() 0 11 3
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\widgets;
9
10
use Yii;
11
use yii\base\Widget;
12
use yii\caching\Cache;
13
use yii\caching\Dependency;
14
use yii\di\Instance;
15
16
/**
17
 * FragmentCache is used by [[\yii\base\View]] to provide caching of page fragments.
18
 *
19
 * @property string|false $cachedContent The cached content. False is returned if valid content is not found
20
 * in the cache. This property is read-only.
21
 *
22
 * @author Qiang Xue <[email protected]>
23
 * @since 2.0
24
 */
25
class FragmentCache extends Widget
26
{
27
    /**
28
     * @var Cache|array|string the cache object or the application component ID of the cache object.
29
     * After the FragmentCache object is created, if you want to change this property,
30
     * you should only assign it with a cache object.
31
     * Starting from version 2.0.2, this can also be a configuration array for creating the object.
32
     */
33
    public $cache = 'cache';
34
    /**
35
     * @var int number of seconds that the data can remain valid in cache.
36
     * Use 0 to indicate that the cached data will never expire.
37
     */
38
    public $duration = 60;
39
    /**
40
     * @var array|Dependency the dependency that the cached content depends on.
41
     * This can be either a [[Dependency]] object or a configuration array for creating the dependency object.
42
     * For example,
43
     *
44
     * ```php
45
     * [
46
     *     'class' => 'yii\caching\DbDependency',
47
     *     'sql' => 'SELECT MAX(updated_at) FROM post',
48
     * ]
49
     * ```
50
     *
51
     * would make the output cache depends on the last modified time of all posts.
52
     * If any post has its modification time changed, the cached content would be invalidated.
53
     */
54
    public $dependency;
55
    /**
56
     * @var array list of factors that would cause the variation of the content being cached.
57
     * Each factor is a string representing a variation (e.g. the language, a GET parameter).
58
     * The following variation setting will cause the content to be cached in different versions
59
     * according to the current application language:
60
     *
61
     * ```php
62
     * [
63
     *     Yii::$app->language,
64
     * ]
65
     * ```
66
     */
67
    public $variations;
68
    /**
69
     * @var bool whether to enable the fragment cache. You may use this property to turn on and off
70
     * the fragment cache according to specific setting (e.g. enable fragment cache only for GET requests).
71
     */
72
    public $enabled = true;
73
    /**
74
     * @var array a list of placeholders for embedding dynamic contents. This property
75
     * is used internally to implement the content caching feature. Do not modify it.
76
     */
77
    public $dynamicPlaceholders;
78
79
80
    /**
81
     * Initializes the FragmentCache object.
82
     */
83 6
    public function init()
84
    {
85 6
        parent::init();
86
87 6
        $this->cache = $this->enabled ? Instance::ensure($this->cache, Cache::className()) : null;
88
89 6
        if ($this->cache instanceof Cache && $this->getCachedContent() === false) {
90 5
            $this->getView()->cacheStack[] = $this;
91 5
            ob_start();
92 5
            ob_implicit_flush(false);
93
        }
94 6
    }
95
96
    /**
97
     * Marks the end of content to be cached.
98
     * Content displayed before this method call and after [[init()]]
99
     * will be captured and saved in cache.
100
     * This method does nothing if valid content is already found in cache.
101
     */
102 6
    public function run()
103
    {
104 6
        if (($content = $this->getCachedContent()) !== false) {
105 4
            echo $content;
106 6
        } elseif ($this->cache instanceof Cache) {
107 5
            array_pop($this->getView()->cacheStack);
108
109 5
            $content = ob_get_clean();
110 5
            if ($content === false || $content === '') {
111
                return;
112
            }
113 5
            if (is_array($this->dependency)) {
114
                $this->dependency = Yii::createObject($this->dependency);
115
            }
116 5
            $data = [$content, $this->dynamicPlaceholders];
117 5
            $this->cache->set($this->calculateKey(), $data, $this->duration, $this->dependency);
118
119 5
            if (empty($this->getView()->cacheStack) && !empty($this->dynamicPlaceholders)) {
120 3
                $content = $this->updateDynamicContent($content, $this->dynamicPlaceholders);
121
            }
122 5
            echo $content;
123
        }
124 6
    }
125
126
    /**
127
     * @var string|bool the cached content. False if the content is not cached.
128
     */
129
    private $_content;
130
131
    /**
132
     * Returns the cached content if available.
133
     * @return string|false the cached content. False is returned if valid content is not found in the cache.
134
     */
135 6
    public function getCachedContent()
136
    {
137 6
        if ($this->_content !== null) {
138 6
            return $this->_content;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->_content; of type string|boolean adds the type boolean to the return on line 138 which is incompatible with the return type documented by yii\widgets\FragmentCache::getCachedContent of type string|false.
Loading history...
139
        }
140
141 6
        $this->_content = false;
142
143 6
        if (!($this->cache instanceof Cache)) {
144 2
            return $this->_content;
145
        }
146
147 5
        $key = $this->calculateKey();
148 5
        $data = $this->cache->get($key);
149 5
        if (!is_array($data) || count($data) !== 2) {
150 5
            return $this->_content;
151
        }
152
153 4
        list ($this->_content, $placeholders) = $data;
154 4
        if (!is_array($placeholders) || count($placeholders) === 0) {
155 1
            return $this->_content;
156
        }
157
158 3
        if (empty($this->getView()->cacheStack)) {
159
            // outermost cache: replace placeholder with dynamic content
160 3
            $this->_content = $this->updateDynamicContent($this->_content, $placeholders);
161
        }
162 3
        foreach ($placeholders as $name => $statements) {
163 3
            $this->getView()->addDynamicPlaceholder($name, $statements);
164
        }
165
166 3
        return $this->_content;
167
    }
168
169
    /**
170
     * Replaces placeholders in content by results of evaluated dynamic statements.
171
     *
172
     * @param string $content
173
     * @param array $placeholders
174
     * @return string final content
175
     */
176 3
    protected function updateDynamicContent($content, $placeholders)
177
    {
178 3
        foreach ($placeholders as $name => $statements) {
179 3
            $placeholders[$name] = $this->getView()->evaluateDynamicContent($statements);
180
        }
181
182 3
        return strtr($content, $placeholders);
183
    }
184
185
    /**
186
     * Generates a unique key used for storing the content in cache.
187
     * The key generated depends on both [[id]] and [[variations]].
188
     * @return mixed a valid cache key
189
     */
190 5
    protected function calculateKey()
191
    {
192 5
        $factors = [__CLASS__, $this->getId()];
193 5
        if (is_array($this->variations)) {
194
            foreach ($this->variations as $factor) {
195
                $factors[] = $factor;
196
            }
197
        }
198
199 5
        return $factors;
200
    }
201
}
202