Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
DependencyWrapper
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 6
210
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 isExpired
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 initialiseDeps
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 getValue
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 storeToCache
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getValueFromCache
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace MediaWiki\Language\Dependency;
8
9use Wikimedia\ObjectCache\BagOStuff;
10
11/**
12 * Store an arbitrary value whilst representing several CacheDependency objects as one.
13 *
14 * You should typically only use DependencyWrapper::getValueFromCache(),
15 * rather than instantiating one of these objects directly.
16 *
17 * @ingroup Language
18 */
19class DependencyWrapper {
20    /** @var mixed */
21    private $value;
22    /** @var CacheDependency[] */
23    private $deps;
24
25    /**
26     * @param mixed $value The user-supplied value
27     * @param CacheDependency|CacheDependency[] $deps A dependency or dependency
28     *   array. All dependencies must be objects implementing CacheDependency.
29     */
30    public function __construct( $value = false, $deps = [] ) {
31        $this->value = $value;
32
33        if ( !is_array( $deps ) ) {
34            $deps = [ $deps ];
35        }
36
37        $this->deps = $deps;
38    }
39
40    /**
41     * @param callback|null $callback Optional callback which will be called with a string
42     *    describing the reason why isExpired() is returning true.
43     * Returns true if any of the dependencies have expired
44     *
45     * @return bool
46     */
47    public function isExpired( $callback = null ) {
48        foreach ( $this->deps as $dep ) {
49            if ( $dep->isExpired( $callback ) ) {
50                return true;
51            }
52        }
53
54        return false;
55    }
56
57    /**
58     * Initialise dependency values in preparation for storing. This must be
59     * called before serialization.
60     */
61    public function initialiseDeps() {
62        foreach ( $this->deps as $dep ) {
63            $dep->loadDependencyValues();
64        }
65    }
66
67    /**
68     * Get the user-defined value
69     * @return bool|mixed
70     */
71    public function getValue() {
72        return $this->value;
73    }
74
75    /**
76     * Store the wrapper to a cache
77     *
78     * @param BagOStuff $cache
79     * @param string $key
80     * @param int $expiry
81     */
82    public function storeToCache( $cache, $key, $expiry = 0 ) {
83        $this->initialiseDeps();
84        $cache->set( $key, $this, $expiry );
85    }
86
87    /**
88     * Attempt to get a value from the cache. If the value is expired or missing,
89     * it will be generated with the callback function (if present), and the newly
90     * calculated value will be stored to the cache in a wrapper.
91     *
92     * @param BagOStuff $cache
93     * @param string $key The cache key
94     * @param int $expiry The expiry timestamp or interval in seconds
95     * @param callable|false $callback The callback for generating the value, or false
96     * @param array $callbackParams The function parameters for the callback
97     * @param array $deps The dependencies to store on a cache miss. Note: these
98     *    are not the dependencies used on a cache hit! Cache hits use the stored
99     *    dependency array.
100     *
101     * @return mixed The value, or null if it was not present in the cache and no
102     *    callback was defined.
103     */
104    public static function getValueFromCache( $cache, $key, $expiry = 0, $callback = false,
105        $callbackParams = [], $deps = []
106    ) {
107        $obj = $cache->get( $key );
108
109        if ( is_object( $obj ) && $obj instanceof DependencyWrapper && !$obj->isExpired() ) {
110            $value = $obj->value;
111        } elseif ( $callback ) {
112            $value = $callback( ...$callbackParams );
113            # Cache the newly-generated value
114            $wrapper = new DependencyWrapper( $value, $deps );
115            $wrapper->storeToCache( $cache, $key, $expiry );
116        } else {
117            $value = null;
118        }
119
120        return $value;
121    }
122}