Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 24 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
DependencyWrapper | |
0.00% |
0 / 24 |
|
0.00% |
0 / 6 |
210 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
isExpired | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
initialiseDeps | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
getValue | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
storeToCache | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getValueFromCache | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | /** |
22 | * Store an arbitrary value whilst representing several CacheDependency objects as one. |
23 | * |
24 | * You should typically only use DependencyWrapper::getValueFromCache(), |
25 | * rather than instantiating one of these objects directly. |
26 | * |
27 | * @ingroup Language |
28 | */ |
29 | class DependencyWrapper { |
30 | private $value; |
31 | /** @var CacheDependency[] */ |
32 | private $deps; |
33 | |
34 | /** |
35 | * @param mixed $value The user-supplied value |
36 | * @param CacheDependency|CacheDependency[] $deps A dependency or dependency |
37 | * array. All dependencies must be objects implementing CacheDependency. |
38 | */ |
39 | public function __construct( $value = false, $deps = [] ) { |
40 | $this->value = $value; |
41 | |
42 | if ( !is_array( $deps ) ) { |
43 | $deps = [ $deps ]; |
44 | } |
45 | |
46 | $this->deps = $deps; |
47 | } |
48 | |
49 | /** |
50 | * Returns true if any of the dependencies have expired |
51 | * |
52 | * @return bool |
53 | */ |
54 | public function isExpired() { |
55 | foreach ( $this->deps as $dep ) { |
56 | if ( $dep->isExpired() ) { |
57 | return true; |
58 | } |
59 | } |
60 | |
61 | return false; |
62 | } |
63 | |
64 | /** |
65 | * Initialise dependency values in preparation for storing. This must be |
66 | * called before serialization. |
67 | */ |
68 | public function initialiseDeps() { |
69 | foreach ( $this->deps as $dep ) { |
70 | $dep->loadDependencyValues(); |
71 | } |
72 | } |
73 | |
74 | /** |
75 | * Get the user-defined value |
76 | * @return bool|mixed |
77 | */ |
78 | public function getValue() { |
79 | return $this->value; |
80 | } |
81 | |
82 | /** |
83 | * Store the wrapper to a cache |
84 | * |
85 | * @param BagOStuff $cache |
86 | * @param string $key |
87 | * @param int $expiry |
88 | */ |
89 | public function storeToCache( $cache, $key, $expiry = 0 ) { |
90 | $this->initialiseDeps(); |
91 | $cache->set( $key, $this, $expiry ); |
92 | } |
93 | |
94 | /** |
95 | * Attempt to get a value from the cache. If the value is expired or missing, |
96 | * it will be generated with the callback function (if present), and the newly |
97 | * calculated value will be stored to the cache in a wrapper. |
98 | * |
99 | * @param BagOStuff $cache |
100 | * @param string $key The cache key |
101 | * @param int $expiry The expiry timestamp or interval in seconds |
102 | * @param callable|false $callback The callback for generating the value, or false |
103 | * @param array $callbackParams The function parameters for the callback |
104 | * @param array $deps The dependencies to store on a cache miss. Note: these |
105 | * are not the dependencies used on a cache hit! Cache hits use the stored |
106 | * dependency array. |
107 | * |
108 | * @return mixed The value, or null if it was not present in the cache and no |
109 | * callback was defined. |
110 | */ |
111 | public static function getValueFromCache( $cache, $key, $expiry = 0, $callback = false, |
112 | $callbackParams = [], $deps = [] |
113 | ) { |
114 | $obj = $cache->get( $key ); |
115 | |
116 | if ( is_object( $obj ) && $obj instanceof DependencyWrapper && !$obj->isExpired() ) { |
117 | $value = $obj->value; |
118 | } elseif ( $callback ) { |
119 | $value = $callback( ...$callbackParams ); |
120 | # Cache the newly-generated value |
121 | $wrapper = new DependencyWrapper( $value, $deps ); |
122 | $wrapper->storeToCache( $cache, $key, $expiry ); |
123 | } else { |
124 | $value = null; |
125 | } |
126 | |
127 | return $value; |
128 | } |
129 | } |