Passed
Push — master ( 28cbf6...ac1a5a )
by
unknown
05:50 queued 19s
created

Dependency::getHasChanged()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @link https://www.yiiframework.com/
5
 * @copyright Copyright (c) 2008 Yii Software LLC
6
 * @license https://www.yiiframework.com/license/
7
 */
8
9
namespace yii\caching;
10
11
/**
12
 * Dependency is the base class for cache dependency classes.
13
 *
14
 * Child classes should override its [[generateDependencyData()]] for generating
15
 * the actual dependency data.
16
 *
17
 * For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
18
 *
19
 * @author Qiang Xue <[email protected]>
20
 * @since 2.0
21
 */
22
abstract class Dependency extends \yii\base\BaseObject
23
{
24
    /**
25
     * @var mixed the dependency data that is saved in cache and later is compared with the
26
     * latest dependency data.
27
     */
28
    public $data;
29
    /**
30
     * @var bool whether this dependency is reusable or not. True value means that dependent
31
     * data for this cache dependency will be generated only once per request. This allows you
32
     * to use the same cache dependency for multiple separate cache calls while generating the same
33
     * page without an overhead of re-evaluating dependency data each time. Defaults to false.
34
     */
35
    public $reusable = false;
36
37
    /**
38
     * @var array static storage of cached data for reusable dependencies.
39
     */
40
    private static $_reusableData = [];
41
42
43
    /**
44
     * Evaluates the dependency by generating and saving the data related with dependency.
45
     * This method is invoked by cache before writing data into it.
46
     * @param CacheInterface $cache the cache component that is currently evaluating this dependency
47
     */
48 38
    public function evaluateDependency($cache)
49
    {
50 38
        if ($this->reusable) {
51 1
            $hash = $this->generateReusableHash();
52 1
            if (!array_key_exists($hash, self::$_reusableData)) {
53 1
                self::$_reusableData[$hash] = $this->generateDependencyData($cache);
54
            }
55 1
            $this->data = self::$_reusableData[$hash];
56
        } else {
57 37
            $this->data = $this->generateDependencyData($cache);
58
        }
59
    }
60
61
    /**
62
     * Returns a value indicating whether the dependency has changed.
63
     * @deprecated since version 2.0.11. Will be removed in version 2.1. Use [[isChanged()]] instead.
64
     * @param CacheInterface $cache the cache component that is currently evaluating this dependency
65
     * @return bool whether the dependency has changed.
66
     */
67
    public function getHasChanged($cache)
68
    {
69
        return $this->isChanged($cache);
70
    }
71
72
    /**
73
     * Checks whether the dependency is changed.
74
     * @param CacheInterface $cache the cache component that is currently evaluating this dependency
75
     * @return bool whether the dependency has changed.
76
     * @since 2.0.11
77
     */
78 8
    public function isChanged($cache)
79
    {
80 8
        if ($this->reusable) {
81 1
            $hash = $this->generateReusableHash();
82 1
            if (!array_key_exists($hash, self::$_reusableData)) {
83 1
                self::$_reusableData[$hash] = $this->generateDependencyData($cache);
84
            }
85 1
            $data = self::$_reusableData[$hash];
86
        } else {
87 7
            $data = $this->generateDependencyData($cache);
88
        }
89
90 8
        return $data !== $this->data;
91
    }
92
93
    /**
94
     * Resets all cached data for reusable dependencies.
95
     */
96 1
    public static function resetReusableData()
97
    {
98 1
        self::$_reusableData = [];
99
    }
100
101
    /**
102
     * Generates a unique hash that can be used for retrieving reusable dependency data.
103
     *
104
     * @return string a unique hash value for this cache dependency.
105
     * @see reusable
106
     */
107 2
    protected function generateReusableHash()
108
    {
109 2
        $clone = clone $this;
110 2
        $clone->data = null; // https://github.com/yiisoft/yii2/issues/3052
111
112
        try {
113 2
            $serialized = serialize($clone);
114 1
        } catch (\Exception $e) {
115
            // unserializable properties are nulled
116 1
            foreach ($clone as $name => $value) {
117 1
                if (is_object($value) && $value instanceof \Closure) {
118 1
                    $clone->{$name} = null;
119
                }
120
            }
121 1
            $serialized = serialize($clone);
122
        }
123
124 2
        return sha1($serialized);
125
    }
126
127
    /**
128
     * Generates the data needed to determine if dependency is changed.
129
     * Derived classes should override this method to generate the actual dependency data.
130
     * @param CacheInterface $cache the cache component that is currently evaluating this dependency
131
     * @return mixed the data needed to determine if dependency has been changed.
132
     */
133
    abstract protected function generateDependencyData($cache);
134
}
135